﻿/* 
Budweiser.com Downloads Downloads JavaScript file 
Register mouse events
Resizing preview container widths
*/

Preview = {
	adjust:function(){
		
		var screenWidth = $(window).width();
							
        // current content div
		var contentWidth = $('li.content-view').width();
		var marginWidth = $('li.content-view').css('margin-left').match(/^\d+/);
		var centerWidth = 2 * marginWidth + contentWidth;
		
		var minPreviewWidth = (1024 - centerWidth)/2;
		var maxPreviewWidth = (1680 - centerWidth)/2;
				
		var newWidth = Math.floor((screenWidth - centerWidth) / 2);
		newWidth = newWidth >= minPreviewWidth ? newWidth : minPreviewWidth;
		newWidth = newWidth < maxPreviewWidth ? newWidth : maxPreviewWidth;
				
		//console.log('screenWidth: ' + screenWidth + ' contentWidth: ' + contentWidth + ' marginWidth: ' + marginWidth);

        $('li.content-view').css('left', newWidth + 'px');
	}
};

$(document).ready( function(){

	Preview.adjust();
	$(window).bind('resize', Preview.adjust);

    $('#left-arrow-container a').mouseover( function(e) {
        imgsrc = $(this).children('img').attr('src');
        matches = imgsrc.match(/-active/);
        
        if (!matches) {
            imgsrcON = imgsrc.replace(/.png$/ig, '-active.png');
            $(this).children('img').attr('src', imgsrcON);
        }
    });

    $('#left-arrow-container a').mouseleave( function(e) {
        imgsrc = $(this).children('img').attr('src');
        matches = imgsrc.match(/-active/);
        
        if (matches) {
            imgsrcOFF = imgsrc.replace(/-active.png$/ig, '.png');
            $(this).children('img').attr('src', imgsrcOFF);
        }
    });

    $('#right-arrow-container a').mouseover( function(e) {
        imgsrc = $(this).children('img').attr('src');
        matches = imgsrc.match(/-active/);
        
        if (!matches) {
            imgsrcON = imgsrc.replace(/.png$/ig, '-active.png');
            $(this).children('img').attr('src', imgsrcON);
        }
    });

    $('#right-arrow-container a').mouseleave( function(e) {
        imgsrc = $(this).children('img').attr('src');
        matches = imgsrc.match(/-active/);
        
        if (matches) {
            imgsrcOFF = imgsrc.replace(/-active.png$/ig, '.png');
            $(this).children('img').attr('src', imgsrcOFF);
        }
    });

});

var urlValues = new Array();
Downloads = {
	isVideoContent: function() {
		return ($('#downloads-commercials').length > 0);
	},

	isAudioContent: function() {
		return ($('#downloads-ringtones-landing').length > 0);
	},

	initialize: function () {
		Downloads.setupGallery();
	},

	setupGallery: function () {
		// Define constants
		var ACTIVE_CLASS = 'active',
			CURRENT_CLASS = 'current',
			FADE_SPEED = 400,
			INACTIVE_CLASS = 'inactive',
			SELECTED_CLASS = 'selected';

		// Define commonly used elements
		var galleryContainer = $('#downloads-container').get(0),
			galleryItems = $('li.content-view', galleryContainer).get(),
			navigationContainer = $('#section-thumbstrip').get(0);
			navigationItems = $('li', navigationContainer).get();

		// Initial setup
		$('a', navigationItems).each(function (i) {
			$(this).parent().html($(this).html());
		});

		$(navigationItems)
			.bind('mouseover',function (e) {
				$(this).addClass(ACTIVE_CLASS);
				$(this).removeClass(INACTIVE_CLASS);
			}).bind('mouseout',function (e) {
				$(this).addClass(INACTIVE_CLASS);
				$(this).removeClass(ACTIVE_CLASS);
			});

		// Create the gallery
		var gallery = new Bud.ui.Gallery({
			galleryItems: galleryItems,
			navigationItems: navigationItems
		});

		$(gallery.galleryItems).each(function (index) {
			if ($(this).hasClass(CURRENT_CLASS)) {
				gallery.goTo(index);
			}
		});

		$(gallery).bind('update', function (event) {
			// Stop any playing video or audio
			if(Downloads.isVideoContent()){
				Player.stopVideo();
			} else if (Downloads.isAudioContent()){
				Player.stopAudio();
			}

			// Animate gallery items
			$(gallery.galleryItems[gallery.lastIndex]).fadeOut(FADE_SPEED, function (event) {
				$(this).removeClass(CURRENT_CLASS);
			});
			$(gallery.galleryItems[gallery.currentIndex]).fadeIn(FADE_SPEED, function (event) {
				$(this).addClass(CURRENT_CLASS);
			});

			// Change navigation state
			$(gallery.navigationItems[gallery.lastIndex]).removeClass(SELECTED_CLASS).addClass(INACTIVE_CLASS);
			$(gallery.navigationItems[gallery.currentIndex]).addClass(SELECTED_CLASS);

			// Hide overlay containers
			$('li.content-view .screensaver-platform-container').hide();
			$('li.content-view .desktop-wallpaper-resolution-container').hide();
			$('li.content-view .video-platform-container').hide();
		    $('li.content-view .mobile-wallpaper-form').hide();
		});
	}
};

/**
 * Three strikes and then refactor...
 */
Player = {
	showError: false,
	stopVideo:function(){
		$(".flash-content object").each(Player.stopVideoCatchError);
	},

	stopAudio: function() {
		$(".flash-content object").each(Player.stopAudioCatchError);
	},
	
	showException: function(e) {
		if (Player.showError) {
			alert(e);
		}
	},

	/**
	 * In the context in which these two functions are used
	 * 'this' refers to the dom node that is a flash move.
	 */
	stopVideoCatchError: function(i) {
		try {
			this.stopVideo();
		} catch(e) {
			Player.showException(e);
		}
	},

	stopAudioCatchError: function(i) {
		try {
			if (this.isAudioPlaying()) {
				this.stopAudio();
			}
		} catch (f) {
			Player.showException(f);
		}
	}		
};


$(document).ready(Downloads.initialize);