

$(document).ready(function() {
	// invoke the code formerly known as "the awesome"
	//$("#shareholder").social({type:'photo', item:'remote'});
	$("#gallery_main h2").social({type:'photoSet', item:'remote'});
	$("#shareholder[remote]").social({type:'video', item:'remote'});
	$("#intro2 .video h2[playlist]").social({type:'videoGallery', item:'remote'});
	
	
});

$(window).bind("beforeunload", function(){
		
	// pass vehicleCode to Social KMU popup
   	vehCode = vehCode.toUpperCase();
	vehURL = "/MusaWeb/displaySocialHandraiser.action?vehicleCode="+ vehCode;
	
	// If user shared and didn't see pop up before it has been displayed
	if(($.cookie('shared')) && (!$.cookie('socialMediaKMUSubmitted'))){
		//$.cookie('socialMediaKMUSubmitted', true, { expires: 30 });
		window.open(vehURL,'KMUPopup','width=550,height=375,scrollbars=yes');
	}

});





(function($){
	
	/*	
	*	$.fn.social // Inserts Social share menu
	*	
	*	Receives:
	*		type	shareable type, defaults to photo 
	*		item	shareable source to be shared, defaults to actual.
	*				if remote finds related file at flick by call to XML.
	**/
	
	$.fn.social = function(options){     
		
		var defaults = {  
			type: 'photo', // photo, photoSet, video, videoGallery
			item: 'actual' // actual, remote
		};  
		var options = $.extend(defaults, options);
		
		// List of supported networks
		var shareNetworks = '<div class="sharefloat"><ul class="shareNetworks" style="z-index: 1000; background-color: #fff;">'+
					'<li><div class="shareIcon url">&nbsp;</div>'+
					'<div class="pitf" onclick="$.fn.social.share(\'url\', \''+options.type+'\', \''+options.item+'\');">Copy URL</div></li>'+
					'<li><div class="shareIcon facebook">&nbsp;</div>'+
					' <div class="pitf" onclick="$.fn.social.share(\'facebook\', \''+options.type+'\', \''+options.item+'\');">Facebook</div></li>'+
					'<li><div class="shareIcon myspace">&nbsp;</div>'+
					' <div class="pitf" onclick="$.fn.social.share(\'myspace\', \''+options.type+'\', \''+options.item+'\');">My Space</div></li>'+
					'<li><div class="shareIcon digg">&nbsp;</div>'+
					' <div class="pitf" onclick="$.fn.social.share(\'digg\', \''+options.type+'\', \''+options.item+'\');">digg</div></li>'+
					'<li><div class="shareIcon delicious">&nbsp;</div>'+
					' <div class="pitf" onclick="$.fn.social.share(\'delicious\', \''+options.type+'\', \''+options.item+'\');">del.icio.us</div></li>'+
					'<li><div class="shareIcon twitter">&nbsp;</div>'+
					' <div class="pitf" onclick="$.fn.social.share(\'twitter\', \''+options.type+'\', \''+options.item+'\');">Twitter</div></li>';
			// no Flicker sharing for videos
			if((options.type == 'photo') || (options.type == 'photoSet')){
				shareNetworks +='<li><div class="shareIcon flickr">&nbsp;</div>'+
					' <div class="pitf" onclick="$.fn.social.share(\'flickr\', \''+options.type+'\', \''+options.item+'\');">Flickr</div></li>';
			}
			shareNetworks +='</ul></div>'
		
		// Sets different type of menus for each type of asset
		switch(options.type){
			case 'photo':
				var shareMenu =	'<div class="shareMenuPhoto">'+
					'<ul class="shareTag">'+
					'<li>'+
					'<div class="tagTitle">SHARE THIS IMAGE</div>'+
					shareNetworks+
					'</li></ul></div><div class="spacer" style="clear: both;"></div>'+
					'<!-- End of .shareMenu -->';
				break;
			case 'photoSet':
				var shareMenu =	'<div class="shareMenuSet">'+
					'<ul class="shareTag">'+
					'<li>'+
					'<div class="tagTitle"><a href="javascript:void(0)">Share this Slideshow</a></div>'+
					shareNetworks+
					'</li></ul></div><div class="spacer" style="clear: both;"></div>'+
					'<!-- End of .shareMenu -->';
				break;
			case 'video':
				var shareMenu =	'<div class="shareMenuVideo">'+
					'<ul class="shareTag">'+
					'<li>'+
					'<div class="tagTitle">SHARE THIS DEMO</div>'+
					shareNetworks+
					'</li></ul></div><div class="spacer" style="clear: both;"></div>'+
					'<!-- End of .shareMenu -->';
				break;
			case 'videoGallery':
				var shareMenu =	'<div class="shareMenuSet">'+
					'<ul class="shareTag">'+
					'<li>'+
					'<div class="tagTitle"><img src="musa2/images/garage/gallery/social/share_video_gallery.gif"/></div>'+
					shareNetworks+
					'</li></ul></div><div class="spacer" style="clear: both;"></div>'+
					'<!-- End of .shareMenu -->';
				break;
		}

		
		// Inserts corresponding menu to each element matched by selector
		return this.each(function(){
			
			switch(options.type){
				case 'photo':
					
					$(this).html(shareMenu);
					var thisMenu = $(this).find(".shareMenuPhoto");
					break;
					
				case 'photoSet':
					$(this).before(shareMenu);
					var thisMenu = $(this).parent().find(".shareMenuSet");
					break;
					
				case 'video':
					$(this).html(shareMenu);
					var thisMenu = $(this).find(".shareMenuVideo");
					break;
					
				case 'videoGallery':
					$(this).before(shareMenu);
					var thisMenu = $(this).parent().find(".shareMenuSet");
					break;
			}
			
			// Binds mouseover / mouseout behaviour to this menu
			thisMenu.bind("mouseover mouseout", function(){
        		if($(".shareNetworks", thisMenu).css('display') == 'block'){
					$(".shareNetworks", thisMenu).css('display', 'none');
				}else{
					$(".shareNetworks", thisMenu).css('display', 'block');
					// In case it is a video we keep track of which is the one being shared
					if(options.type == 'video'){
						currAsset = $('#shareholder').attr('remote');
					}
					if(options.type == 'videoGallery'){
						currAsset = thisMenu.parent().find('h2').attr('playlist');
					}
				}
    		});
			
		});

	}; 
	
	
	/*	
	*	$.fn.social.share // Opens sharing popup
	*	
	*	Receives:
	*
	*		service		where assets are going to be shared
	*		type		shareable type, defaults to photo 
	*		item		source of asset to be shared, defaults to "actual".
	*					if "remote" looks for corresponding remote file.
	**/
	
	$.fn.social.share = function(service, type, item){ 
	
		var title = vehModel;
		var winFeatures = 'width=750,height=500,scrollbars=1';
		var assetURL;
		var embed;
		var tweet;
		var flickrURL;
		
		//Omniture Variable Declarations		
		var shareId='';
		var shareType='';
		var linkIdx='';
		var sectionIdx=$("div.tabholder_on div a").text();
		var assetIdx=$("#gallery_glory img").attr("id");
		
		
		// if user shares, track it for the popup
		$.cookie('shared', true, { expires: 30 });

		// Sets actual or remote assetURL
		switch(type){
			case 'photo':
				// We are sharing a remote asset
				if(item != 'actual'){
					assetURL = currAsset;
				// We are sharing the actual file
				}else{
					assetURL = $('#imageDisplay img.bigPhoto').attr('src');
				}
				embed = '<img src="'+assetURL+'" alt="'+title+'" />';
				tweet = title+' '+assetURL;
				flickrURL = flickrImg;
				shareType="Photo";
				
				break;
			
			case 'photoSet':
				assetURL = $('.tabholder_on a.tablink').attr('set');
				embed = '<a href="'+assetURL+'" target="_blank"><img width="200" src="'+$('.thumbs.active ul.clearfix li:first a img').attr('remote')+'" alt="'+title+' gallery" /><br />'+title+' gallery</a>';
				tweet = title+' gallery '+assetURL;
				flickrURL = $('.tabholder_on a.tablink').attr('set');
				shareType="Slideshow";
				break;

			case 'video':
				assetURL = 'http://www.youtube.com/watch?v='+currAsset;
				embed = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'+currAsset+'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+currAsset+'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>';
				tweet = title+' http://www.youtube.com/watch?v='+currAsset;
				flickrURL = $('.thumbs:first h2').attr('collection');
				shareType="Video";
				break;
			
			case 'videoGallery':
				assetURL = 'http://www.youtube.com/view_play_list?p='+currAsset;
				embed = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/p/'+currAsset+'&hl=es&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/p/'+currAsset+'&hl=es&fs=1" type="application/x-shockwave-flash" width="480" height="385" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
				tweet = title+' http://www.youtube.com/view_play_list?p='+currAsset;
				flickrURL = $('.thumbs:first h2').attr('collection');
				shareType="Videos";
				break;
		}
		
		
		
		
		
		// Pops up sharing window
		switch(service) {
			case 'url':
				prompt('Copy and paste this code into your own website or blog', embed);
				break;
			case 'facebook':
				linkIdx="Facebook";
				open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(assetURL)+'&t='+encodeURIComponent(title),'Facebook',winFeatures);			
				break;
			case 'myspace':
				linkIdx="MySpace";
				open('http://www.myspace.com/Modules/PostTo/Pages/?t='+encodeURIComponent(title)+'&c=&u='+encodeURIComponent(assetURL)+'&l=4','MySpace',winFeatures);
				break;	
			case 'digg':
				linkIdx="Digg";
				open('http://digg.com/submit?url='+encodeURIComponent(assetURL)+'&title='+encodeURIComponent(title)+'&media=image&topic=autos','Digg',winFeatures);
				break;				
			case 'delicious':
				linkIdx="Delicious";
				open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(assetURL)+'&title='+encodeURIComponent(title),'Delicious',winFeatures);
				break;
			case 'twitter':
				linkIdx="Twitter";
				open('http://twitter.com/home?status='+encodeURIComponent(tweet),'Twitter',winFeatures);
				break;
			case 'flickr':			
				linkIdx="Flickr";
				alert(flickrURL);
				open(flickrURL,'Flickr',winFeatures);
				break;
			
		}
		//Concatenate Variables and Fire Omniture
		
		shareId="Share Gallery " + shareType + "To " + linkIdx + " - " + vehCode + " " + sectionIdx + " " + shareType;
		if(shareType!="Slideshow") shareId += " " + assetIdx;
		s.linkTrackVars='eVar6,eVar28,s.events';
		//s.linkName=shareId;
		s.linkTrackEvents='event10';
		s.eVar6=shareId;
		s.eVar28=shareId;
		s.events='event10';
		void(s_gi(s_account));
		s.tl(this, 'o', shareId );

	};
	
})(jQuery);  