/*
 * Popup jQuery plugin
 * 
 *  Author: Lokesh N 
 *  Dependacies:
 * 		jquery-1.2.6.min.js
 * 		jquery.cookies.2.0.min.js
 * 		strings.XX.js
 */

/* This is the method which will render the form with the input of discalimer id and 
 * reding from variable disclaimer+id , where the variable is created by KCI_Link_C/Link template
 */
function centerPopup(id){
	//Create popup
	var textval = eval("disclaimer"+id).text;
	var url = eval("disclaimer"+id).href;
	var popup = eval("disclaimer"+id).popup;
	var html = '<form id="changeCountryConfirm" onsubmit="return false">'
		+ '<a href="#" onclick="jQuery(this).parent().fadeOut(400).remove();return false;" class="close">' + strings.close + '</a>'
		+  textval 
		+ '<div><button class="primaryButtonSmall" onclick="jQuery(this).parent().parent().fadeOut(400).remove(); submitPage(\''+url+'\', \''+popup+'\')"><span>' + strings.Continue + '</span></button>'
		+ '<button class="secondaryButtonSmall" onclick="jQuery(this).parent().parent().fadeOut(400).remove();return false;"><span>' + strings.Cancel + '</span></button></div>'
		+ '</form>';
	$('body').append(html);
							
}

String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}

function submitPage(href,popup){
	
	if(href.startsWith("/")){
		var url = window.location.href;
		var nohttp = url.split('//')[1];
		var hostPort = nohttp.split('/')[0]
		href = "http://"+ hostPort + href;	
	}
	if(popup == "Yes"){
		window.open(href);
	}
	else {
		location.href=href;
	}
}



//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	$("a[name='clickable']").click(function(){
		var $this	 	= $(this);
		var id 			=  $this.attr('id');	//link id
		centerPopup(id);
	});	

});