/*

Social Media links for AboutSpecials promotion pages
Author : Mario Edgar
Date : May 2010

*/

var googlePlusOneClick;

jQuery(function() {				
				
	var sm = Cobalt.Social.ShareThis,
		socialMediaAs = {},
		socialMediaAsString ="",
		dealerSite = ContextManager.getDealershipName(),
		appender=(!isEmpty(pageUrlUtility.getParameter("promo")))?findPromoCode("promo"):((!isEmpty(pageUrlUtility.getParameter("p")))?findPromoCode("p"):findPromoCode("")),
		socialMediaAsPromoName=appender.promoName, socialMediaAsUrl=appender.url,
		googlePlusMarkup = "<g:plusone annotation='none' callback='googlePlusOneClick' href='"+sMAsUrl('_googlePlus')+"'></g:plusone>";
	
	function findPromoCode(p) {
		if (p!="") {
			var pName=pageUrlUtility.getParameter(p);
			return {promoName:pName, url:pageUrlUtility.getBaseHref()+ContextManager.getPageName()+"?"+p+"="+pName};
		} else {
			return {promoName:Cobalt.page_promoCode, url:pageUrlUtility.getBaseHref()+ContextManager.getPageName().split("_D")[0]}
		}
	}
	 
	setupSocial();
	
	function setupSocial(){
		socialMediaAs.facebook = { 
			"type" : "facebook",			
			"img" : "images/socialMedia/share.png",
			"alt" : "Share on Facebook"
		};		
		socialMediaAs.twitter = { 
			"type" : "twitter",			
			"img" : "images/socialMedia/tweet.png",
			"alt" : "Share on Twitter"
		};
		socialMediaAs.email = { 		
			"type" : "email",			
			"img" : "images/socialMedia/email.png",
			"alt" : "Email a friend"
		};	
		
		for (var i in socialMediaAs) {
			var tmp = socialMediaAs[i];
			socialMediaAsString += "<a style='cursor:pointer;' rel='"+tmp.type+"' ><img style='border:none; margin-right: 10px;' src='"+tmp.img+"' alt='"+tmp.alt+"' title='"+tmp.alt+"' /></a>";
		}		
		jQuery("#socialMediaAsDiv").html(socialMediaAsString+googlePlusMarkup);
		setupGooglePlus();
	}
	
	function setupGooglePlus(){
		/* src: http://www.google.com/webmasters/+1/button/ */
		var po = document.createElement('script'); 
		po.type = 'text/javascript'; 
		po.async = true;
    	po.src = 'https://apis.google.com/js/plusone.js';
    	var s = document.getElementsByTagName('script')[0]; 
		s.parentNode.insertBefore(po, s);
	}
	
	jQuery("#socialMediaAsDiv a").click(function(){
		var tmp = jQuery(this), popWin, currentSocialType = tmp.attr("rel");
        switch(currentSocialType){		
			case "facebook":
				popWin = window.open('','');
            	sm.shortenUrl(sMAsUrl('_fb'),
                	function(url) {socialCallBack(url,popWin,currentSocialType)});
              	break;
            case "twitter":
				popWin = window.open('','');
                sm.shortenUrl(sMAsUrl('_tw'),
                	function(url) {socialCallBack(url,popWin,currentSocialType)});
                break;
			case "email":
				sm.shortenUrl(sMAsUrl('_email'),
					function(url) {socialCallBack(url,popWin,currentSocialType)});
				break;
		}		
	});

	function sMAsUrl(type){
		var conjunction="?";
		if (socialMediaAsUrl.indexOf("?")>-1) {conjunction="&";}
		   return socialMediaAsUrl+conjunction+'cs:a='+socialMediaAsPromoName+type;
	}
		
	function socialCallBack(url,popWin,socialType) {
		if(isEmpty(url)){url = socialMediaAsUrl;}
		var tmp = url.substring(0, 13),fullUrl = '',isShortened = false;
		if(tmp == "http://bit.ly"){isShortened = true};
		
		if(socialType == "facebook"){
			if(isShortened){
				fullUrl = "http://www.facebook.com/share.php?u="+url;
			}
			else{
				fullUrl = "http://www.facebook.com/share.php?u="+socialMediaAsUrl+"%26cs%3Aa%3D"+socialMediaAsPromoName+"_fb";
			}
		}
		else if(socialType == "twitter"){
			if(window.getTwitterText){
				if(isShortened){
					fullUrl = "http://twitter.com/home?status="+window.getTwitterText()+url;
				}
				else{
					fullUrl = "http://twitter.com/home?status="+window.getTwitterText()+socialMediaAsUrl+"%26cs%3Aa%3D"+socialMediaAsPromoName+"_tw";
				}						
			}
			else{
				if(isShortened){
					fullUrl = "http://twitter.com/home?status=Current specials at "+dealerSite+"+"+url;
				}
				else{
					fullUrl = "http://twitter.com/home?status=Current specials at "+dealerSite+"+"+socialMediaAsUrl+"%26cs%3Aa%3D"+socialMediaAsPromoName+"_tw";
				}						
			}
		}
		else{
			if(isShortened){
				fullUrl = url;
			}
			else{
				fullUrl = socialMediaAsUrl+"%26cs%3Aa%3D"+socialMediaAsPromoName+"_email";
			}
		}	
		tagNgo(socialType,fullUrl,popWin);
	}
	
	function tagNgo(tag,url,popWin){
		var promoPixelTag = {
			pageLayout : 'linkout_'+socialMediaAsPromoName+"_"+tag,
			pageName : socialMediaAsPromoName+"_"+tag,		
			pageLabel : 'linkout_'+socialMediaAsPromoName+"_"+tag+'_'+ContextManager.getPageName()+'&cs:lo='+socialMediaAsPromoName+"_"+tag
		};
		/* Publish the event */
		EventManager.publish({
			eventName:'com.cobaltgroup.ws.action.click.socialMedia',
			publisherData:promoPixelTag
		});		
		if(isEmpty(popWin)){
			setupEmailFriend(url);
		}
		else{
			redirect(url,popWin);
		}
	}

	function setupEmailFriend(url){
		_EmailAFriend.prototype.getUrl = function() {
			return url;
		}	
		_EmailAFriend.prototype.close = function(horizontalElement, verticalElement) {
			document.body.removeChild(document.getElementById(_EmailAFriend.IFRAME_ID));
		}
		_EmailAFriend.prototype.open = function(horizontalElement, verticalElement, vehicleId) {
			var iframe = document.createElement("iframe");
			iframe.setAttribute("id", _EmailAFriend.IFRAME_ID);
			iframe.name = _EmailAFriend.IFRAME_ID;
			iframe.style.position = "absolute";
			iframe.style.top = "0px";
			iframe.style.visibility = "hidden";
			iframe.style.zIndex = 1000;
			iframe.src = "about:blank";
			iframe.setAttribute("border", "0");
			iframe.setAttribute("frameBorder", "0");
			iframe.setAttribute("scrolling", "no");
			document.body.appendChild(iframe);
			this.hPositionElement = horizontalElement;
			this.vPositionElement = verticalElement;
			iframe.src = "f_EmailAFriend" + (typeof vehicleId != 'undefined' ? ("?vehicleId=" + vehicleId) : ""); 
		}	
		EmailAFriend.getInstance().open(null,null,null);
	}	

	function redirect(url,popWin) {
		if(popWin == null) {
			setTimeout(function(){
				redirect(url,popWin)
			},100);
		}
		else{
			popWin.location = url;
		    popWin.focus();
		}
	};
  	
	/* This function needs to be at the global namespace */
	googlePlusOneClick = function(){
		var tag="googlePlus";
		var promoPixelTag = {
			pageLayout : 'linkout_'+socialMediaAsPromoName+"_"+tag,
			pageName : socialMediaAsPromoName+"_"+tag,		
			pageLabel : 'linkout_'+socialMediaAsPromoName+"_"+tag+'_'+ContextManager.getPageName()+'&cs:lo='+socialMediaAsPromoName+"_"+tag
		};
		EventManager.publish({
			eventName:'com.cobaltgroup.ws.action.click.socialMedia',
			publisherData:promoPixelTag
		});	
	}	
	
	function isEmpty(testVar){
		if ((testVar == undefined) || (testVar == null) || (testVar == "null") || (testVar == "") || (testVar == "undefined")) {
				return true;
			}
		return false;
	}	
});
