/* http://keith-wood.name/countdown.html
   Countdown for jQuery v1.4.2.
   Written by Keith Wood (kbwood@virginbroadband.com.au) January 2008.
   Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and 
   MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. 
   Please attribute the author if you use it. */
(function($){function Countdown(){this.regional=[];this.regional['']={labels:['Years','Months','Weeks','Days','Hours','Minutes','Seconds'],labels1:['Year','Month','Week','Day','Hour','Minute','Second'],compactLabels:['y','m','w','d'],timeSeparator:':',isRTL:false};this._defaults={format:'dHMS',layout:'',compact:false,description:'',expiryUrl:null,alwaysExpire:false,onExpiry:null,onTick:null,serverTime:null};$.extend(this._defaults,this.regional[''])}var q='countdown';var Y=0;var O=1;var W=2;var D=3;var H=4;var M=5;var S=6;$.extend(Countdown.prototype,{markerClassName:'hasCountdown',setDefaults:function(a){this._resetExtraLabels(this._defaults,a);extendRemove(this._defaults,a||{})},_attachCountdown:function(a,b){a=$(a);if(a.is('.'+this.markerClassName)){return}a.addClass(this.markerClassName);if(!a[0].id){a[0].id='cdn'+new Date().getTime()}var c={};c.options=$.extend({},b);c._periods=[0,0,0,0,0,0,0];this._adjustSettings(c);$.data(a[0],q,c);this._updateCountdown(a,c)},_updateCountdown:function(a,b){var c=$(a);b=b||$.data(c[0],q);if(!b){return}c.html(this._generateHTML(b));c[(this._get(b,'isRTL')?'add':'remove')+'Class']('countdown_rtl');var d=this._get(b,'onTick');if(d){d.apply(c[0],[b._hold!='lap'?b._periods:this._calculatePeriods(b,b._show,new Date())])}var e=b._hold!='pause'&&(b._since?b._now.getTime()<=b._since.getTime():b._now.getTime()>=b._until.getTime());if(e){if(b._timer||this._get(b,'alwaysExpire')){var f=this._get(b,'onExpiry');if(f){f.apply(c[0],[])}var g=this._get(b,'expiryUrl');if(g){window.location=g}}b._timer=null}else if(b._hold=='pause'){b._time=null}else{var h=this._get(b,'format');b._timer=setTimeout('$.countdown._updateCountdown("#'+c[0].id+'")',(h.match('s|S')?1:(h.match('m|M')?30:600))*980)}$.data(c[0],q,b)},_changeCountdown:function(a,b){var c=$.data(a,q);if(c){this._resetExtraLabels(c.options,b);extendRemove(c.options,b||{});this._adjustSettings(c);$.data(a,q,c);this._updateCountdown(a,c)}},_resetExtraLabels:function(a,b){var c=false;for(var n in b){if(n.match(/[Ll]abels/)){c=true;break}}if(c){for(var n in a){if(n.match(/[Ll]abels[0-9]/)){a[n]=null}}}},_destroyCountdown:function(a){a=$(a);if(!a.is('.'+this.markerClassName)){return}a.removeClass(this.markerClassName).empty();var b=$.data(a[0],q);if(b._timer){clearTimeout(b._timer)}$.removeData(a[0],q)},_pauseCountdown:function(a){this._hold(a,'pause')},_lapCountdown:function(a){this._hold(a,'lap')},_resumeCountdown:function(a){this._hold(a,null)},_hold:function(a,b){var c=$.data(a,q);if(c){if(c._hold=='pause'&&!b){c._periods=c._savePeriods;var d=(c._since?'-':'+');c[c._since?'_since':'_until']=this._determineTime(d+c._periods[0]+'Y'+d+c._periods[1]+'O'+d+c._periods[2]+'W'+d+c._periods[3]+'D'+d+c._periods[4]+'H'+d+c._periods[5]+'M'+d+c._periods[6]+'S')}c._hold=b;c._savePeriods=(b=='pause'?c._periods:null);$.data(a,q,c);this._updateCountdown(a,c)}},_getTimesCountdown:function(a){var b=$.data(a,q);return(!b?null:(!b._hold?b._periods:this._calculatePeriods(b,b._show,new Date())))},_get:function(a,b){return(a.options[b]!=null?a.options[b]:$.countdown._defaults[b])},_adjustSettings:function(a){var b=new Date();var c=this._get(a,'serverTime');a._offset=(c?c.getTime()-b.getTime():0);a._since=this._get(a,'since');if(a._since){a._since=this._determineTime(a._since,null)}a._until=this._determineTime(this._get(a,'until'),b);a._show=this._determineShow(a)},_determineTime:function(k,l){var m=function(a){var b=new Date();b.setTime(b.getTime()+a*1000);return b};var n=function(a,b){return 32-new Date(a,b,32).getDate()};var o=function(a){var b=new Date();var c=b.getFullYear();var d=b.getMonth();var e=b.getDate();var f=b.getHours();var g=b.getMinutes();var h=b.getSeconds();var i=/([+-]?[0-9]+)\s*(s|S|m|M|h|H|d|D|w|W|o|O|y|Y)?/g;var j=i.exec(a);while(j){switch(j[2]||'s'){case's':case'S':h+=parseInt(j[1]);break;case'm':case'M':g+=parseInt(j[1]);break;case'h':case'H':f+=parseInt(j[1]);break;case'd':case'D':e+=parseInt(j[1]);break;case'w':case'W':e+=parseInt(j[1])*7;break;case'o':case'O':d+=parseInt(j[1]);e=Math.min(e,n(c,d));break;case'y':case'Y':c+=parseInt(j[1]);e=Math.min(e,n(c,d));break}j=i.exec(a)}b=new Date(c,d,e,f,g,h,0);return b};var p=(k==null?l:(typeof k=='string'?o(k):(typeof k=='number'?m(k):k)));if(p)p.setMilliseconds(0);return p},_generateHTML:function(c){c._periods=periods=(c._hold?c._periods:this._calculatePeriods(c,c._show,new Date()));var d=false;var e=0;for(var f=0;f<c._show.length;f++){d|=(c._show[f]=='?'&&periods[f]>0);c._show[f]=(c._show[f]=='?'&&!d?null:c._show[f]);e+=(c._show[f]?1:0)}var g=this._get(c,'compact');var h=this._get(c,'layout');var i=(g?this._get(c,'compactLabels'):this._get(c,'labels'));var j=this._get(c,'timeSeparator');var k=this._get(c,'description')||'';var l=function(a){return(a<10?'0':'')+a};var m=function(a){var b=$.countdown._get(c,'compactLabels'+periods[a]);return(c._show[a]?periods[a]+(b?b[a]:i[a])+' ':'')};var n=function(a){var b=$.countdown._get(c,'labels'+periods[a]);return(c._show[a]?'<div class="countdown_section"><span class="countdown_amount">'+periods[a]+'</span><br/>'+(b?b[a]:i[a])+'</div>':'')};return(h?this._buildLayout(c,h,g):((g?'<div class="countdown_row countdown_amount'+(c._hold?' countdown_holding':'')+'">'+m(Y)+m(O)+m(W)+m(D)+(c._show[H]?l(periods[H]):'')+(c._show[M]?(c._show[H]?j:'')+l(periods[M]):'')+(c._show[S]?(c._show[H]||c._show[M]?j:'')+l(periods[S]):''):'<div class="countdown_row countdown_show'+e+(c._hold?' countdown_holding':'')+'">'+n(Y)+n(O)+n(W)+n(D)+n(H)+n(M)+n(S))+'</div>'+(k?'<div class="countdown_row countdown_descr">'+k+'</div>':'')))},_buildLayout:function(f,g,h){var i=(h?this._get(f,'compactLabels'):this._get(f,'labels'));var j=g;var k=function(a,b){var c=new RegExp('%'+a+'.*%'+a);var d=new RegExp('%'+a+'.*');while(true){var e=c.exec(j);if(!e){break}e[0]=e[0].substr(0,2)+e[0].substr(2).replace(d,'%'+a);j=j.replace(e[0],f._show[b]?l(e[0],a,b):'')}};var l=function(a,b,c){var d=$.countdown._get(f,(h?'compactLabels':'labels')+f._periods[c]);return a.substr(2,a.length-4).replace(/%nn/g,(f._periods[c]<10?'0':'')+f._periods[c]).replace(/%n/g,f._periods[c]).replace(/%l/g,(d?d[c]:i[c]))};k('Y',Y);k('O',O);k('W',W);k('D',D);k('H',H);k('M',M);k('S',S);return j},_determineShow:function(a){var b=this._get(a,'format');var c=[];c[Y]=(b.match('y')?'?':(b.match('Y')?'!':null));c[O]=(b.match('o')?'?':(b.match('O')?'!':null));c[W]=(b.match('w')?'?':(b.match('W')?'!':null));c[D]=(b.match('d')?'?':(b.match('D')?'!':null));c[H]=(b.match('h')?'?':(b.match('H')?'!':null));c[M]=(b.match('m')?'?':(b.match('M')?'!':null));c[S]=(b.match('s')?'?':(b.match('S')?'!':null));return c},_calculatePeriods:function(c,d,e){c._now=e;c._now.setMilliseconds(0);var f=new Date(c._now.getTime());if(c._since&&e.getTime()<c._since.getTime()){c._now=e=f}else if(c._since){e=c._since}else{f.setTime(c._until.getTime());if(e.getTime()>c._until.getTime()){c._now=e=f}}f.setTime(f.getTime()-c._offset);var g=[0,0,0,0,0,0,0];if(d[Y]||d[O]){var h=Math.max(0,(f.getFullYear()-e.getFullYear())*12+f.getMonth()-e.getMonth()+(f.getDate()<e.getDate()?-1:0));g[Y]=(d[Y]?Math.floor(h/12):0);g[O]=(d[O]?h-g[Y]*12:0);if(c._since){f.setFullYear(f.getFullYear()-g[Y]);f.setMonth(f.getMonth()-g[O])}else{e=new Date(e.getTime());e.setFullYear(e.getFullYear()+g[Y]);e.setMonth(e.getMonth()+g[O])}}var i=Math.floor((f.getTime()-e.getTime())/1000);var j=function(a,b){g[a]=(d[a]?Math.floor(i/b):0);i-=g[a]*b};j(W,604800);j(D,86400);j(H,3600);j(M,60);j(S,1);return g}});function extendRemove(a,b){$.extend(a,b);for(var c in b){if(b[c]==null){a[c]=null}}return a}$.fn.countdown=function(a){var b=Array.prototype.slice.call(arguments,1);if(a=='getTimes'){return $.countdown['_'+a+'Countdown'].apply($.countdown,[this[0]].concat(b))}return this.each(function(){if(typeof a=='string'){$.countdown['_'+a+'Countdown'].apply($.countdown,[this].concat(b))}else{$.countdown._attachCountdown(this,a)}})};$.countdown=new Countdown()})(jQuery);


/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};




var whichSurvey = Math.floor(Math.random()* 2 + 1);
var popupStatus = 0;
var leaving = true;

function setLeaving(){
	leaving = false;	
}

function setSurveyCookie(){
	
	$.cookie('survey', whichSurvey, {path: '/', domain: 'fdi.com'});
	$.cookie('taken', 'no', {path: '/', domain: 'fdi.com'});
	$.cookie('pages',0, {path: '/', domain: 'fdi.com'});
	$.cookie('timer',600, {path: '/', domain: 'fdi.com'});
}

function surveyStuff(){
	if($("body").attr("id") == "thanks"){
		$.cookie('taken',"yes", {path: '/', domain: 'fdi.com'});
		surveyType = ($.cookie('survey')*1 == 1) ? "Pre-Survey" : "Post-Survey";
		$("#surveyType").attr("value",surveyType);
		$("#nothanks").click(function(){
			location.href = "http://www.fdi.com";						  
		});
		
	}
	else if($("body").attr("id") == "surveyPop"){
		insertSurveyInvite();
		loadPopup("#popupSurvey");
	}
	else if($.cookie('taken') && ($.cookie('taken') !=  "no")){
		//do nothing	
	}
	 else if(!$.cookie('survey') || $.cookie('survey') == ""){
		setSurveyCookie();
		insertSurveyInvite();
		if($.cookie('survey')*1 == 1) loadPopup("#popupSurvey");
		if($.cookie('survey')*1 == 2) postSurveyFunctions();
		
	}
	
		
	else{
		if($.cookie('survey')*1 == 2 || ($.cookie('survey') == 1 && $.cookie('taken') == "no")){
			insertSurveyInvite();
			if($.cookie('survey')*1 == 1 && $.cookie('taken') == "no") loadPopup("#popupSurvey");
			if($.cookie('survey')*1 == 2 && $.cookie('taken') == "no"){
				
				postSurveyFunctions();
			
			}
		}
	}
	
}

function postSurveyFunctions(){
	/*for(i=0; i<exitArray.length; i++){
		if(location.href.indexOf(exitArray[i]) != -1){
			
			//doesn't freeze the unload
			$(window).unload( function(){
				if($.cookie('taken') == "no"){
					return loadPopup("#popupSurvey");
					return false;
				}
			 });*/
			
			/* //Ugly confirmation box
				//message can be customized to advertize but won't be pretty
				//plus I don't think we can reset a cookie to make the survey not come up if they click no
			$(window).bind ("beforeunload", function(){
				if($.cookie('taken') == "no"){
					loadPopup("#popupSurvey");
				}
			 }); 
		}
	}*/
	
	$(window).unload( function(){
		if($.cookie('taken') == "no" && leaving && 
		  ($("body").attr("id") != "surveyPop" && $("body").attr("id") != "thanks") &&
		   $.cookie('frompop') != "y") {
			var windowWidth = document.documentElement.clientWidth;
			var windowHeight = document.documentElement.clientHeight;
			myTop = (windowHeight/2-280/2);
			myLeft =  (windowWidth/2-515/2);
			windowVars = "height=280,width=515,top=" + myTop + ",left=" + myLeft + ",scrollbars=yes,resizable=yes,menubar=no,location=no,status=no,titlebar=no,toolbar=no";
			var myWindow = window.open("http://www.fdi.com/survey/ask.html", "surveyask", windowVars);
		} else if   ($.cookie('frompop') == "y"){
			$.cookie('frompop',null,{path: '/', domain: 'fdi.com'});
		}
	});
	
	$("a[@href^='http']").attr('target','_self');
	$("a").click(function(){
		if (this.href.indexOf("://") == -1 || this.href.indexOf(window.location.host) > -1){
			leaving = false;
		} else{
			if( $.cookie('taken') == "no" && this.href.indexOf(window.location.host) == -1) {
				loadPopup("#popupSurvey");
				return false;
			}
		}
	});
	
	pages = ($.cookie('pages')*1) + 1;
	$.cookie('pages',pages, {path: '/', domain: 'fdi.com'});
	if(pages >= 20){
		//loadPopup("#popupSurvey");
	}
	
	Mycountdown = "+" + ($.cookie('timer')*1);
	if($.cookie('timer')*1 == 0) {
			//loadPopup("#popupSurvey");
	}
	else {
		$('#timer').countdown({until:  Mycountdown, onTick: updateTimer, onExpiry: loadPopup});
	}
}

function updateTimer(periods){
	time = (periods[5]*60) + periods[6];
	$.cookie('timer',time, {path: '/', domain: 'fdi.com'});
}

function killSurvey(value){
	$.cookie('taken',value, {path: '/', domain: 'fdi.com'});	
}

function insertSurveyInvite(){
	var html = "<div id=\"backgroundPopup\"></div>";
		html += "<div id=\"popupSurvey\" class=\"popupWindow\"><div class=\"popupClose\" id=\"popupSurveyClose\"></div>"
		html += "<p>We would like to invite you to take a brief survey to help us improve your experience on FDI.com. There are only 10 questions and it will take less than 5 minutes to complete.</p>";
		html += "<p>Would you like to participate? </p>";
		html += "<p><a href=\"#\" id=\"yessurvey\">Yes</a>";	
		html += "<br /><a href=\"#\" id=\"nosurvey\">No, thank you. Return to FDI.com</a></p>";
		
		surveyType = ($.cookie('survey')*1 == 1) ? "Pre-Survey" : "Post-Survey";
		
		//html += "<p>(Test Purposes: " + surveyType + ")</p>";
		
		html += "</div>";
		html +="<div id=\"timer\"></div>";
		
		$("body").append(html);
		
		centerPopup("#popupSurvey");
		
		$("#popupSurveyClose").click(function(){
			if($("body").attr('id') == "surveyPop"){
				 window.close();
			 } else {
				 disablePopup();
			}
		});
		//Click out event!
		$("#backgroundPopup").click(function(){
			if($("body").attr('id') == "surveyPop"){
				// window.close();
			 } else {
				 disablePopup();
			}
		});
		$("#nosurvey").click(function(){
			killSurvey("never");
			if($("body").attr('id') == "surveyPop"){
				 window.close();
			 } else {
				 disablePopup();
			}							  
		});
			if($.cookie('survey') == 1){
				$("#yessurvey").attr('href',"http://www.fdi.com/survey/index.php?sid=88975&lang=en");
			} else {
				if($("body").attr('id') == "surveyPop"){
					
					$("#yessurvey").click(function(){
						if(window.opener){
							$.cookie('frompop','y',{path: '/', domain: 'fdi.com'});
							window.opener.location.href = "http://www.fdi.com/survey/index.php?sid=26975&lang=en";
							window.close();
						} 
						else{
							//window.moveTo(0,0);
							window.resizeTo(800,600);
							window.location.href = "http://www.fdi.com/survey/index.php?sid=26975&lang=en";
						}
					 });
				}
				else $("#yessurvey").attr('href',"http://www.fdi.com/survey/index.php?sid=26975&lang=en");
			}
		//Press Escape event!
		$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
			}
		});
}

//loading popup with jQuery magic!
function loadPopup(id){
	if(!id || id=="") id = "#popupSurvey";
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$(id).fadeIn("slow",  function() {
		   if(jQuery.browser.msie){
		   	this.style.removeAttribute('filter');
		   }
		});
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$('#popupSurvey').fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(id){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(id).height();
	var popupWidth = $(id).width();
	//centering
	$(id).css({
		
		//"top": "100px",
		//"left": "100px"
		"top": (windowHeight/2-popupHeight/2) + "px",
		"left": (windowWidth/2-popupWidth/2) + "px"
	});
	
	if($("body").attr("id") == "surveyPop"){
		$(id).css({
			"top": "10px",
			"left": "10px"	
		});
	}
	
	
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight,
		"position": "absolute"
	});
	
}

var USPage = "http://www.fdi.com/us_home/us_home.html";
var ROWPage = "http://www.fdi.com/world_home/world_home.html"

$(document).ready(function () {
							
	if($("body").hasClass("disclosurePage")){
			var whichCook = ($("body").attr('id') == "us_home") ? 'disclosureUS' : 'disclosureROW';
			var whichPage = ($("body").attr('id') == "us_home") ? USPage : ROWPage;
			
			if($.cookie(whichCook) && $.cookie(whichCook) == "yes"){
				window.location = whichPage;								 
			}
			
			$("#acceptCookie").bind("click", function(e){
				e.preventDefault();
				$.cookie(whichCook, "yes", {path: '/', domain: 'fdi.com'});
				window.location = whichPage;
			});
		}
		
		if($("#rowTopLink").length != 0){
			if($.cookie('disclosureROW') && $.cookie('disclosureROW') == "yes"){
				$("#rowTopLink").attr('href',ROWPage);						 
			}
		}
		if($("#usTopLink").length != 0){
			if($.cookie('disclosureUS') && $.cookie('disclosureUS') == "yes"){
				$("#usTopLink").attr('href',USPage);						 
			}	
		}
   
   surveyStuff();
});