/**
* BNP JS
*
* @author DMA
* @date 2010-02-06
* @version 1.0
* @dependencies JQUERY 1.3.2, JQUERY TOOLS 1.1.2, JQUERY COOKIE
*/

// set the namespacing
BNP=(typeof(BNP)==="undefined")?{}:BNP;

// tabs
BNP.tabs=function() {
	$("ul.tabs").tabs("div.panes > div", {event:'mouseover'});
};

// menu to deploy
BNP.menu=function() {
	// expand menu navigation left
	$(function(){
		$(".list_to_deploy li span").toggle(function() {
			$(this).nextAll('ul').slideToggle('fast');
			$(this).addClass("open");
		},function(){
			$(this).nextAll('ul').slideUp('fast');
			$(this).removeClass("open");
		});
	});
};

// open segment navigation
BNP.segment=function() {
	$("#segment p").toggle(function() {
		$("#segment div").slideDown();
	}, function() {
		$("#segment div").slideUp();
	});
};

// social bookmark
BNP.social=(function() {
	// show bookmark tool on click icon bookmark
	var showBookmark=function() {
		$("#book").toggle(function() {
			$("#mod_bookmark").slideDown('slow');
		}, function() {
			$("#mod_bookmark").slideUp('slow');
		});
	};
	
	// open the print panel
	var printWin=function(){
		$("#print").bind('click', function() {
			window.print();
			return false;
		});
	};
	
	// populate url of the social bookmark
	var populate=function() {
		var title = encodeURIComponent(document.title);
		var url = encodeURIComponent(document.URL);
		$("#del a").attr( "href", "http://del.icio.us/post?url="+url+"&amp;title="+title);
		$("#go a").attr( "href", "http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=&amp;title="+title);
		$("#digg a").attr( "href", "http://digg.com/submit?phase=2&amp;url="+url+"&amp;title="+title);
		$("#stumble a").attr("href", "http://www.stumbleupon.com/submit?url="+url+"&amp;title="+title);
		$("#link_input").val(location.href);
	};
	
	// launch the bookmark panel
	var bookmark=function() {
		if(window.opera) {  
	        if ($("a.jqbookmark").attr("rel") != ""){ 
	            $("a.jqbookmark").attr("rel","sidebar");  
	        }  
	    }
		$("#book_site a").click(function(event){  
	        event.preventDefault(); // prevent the anchor tag from sending the user off to the link  
	        var url = this.href;  
	        var title = document.title; 
	        if (window.sidebar) { // Mozilla Firefox Bookmark  
	            window.sidebar.addPanel(title, url,"");  
	        } else if( window.external ) { // IE Favorite  
	            window.external.AddFavorite( url, title);  
	        } else if(window.opera) { // Opera 7+  
	            return false; // do nothing - the rel="sidebar" should do the trick  
	        } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)  
	             alert('Unfortunately, this browser does not support the requested action,'+' please bookmark this page manually.');  
	        }
	    });
	};
	
	var init=function(){
		printWin();
		showBookmark();
		populate();
		bookmark();
	};
	return {init:init};
})();

// online service tools
BNP.onlineService=(function() {
	// read cookie to display only the selected boxes
	var cookieState=function() {
		if ( $.cookie("online_service_hide") !== null ){
			var pro_access_div = $.cookie("online_service_hide");
			var value_cookie = pro_access_div.split('|');
			var value="";
			$(value_cookie).each(function () {
				if(this!=""){
					value="."+this;
					$(value).hide();
					value_checkbox="#"+this;
					$(value_checkbox).attr('checked', false);
				 }
			 });
		}
	};
	
	var tips=function() {
		$("#next").click(function() {
			$("#tip1, #arrow_tip_1").hide();
			$("#tip2, #arrow_tip_2").show();
			return false;
		});
		$("#prev").click(function() {
			$("#tip2, #arrow_tip_2").hide();
			$("#tip1, #arrow_tip_1").show();
			return false;
		});
		$("#btn_tips").click(function() {
			$(".tips").fadeOut('slow');
			$("#call_tips").fadeIn('slow');
			$.cookie('tips', 'hide',{ expires: 365, path: '/'});
			return false;
		});
		$("#call_tips").click(function() {
			$(this).fadeOut('slow');
			$(".tips").fadeIn('slow');
			$.cookie('tips', '', { expires: 365, path: '/'});
			return false;
		});
		var tips = $.cookie('tips');
		if (tips == 'hide') {
			$(".tips").css("display","none");
			$("#call_tips").show();
		}
	};
	
	var setBoxOrder=function() {

		var editOrder=(function() {
			if($.cookie('edit_order')){
				var cook = $.cookie('edit_order');
			    var sort = cook.split(";");
			    var len = sort.length;
			    for (i=0;i<len;i++) {
					upc(sort[i]);
					upc(sort[i]);
				}
			}
		})();

		function upc(did){
			var curr = $("#"+did).attr('id');
			var prev = $("#"+did).prev().attr('id');
			if(prev){
				$("#"+prev).insertAfter("#"+curr);
			}
		}
		
	
		var up=(function() {
			$(".up").bind('click', function() {
				if ( $(this).parents(".section").hasClass("ilink")) {
					var obj=$(this).parents(".wrapper_elink").attr('id');
				} else {
					obj=$(this).parents(".section").attr('id');
				}
				var curr = $("#"+obj).attr('id');
				var prev = $("#"+obj).prev().attr('id');
				if(prev){
					$("#"+prev).insertAfter("#"+curr);
					writeCookie();
					return false;
				}
			});
		})();

		var down=(function() {
			$(".down").bind('click', function() {
				if ($(this).parents(".section").hasClass("ilink")) {
					var obj=$(this).parents(".wrapper_elink").attr('id');
				} else {
					obj=$(this).parents(".section").attr('id');
				}
				var curr=$("#"+obj).attr('id'),
				next=$("#"+obj).next().attr('id');
				if(next){
					$("#"+next).insertBefore("#"+curr);
					writeCookie();
					return false;
				}
			});
		})();
		
		var writeCookie=function() {
			var box=$("#service_edit").attr('id'),
			boxrows=$("#service_edit").children(".section:visible").attr('id'),
			order="";
			$('#service_edit').children(".section:visible").each(
				function(){
					var childid=$(this).attr('id');
					order=childid + ";" + order;
			    }
			);
			var storder=order.substring(0, order.length-1);
			$.cookie('edit_order', storder, { expires: 365, path: '/'});
		};
	};
	
	var switchDomInt=function() {
		$("#display_int a, #inner_display_int a").bind('click', function() {
			$(".local_ilink_security").hide();
			$(".int_ilink_security").show();
			if ( $(".int_ilink_security iframe").length > 0 ) {
				var f_int = $(".int_ilink_security iframe").get(0);
				f_int.src = f_int.src;
			}
			$.cookie('loc_int', 'int', { expires: 365, path: '/'});
			return false;
		});
		$("#display_local a, #inner_display_loc a").bind('click', function() {
			$(".int_ilink_security").hide();
			$(".local_ilink_security").show();
			if ( $(".local_ilink_security iframe").length > 0 ) {
				var f_loc = $(".local_ilink_security iframe").get(0);
				f_loc.src = f_loc.src;
			}
			$.cookie('loc_int', 'loc', { expires: 365, path: '/'});
			return false;
		});
		// display int or local with the cookie
		if ( $.cookie("loc_int") !== null ){
			var loc_int = $.cookie('loc_int');
			if ( loc_int == 'int' ){
				$(".local_ilink_security").hide();
				$(".int_ilink_security").show();
				$("input[value='loc']").attr('checked', 'false');
				$("input[value='int']").attr('checked', 'true');
			}
			else {
				$(".int_ilink_security").hide();
				$(".local_ilink_security").show();
				$("input[value='loc']").attr('checked','true');
				$("input[value='int']").attr('checked','false');
			}
		}
	};
	
	// launch edit mode
	var getEdit=function() {
		$("#setting").bind('click', function() {
			var offset = $("#box_online").offset(),
			top = offset.top,
			left = offset.left,
			bodyHeight = $("body").height();
			$("#modal").css({height:bodyHeight}).show();
			$("#box_online_edit").css({top:top, left:left}).show();
			// give attribute to checkbox and class to label
			$("#box_online_edit input:not(:checked)").each(function() {
				$(this).next("label").addClass("disabled");
			});
			return false;
		});
	};
	
	var enableEdit=function() {
		// action when you click on checkbox
		$("#box_online_edit :checkbox").click(function() {
			if ( $(this).next("label").hasClass("disabled") ) {
				$(this).next("label").removeClass('disabled');
			} else {
				$(this).next("label").addClass('disabled');
			}
		});
		// action when you click on the box title
		$("#box_online_edit .title :checkbox").click(function() {
			//alert("toto");
			if ($(this).next("label").hasClass("disabled")){
				$(this).parents(".section").find('label').addClass('disabled');
				$(this).parents(".section").find('input').not('.title input').attr("disabled", true); 
			} else {
				$(this).parents(".section").find('label').removeClass('disabled');
				$(this).parents(".section").find('input').not('.title input').removeAttr("disabled");
				 
			}
		});
		
		// action when you click on radio
		$("input[name='loc_int']").click(function() {
			$(this).next("label").removeClass('disabled');
			$("input[name='loc_int']:not(:checked)").next("label").addClass('disabled');
		});
	};
	
	var saveEdit=function() {
		$("#box_online_edit .btn_form_active").bind("click",function(){
			// collect the id to hide the correspondant class 
			var bloc_pro_access="";
			$("#box_online_edit :checkbox").each(function(){
				//looping through checkboxes
				var id=$(this).attr("id");
				if($(this).get(0).checked){
					$("."+id).show();
					bloc_pro_access.replace(id+"|", "");
				} else {
					$("."+id).hide();
					bloc_pro_access+=id+"|";	
				}
				$.cookie('online_service_hide', bloc_pro_access, { expires: 365, path: '/'});
			});
			// remember loc int elink choice
			var valueRadio = $("input[name='loc_int']:checked").val();
			if ($("input[name='loc_int']:checked").val() == 'loc') {
				$(".int_ilink_security").hide();
				$(".local_ilink_security").show();
			} else {
				$(".local_ilink_security").hide();
				$(".int_ilink_security").show();
			}
			$.cookie('loc_int', valueRadio, { expires: 365, path: '/'});
			
			
			$("#modal, #box_online_edit ").hide();
			return false;
		});
	};
	
	var resetEdit=function() {
		// action when you click on reset setting
		$("#reset").bind('click', function() {
			$("#box_online_edit input:checkbox").attr('checked','true');
			$("#loc").attr('checked','true');
			$("#box_online_edit label").removeClass('disabled');
			$("#int").parents("label").addClass('disabled');
			return false;
		});
	};
	
	var closeEdit=function() {
		$("#setting_edit,#cancel_iframe").bind('click', function() {
			$("#modal, #box_online_edit").hide();
			return false;
		});
	};
	
	var init=function(){
		cookieState();
		tips();
		setBoxOrder();
		switchDomInt();
		getEdit();
		enableEdit();
		saveEdit();
		resetEdit();
		closeEdit();
	};
	return {init:init};
})();

//Functions for sitestat softclick

//Softclick function as from sitestat document
function ns_onclick(i, b, c, h, j) { var e = ""; if (typeof ns_pixelUrl == "string") { e = ns_pixelUrl.substring(0, ns_pixelUrl.indexOf("?") + 1) } e += c; e += "&ns_type=" + h + "&ns_action=view"; e += "&ns__t=" + (new Date()).getTime(); if (!b) { b = i.href } var d = document.referrer; if (d.lastIndexOf("/") == d.length - 1) { d = d.substring(d.lastIndexOf("/"), 0) } if (d.length > 0) { e += "&amp;ns_referrer=" + escape(d) } j = j || ""; var f = (i && i.target && i.target != "") ? (i.target.substring(0, 1) == "_") ? i.target.substring(1) : i.target : "self"; var g = new Image(); if (f && b) { if (window[f]) { window.ns_softclick_timer = function(k, a) { return function() { if (window.ns_softclick_timeout) { window.clearTimeout(window.ns_softclick_timeout) } g.onload = g.onerror = function() { return }; window[((window[k]) ? k : "self")].location.href = a } } (f, b); ns_softclick_timeout = window.setTimeout("ns_softclick_timer()", 5000); g.onload = g.onerror = window.ns_softclick_timer } else { window.open(b, f, j) } } g.src = e;return false; }
//function ns_onclick(i, b, c, h, j) { alert(i + " - " + c + " - "); } 

BNP.documentLinksToSoftClick = function() {
    $("a[href$='.pdf'],a[href$='.doc'],a[href$='.docx'],a[href$='.xls'],a[href$='.xlsx'],a[href$='.rtf']").bind('click', function() {
    ns_onclick($(this)[0], '', $(this).attr("href"), 'pdf'); return false;
    });
};

// launch functions on dom ready
$(document).ready(function() {
	BNP.tabs();
	BNP.menu();
	BNP.segment();
	BNP.documentLinksToSoftClick();
	BNP.social.init();
});

function callCampaignLocator() {
    $("#modal").show();
    $("#wrapper_modal_content").show();
    $("#wrapper_modal_content").load("/02/Modules/Fortis_BusinessLocator_ModuleTemplates/CampaignLocatorRedirection.aspx");
    return false;
}
