/**
2 * 
3 */

var galleryId='';

// Prevent two omniture calls on page load
var initialGalleryLoad = 0;

(function($){
	$.musa_gallery={
		
	
		_init:function(){
			$.musa_gallery.linkTabs();
			
			//Hookup thumbs
			$('.items a').click(function(e){
				e.stopPropagation();
				e.preventDefault();				
				return false;	
				
			})
			//Remove any loading states
			$('.loading').removeClass('loading');
			$.musa_gallery.displayImage($("div.items a:first"));
		},
		
		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');
			galleryId=currenttab;
			//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");
			
			if(currenttab=="video" || currenttab=="360" ){
				$("#prevtop").hide(); 
				$("#nexttop").hide(); 
			} 
			
			
			//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');
			
			//Copy thumbs for a section into gallery
			$('div.items').html($('#' + tab.attr('href')).html());
			
			//sequentially name the images copied into the gallery
			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') );
			}*/
			if(currenttab=="ext" || currenttab=="int") $.musa_gallery.displayImage($("div.items a:first"));
			else if(currenttab=="video") $.musa_gallery.displayVideo($("div.items a:first"));
			else $.musa_gallery.display360($("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
			$('#gallery_glory').show();
			$('#videoDisplay').hide();
			$("#shareholder").show();
			$("#shareholder").removeClass("sharevideo");
			$("#shareholder").addClass("sharephoto");
			$("div.slideholder").show(); 
			
			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();

			// Do not call omniture on gallery load
			if (initialGalleryLoad >= 2)
				$.musa_gallery.fireOmniture(thumbImg.children().attr('name'));
			else initialGalleryLoad++;
			
			//$("#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 flashView = thumb360.find('img').attr('view');
			var vehicleCode = vehCode;
			vehCode = vehCode.toLowerCase();
			$("#info_band div.info").html('');
			$("#shareholder").hide();
			$("div.slideholder").hide();
			
			$('div.items a.active').removeClass('active');
			thumb360.addClass('active');
			
			var moviePath= appURL + "/MusaWeb" + baseHREF + "base" + flashView + "360.swf";
			var movieBase= appURL + "/MusaWeb" + baseHREF;
			$('#videoDisplay').flash({ 
				swf: moviePath, 
				height: 443, 
				width: 975, 
				params: { 
					play: true,
					base: movieBase,
					wmode: 'transparent',
					scale: 'noscale',
					name: 'rotation360',
					quality:'high',
					bgcolor:'#fff',
					flashvars: { 
						assetURL: appUrl, 
						streamingUrl:'cp23260.edgefcs.net/production', 
						applicationUrl: appUrl 
					} 
				} 
			});
			$('#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");
			$("#prevtop").hide(); 
			$("#nexttop").hide(); 
			$("div.slideholder").hide(); 
			
			$("#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:  443, 
				width: 975, 
				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'});
		},
		fireOmniture: function(imgId) {
			
			if (galleryId == "ext"){				
				galleryId = "exterior";
				
			}else if (galleryId == "int"){
				galleryId = "interior";
			}	

			s.pageName="musa:models_gallery_" + galleryId + "_photo[" + imgId + "]_" + vehCode;
			s.linkTrackVars="events";
			s.linkTrackEvents="event28,event36";
			s.events="event28,event36";
			void(s_gi(s_account));
			s.tl(this,"o","Gallery Views - [" + vehCode + "] - [" + galleryId + "] Photo[" + imgId + "]")
		}
	}

	$.musa_gallery._init();
})(jQuery);



