/**
2 * @author larryroot
3 */

(function($){
	$.musa_gallery={
		
	
		_init:function(){
			$.musa_gallery.linkTabs();
			//$.musa_gallery.hookupBackNext();
			//$("a.prev").addClass("hiddenprev");
			//$("a.hiddenprev").removeClass("prev");
			
			
			//Hookup thumbs
			$('.items a').click(function(e){
				e.stopPropagation();
				e.preventDefault();
				if($(this).parent().parents().hasClass("video")){
					$.musa_gallery.displayVideo($(this));
				}else{
					if($(this).hasClass('flash360')){
						$.musa_gallery.display360($(this));
					}else{
						$.musa_gallery.displayImage($(this));
					}
				}
				return false;	
				
			})
			//Remove any loading states
			$('.loading').removeClass('loading');
		},
		/*
		hookupBackNext: function() {
			$('#veh_gallery_backnext a').click(function(e){
				e.stopPropagation();
				e.preventDefault();

				var link = $(this);
				if (link.hasClass('back')) {
					var prevImg = $('.thumbs.active li.on').prev('li.thumbnail');
					if ( prevImg.length > 0) {
						$.musa_gallery.displayImage(prevImg);
					} else {
						$.musa_gallery.displayImage( $('.thumbs.active li:last') );
					}
				} else if (link.hasClass('next')) {
					var nextImg = $('.thumbs.active li.on').next('li');
					if ( nextImg.length > 0) {
						$.musa_gallery.displayImage(nextImg);
					} else {
						$.musa_gallery.displayImage( $('.thumbs.active li.thumbnail:first') );
					}
				}
				return false;
			});
			
		},
		*/
		linkTabs:function(){
			$('a.tablink').each(function(){
				$(this).click(function(e){ //prevent omniture from hanging the UI
					e.stopPropagation();
					e.preventDefault();					
					return false;
				});
				
				//Attach thumbnail element to tab
				$(this).mousedown(function(e){
					$.musa_gallery.activateTab( $(this) );
				});				
			});
			$.musa_gallery.activateTab( $('div.tabholder:first div a') );
		},
		
		activateTab:function(tab){
			var currenttab=tab.attr('href');

			//Quit if active tab was clicked
			if (tab.parent().parent().hasClass('tabholder_on')) return;


			// Reset prev/next icons during every tab change
			$("a.hiddenprev").addClass("prev");
			$("a.prev").removeClass("hiddenprev");

			$("a.hiddennext").addClass("next");
			$("a.next").removeClass("hiddennext");
			
			//Reset all tabs and thumb display
			$('.tabholder_on').removeClass('tabholder_on');
			//$('.thumbs.active').removeClass('active');
			//$('.thumbs.active li.on').removeClass('on');
			
			//Active passed in tab
			tab.parent().parent().addClass('tabholder_on');
			$('div.items').html($('#' + tab.attr('href')).html());
			
			for(i=0; i<$("div.items a img").length; i++) {
				$("div.items a img").eq(i).attr("name", i+1 );
			}
			
			var scrollmin;
			if($('div.items a').length > 4) scrollmin=5;
			else scrollmin=$('div.items a').length;

			$("#scrollable").scrollable({size:scrollmin,items:'.items',horizontal:true});
			
			$('.items a').mouseover(function() {
				$(this).addClass("mouse");
			
			});
			
			$('.items a').mouseout(function() {
				$(this).removeClass("mouse");
			
			});
			
			
			$('.items a').click(function(e){
				e.stopPropagation();
				e.preventDefault();
				if(currenttab=="video"){
					$.musa_gallery.displayVideo($(this));
				}else if(currenttab=="360"){
					$.musa_gallery.display360($(this));
				}else{
					$.musa_gallery.displayImage($(this));				
				}
				return false;	
				
			})
			/*if(tab.find('a').text() != 'Videos'){
				//Display first image
				$('#gallery_glory').css('background-color', '#333333');
				$.musa_gallery.displayImage( $('.thumbs.active ul.clearfix li.thumbnail:first') );
			}else{
				$('#gallery_glory').css('background-color', 'white');
				//Display first video
				$.musa_gallery.displayVideo( $('.thumbs.active ul.clearfix li:first') );
			}*/
			$.musa_gallery.displayImage($("div.items a:first"));
			
			// This forced prev hack is used to remove prev icon after switching tabs
			$("#scrollable").scrollable("prev");

		},
		
		displayPrevImage: function() {
			var prevThumb='';
		
			prevThumb=$("a.active").prev();				
			$("#scrollable").scrollable("prev"); 
			$.musa_gallery.displayImage($(prevThumb));
		},
		
		displayNextImage: function() {
			var nextThumb='';
			nextThumb=$("a.active").next();
			$("#scrollable").scrollable("next"); 
			$.musa_gallery.displayImage($(nextThumb));
		},
		
		displayImage:function(thumbImg){
			// Removes embed if flash360 or flash video was used before
			
			$("#shareholder").removeClass("sharevideo");
			$("#shareholder").addClass("sharephoto");
			
			
			if($('#gallery_glory img.bigPhoto')){
				$('#gallery_glory img.bigPhoto').remove();
			}
							
			$('#gallery_glory').html('<img src="'+thumbImg.attr('href')+'" class="bigPhoto" id="' + thumbImg.children().attr('name')+'" />').show();
			
			//$("#gallery_glory img.bigPhoto").social({type:'photo', item:'remote'});
			
			
			currAsset = thumbImg.find('img').attr('remote'); // Global variable
			flickrImg = thumbImg.find('img').attr('flickr'); // Global variable
			$('#wallpaperDisplay').hide();
			
			
			
			$('div.items a.active').removeClass('active');
			thumbImg.addClass('active');
			$("#shareholder").social({type:'photo', item:'remote'});
			
			//places description text into bar over bottom of glory image
			$("#info_band div.info").html(thumbImg.find('img').attr('description'));
			if($("a.active").is(":last-child")) $("#nexttop").hide(); 
			else $("#nexttop").show();
			
			if($("a.active").is(":first-child")) $("#prevtop").hide(); 
			else  $("#prevtop").show(); 			
		},
		
		display360:function(thumb360){
			//var assetURL = "http://images.mazdausa.com/MusaWeb/";
			//var flash360 = thumb360.find('img').attr('flash');
			var flashView = thumb360.find('img').attr('view');
			var vehicleCode = vehCode;
			vehCode = vehCode.toLowerCase();
			
			// vehCode and vehYear is set in p_models_gallery.jsp
			if (vehYear == "2010"){
				var moviePath = assetURL + "flash/vehicles/garage/2010/" + vehCode + "/musa/360/" + "e_garage_" + vehCode + "_360_" + flashView + ".swf";
				var movieBase = assetURL + "flash/vehicles/garage/2010/" + vehCode + "/musa/360/";
			}else{
				var moviePath = assetURL + "flash/vehicles/garage/" + vehCode + "/360/" + "e_garage_" + vehCode + "_360_" + flashView + ".swf";
				var movieBase = assetURL + "flash/vehicles/garage/" + vehCode + "/360/";
			}

			
			
			$('#videoDisplay').html('<a href=\"http://get.adobe.com/flashplayer/\"><img src=\"images/vehicles/gallery/downloadFlash_reveal.jpg\" width=\"750\" height=\"319\" border=\"0\" alt=\"\"  /></a>');
			$('#videoDisplay').flash({ 
				swf: moviePath, 
				height: 319, 
				width: 750, 
				params: { 
					play: true,
					base: movieBase,
					wmode: 'transparent',
					scale: 'noscale',
					name: 'rotation360',
					quality:'high',
					bgcolor:'#fff',
					flashvars: { 
						assetURL: assetURL, 
						streamingUrl:'cp23260.edgefcs.net/production', 
						applicationUrl: 'http://www.mazdausa.com' 
					} 
				} 
			});
			
			
			
			
			$('#gallery_glory').hide();
			$('#videoDisplay').show();
		},
		
		displayVideo:function(thumbVideo){
			var assetURL = "http://images.mazdausa.com/MusaWeb/";
			var video = thumbVideo.find('img').attr('flv');
			var flv_width = thumbVideo.find('img').attr('flv_width');
			var flv_height = thumbVideo.find('img').attr('flv_height');
			var remotevar= thumbVideo.find('img').attr('remote');
			$("#shareholder").removeClass("sharephoto");
			$("#shareholder").addClass("sharevideo");
			$("#shareholder").attr({remote: remotevar});
			
			// videos
			if (vehCode == "MZ6"){
				vehCode = "M6S";
			}
	
			vehCode = vehCode.toLowerCase();
	
			var movieVersion = 'base';
			var moviePath = assetURL + "flash/vehicles/garage/" + vehCode + "/videos/p_garage_" + vehCode + "_videos_base.swf";
			var movieBase = assetURL + "flash/vehicles/garage/" + vehCode + "/videos/";
			var appURL1 = location.protocol;
			var appURL2 = location.hostname;
			
			
			$('#gallery_glory').html('<a href=\"http://get.adobe.com/flashplayer/\"><img src=\"images/vehicles/gallery/downloadFlash_reveal.jpg\" width=\"750\" height=\"319\" border=\"0\" alt=\"\"  /></a>');
			$('#gallery_glory').flash({ 
				swf: moviePath, 
				height:  319, 
				width: 750, 
				params: { 
					play: true,
					base: movieBase,
					wmode: 'transparent',
					scale: 'noscale',
					name: 'bod',
					quality:'high',
					align: '',
					bgcolor:'#fff',
					flashvars: { 
						assetURL: assetURL, 
						videoFile: video,
						videoWidth: flv_width,
						videoHeight: flv_height, 
						streamingUrl:'cp23260.edgefcs.net/production', 
						applicationUrl: 'http://www.mazdausa.com' 
					} 
				} 
			});
			$("#shareholder").social({type:'video', item:'remote'});
		}
	}

	$.musa_gallery._init();
})(jQuery);


