/**
 * @author Publicis Modem & Dialog
*/
var thirdPartyWarning = "You are leaving a Citi Website and going to a third party site. That site may have a privacy policy different from Citi and may provide less security than this Citi Site. Citi and its affiliates are not responsible for the products, services and content on a third-party website. Do you want to go to the third-party site?"
var pluginThirdPartyWarning = "Following contextual links for plugins could take you away from this Citi website. Leaving a Citi Website and going to a third party site. That site may have a privacy policy different from Citi and may provide less security than this Citi Site. Citi and its affiliates are not responsible for the products, services and content on a third-party website. Do you want to go to the third-party site?"
var pluginDisabled = "Sorry, the contextual menu for this plugin is disabled on this site."

$(document).ready(function(){
	setSpeedBumps();
	styleCaptionedImages(); 
});



function setSpeedBumps(){
var thirdPartyWarning = "goog?", url, permit = {'domains': ['blogmgmt.citi.com','www.citimortgage.com','blog.citimortgage.com','citibank.com','localhost', 'citi.chnmct113527.ad.infosys.com', 'blogsciti.chnmct113527.ad.infosys.com','citi.com','blog.womenandco.com','www.citigroup.com','www.citibank.com'] , 'target': ["#video_div a", ".read-full a"]}, nopermit = {'target': [".entry-asset a", "a.twitterlink",".fsrt_footer a",".footer a",".fsrq_footer a,a.youtubelink,a.facebooklink "]};
        jQuery(function () {
                for (var i = 0; i < nopermit.target.length; i = 1 + i) {
                        jQuery(nopermit.target[i]).each(function (index) {
                        var     url = jQuery(this).attr('href');
                                if (!/^javascript:/.test(url) && /http[s]?:\/\//.test(url) && !(new RegExp("/(www.)?" + window.location.host + "/")).test(url) && (jQuery(this).not(permit.target.toString()).length !== 0)) {
                                        jQuery(this).attr({'title': url, 'target': "_parent", 'href': '#'});
                                        jQuery(this).bind('click', function () {
											var domainname = this.title.match(/:\/\/(.[^/]+)/)[1];
                                                if ((jQuery.inArray(domainname, permit.domains) !== -1)) {
                                                        window.location.href=this.title;
                                                } else {
                                                        jQuery(this).attr({'title': url,'target': "_parent",'href':'#','click': launchSpeedBump});
                                                }
                                                return false;
                                        });
                                }
                        });
                }

        });

	setTimeout('loopPlugins()', 500);
}

/*
function setSpeedBumps(){
	var replaceLinks = $(".entry-asset a, a.twitterlink");
	var domainstripper = /:\/\/([^\/]+)/;
	var domain = domainstripper.exec(String(window.location))[1];
	var loopdomain = "";
	jQuery.each(replaceLinks, function(i, val){
		var targetUrl = $(val).attr('href');
		loopdomain = targetUrl.charAt(0) == "h" ? domainstripper.exec(targetUrl)[1] : domain;
		if(domain != loopdomain && targetUrl != 'javascript:void();' && loopdomain.indexOf('new.citi.com') == -1){
			$(val).attr('href','javascript:void();');
			$(val).attr('title', targetUrl);
			$(val).bind('click', launchSpeedBump)
			
			/*
			non-system, pop-up blocker friendly solution
			$(val).attr('href', '/thirdParty.html#'+targetUrl);
			$(val).attr('target','_blank');
			$(val).attr('title', targetUrl);
			
		}
	});
}
*/

function loopPlugins(){

	var plugins = $('object, embed');
       var count=0;
	jQuery.each(plugins, function(i, val){
		var curPlugin = $(val).parent();
		var notify = function(){
			window.alert(pluginThirdPartyWarning);
			return false;
		}
		var block = function(){
			window.alert(pluginDisabled);
			return false;
		}
		var blockMenu = function($e){
			if($e.button != 0){ 
				//var target  
				if($e.srcElement && $e.srcElement.nodeName == "OBJECT"){
					return notify();
				}else if($e.rangeParent.children[0].nodeName == "OBJECT"){
					return notify();
				}
			}
		}
		var blockIEMenu = function($e){
			if($e.which == 3){
				//var go = confirm(thirdPartyWarning);
				return block();
			}
		};
		if(window.addEventListener && count == 0){
			window.addEventListener("mousedown", blockMenu, true);
                     count++;
		}else if(count == 0){
			curPlugin.mousedown( blockIEMenu);
		}
	});
}

function styleCaptionedImages(){
	var clearedStyle = {
		'margin':"0 0 10px 0",
		'float':"none"
	}
	$(".image-wrapping").each(function(index){
		var thisOne = $("img", $(".image-wrapping")[index])
		var orgStyle = {
		  'float':thisOne.css("float"),
		  'margin':thisOne.css("text-align") == "center" && thisOne.css("float") != "left" && thisOne.css("float") != "right" ? "0 auto 20px" :thisOne.css("margin"),
		  'width':$("img", $(".image-wrapping")[index]).width()+"px",
		  'display':thisOne.css("text-align") == "center" && thisOne.css("float") != "left" && thisOne.css("float") != "right" ? "block" : "inherit"
		}
		$($(".image-wrapping")[index]).css(orgStyle);
		$("img", $(".image-wrapping")[index]).css(clearedStyle);

	})
}
function launchSpeedBump(){
	var go = confirm(thirdPartyWarning)
	if(go){
		window.open($(this).attr('title'))
	}
}

