$(document).ready(function(){
$("#nav li, #secondaryNav li").hover(
    function() { 
        $(this).addClass("over"); 
    }, 
    function() { 
        $(this).removeClass("over"); 
    });

$("#nav li ul li").hover(
    function() { 
        $(this).addClass("sub-over"); 
    }, 
    function() { 
        $(this).removeClass("sub-over"); 
    });
});

function redirectme(ref) {
	//http://www.elsevierhealth.com:8080/?profileid=142500006&isbn=9780323036672&lid=EHS_MEA_BS-DIS-1&sid=0&orderId=o151100004&destParam=eu
	var url = document.URL;
	
	//parsing ref		
	var x = ref.indexOf("?");
	var s1 = ref.substring(0,x-1)
	var s2 = ref.substring(x,ref.length)

	var y = parseUri404(url);
	//alert(y.path)

	//alert("s1 : " + s1)
	//alert("S2 :" + s2)
	//alert(s1 + y.path + s2)
	//alert(x);
	var index = ref.indexOf(x.path);
	//alert("index"+index);
	var domain=ref.substring(0,index);
	//alert(domain);
	if(domain.indexOf("cert5")!=-1){
		domain="http://cert5-www.elsevierhealth.com";
	}
	else if(domain.indexOf("cert1")!=-1){
		domain="http://cert1-www.elsevierhealth.com";
	}
	else if(domain.indexOf("8")!=-1){
		domain="http://www.elsevierhealth.com:8080";
	}
	else{
		domain="http://cert5-www.elsevierhealth.com";
	}
	//alert(domain);	
	var index404 = url.indexOf(x.path);
	//alert(index404);	
//alert(x.path+ref);
//	alert(domain+x.path+ref);
	//window.location.replace(domain+x.path+ref);


var absurl = "1"


                if (ref.indexOf("apac") != -1) {
					 absurl = "2"
				}
				if (ref.indexOf("shop") != -1) {
					absurl = "2"
				}
                if (ref.indexOf(".ca") != -1) {
					absurl = "2"
				}              
				//alert(ref);
                if (absurl != 2) {
					window.location.replace(s1 + y.path + s2)
                }else {
                	window.location.replace(ref)
                }

}
function parseUri404(sourceUri){
	var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
		uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri),
		uri = {};
	for(var i = 0; i < 10; i++){ uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : ""); }
	if(uri.directoryPath.length > 0){ uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/"); }
	return uri;
}


function splitNavs() {
	
	$('ul.split').each(function() {
	 
		var colsize = Math.round($("li", this).size() / 2);
  
			$("li", this).each(function(i) {
				if (i >= colsize) {
					$(this).addClass('right_col');
				}
			});
		
		$(".right_col", this).insertAfter(this).wrapAll("<ul class='splitcol'></ul>").removeClass("right_col");
		$(this).css("border-right", "none");
		
		var lHeight = $(this).parent().find('.splitcol').height();
		var rHeight = $(this).height();
		if(lHeight < rHeight) {
			$(this).parent().find('.splitcol').height(rHeight);
		} else {
			$(this).height(lHeight);
		}
	});
}




$(document).ready(function(){
	splitNavs();
$(".parametric .closed ul").hide();
$(".parametric .hasSubnav").click(function(e) {
	$(this).find("ul").slideToggle("fast");
	$(this).toggleClass("open");
	e.preventDefault();
});
$(".callout .toggleContent").hide();
$(".callout .open").show();
$(".callout .toggle").css("cursor","pointer").click(function() {
	$(this).next().slideToggle("fast");
	$(this).toggleClass("open");
	e.preventDefault();
});
$("#selectDeselect").click(function() {
	this.checked = !(this.checked == true);
	var ischecked = !(this.checked == true);
	$("#resultsFilters input:checkbox").attr("checked", function() {
		this.checked = ischecked;
	});
});
});





// Checkout: shipping address toggle
$(document).ready(function(){
$("#billShip").click(function() {
		if(document.getElementById("billShip").checked)
				 document.getElementById("shippingAddress").style.display='none'
		else
			  document.getElementById("shippingAddress").style.display='block'
});


//Footer: related sites menu
$("#relatedSites h4 a").click(function(e) {
	var menu = $("#relatedSites ul");
	menu.css("top", "-" + menu.height() + "px").toggle("fast");
	
	var myLink = $(this);
	if(myLink.hasClass("close")) {
		myLink.removeClass();
		myLink.text("Related Elsevier Sites")
	} else {
		myLink.addClass("close");	
		myLink.text("Close Menu")
	}
	e.preventDefault();
});

//Cart promo codes
$(".promo").keyup(function() {
	if($(this).val() !== "") {
		$(this).next().addClass("active");
	} else {
		$(this).next().removeClass("active");
	}
});
});

//Checkout: mail order toggle
$("#mailFaxPhoneToggle").click(function(e) {
		
	$("#mailFaxPhoneInfo").slideToggle("normal");
		
	var myLink = $(this);
	if(myLink.hasClass("close")) {
		myLink.removeClass();
		myLink.text("Show information for ordering by mail, phone or fax")
	} else {
		myLink.addClass("close");	
		myLink.text("Hide information for ordering by mail, phone or fax")
	}
		
	e.preventDefault();

});

// Modal popups

$(document).ready(function(){
	$(".colorbox").colorbox();
                       $("a").click(function() {
                       ///alert(this.id);
                                if(this.id=="addToCartX"){
                                var isbn = this.name;
                                var cartxml='<?xml version="1.0" encoding="UTF-16" ?><cartorder><cart></cart><products><product isbn="' + isbn + '"  qty="1" /></products></cartorder>';
                                      var newform = document.createElement("form")
                                                  newform.setAttribute("name", "theForm");
                                                  newform.setAttribute("id", "theForm");
                                      newform.setAttribute("method", "GET");
                                      newform.setAttribute("action", "/purchaseProcessController.jsp");
                
                                      var newinput = document.createElement('input');
                                      newinput.setAttribute('type','hidden');
                                      newinput.setAttribute('name','cartxml');
                                      newinput.setAttribute('id','cartxml');
                                      newinput.setAttribute('value',cartxml);
                                      var srcinput = document.createElement('input');
                                      srcinput.setAttribute('type','hidden');
                                      srcinput.setAttribute('name','source');
                                      srcinput.setAttribute('id','source');
                                      srcinput.setAttribute('value','coArticle');
                                      newform.appendChild(newinput);
                                      newform.appendChild(srcinput);
                                                  document.getElementsByTagName('BODY')[0].appendChild(newform);
                                      document.getElementById('theForm').submit();
                                      }
                       });
     });
     
     $(document).ready(function(){

    $("button").click(function() {

    

             if(this.id=="addToCartBX"){

             var isbn = this.name;

             var cartxml='<?xml version="1.0" encoding="UTF-16" ?><cartorder><cart></cart><products><product isbn="' + isbn + '"  qty="1" /></products></cartorder>';

                   var newform = document.createElement("form")
                               newform.setAttribute("name", "theForm");
                               newform.setAttribute("id", "theForm");
                   newform.setAttribute("method", "GET");
                   newform.setAttribute("action", "/UKNEPurchaseProcessController.jsp");
                   var newinput = document.createElement('input');
                   newinput.setAttribute('type','hidden');
                   newinput.setAttribute('name','cartxml');
                   newinput.setAttribute('id','cartxml');
                   newinput.setAttribute('value',cartxml)
                   newform.appendChild(newinput);
                   document.getElementsByTagName('BODY')[0].appendChild(newform);
                   document.getElementById('theForm').submit();
                   }
    });

	// country select initialization
	$('#masthead').hover(
		function(){
			$('.dropdown',this).show();
			isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
			if(isIE6)
			{
				$('.dropdown',this).css('left','1px');
			}			
		},
		function(){
			$('.dropdown',this).hide();
		}
	);
});


function ScrollPage(id)
{

$('html, body').animate({ scrollTop: $("#"+id).offset().top }, 1500);
}
/**
 * @name conditionalCarousel 
 * 
 * @description functions as a wrapper for jCarousel, 
 * only initializing jCarousel when the # of items in the list
 * is greater than the number of items set to be shown
 * 
 * @requires	jQuery, jCarousel 
 * @param obj	string identifier of DOM id to be used for jCarousel
 * @param options	JSON object to be passed to jCarousel, 
 * 					also for comparison of max display items
 * @return void
 */
function conditionalCarousel(obj,options)
{
	isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
	if(isIE6)
	{
		
	}
	else{
		if($(obj) && options.visible)
		{
			// number of items in carousel object
			var ob_len = $(obj).children('li').length;
			// number of items desired to be shown in carousel 
			var ob_vis = options.visible;
			if(ob_len > ob_vis)
			{
				// initialize carousel
				$(obj).jcarousel(options);
			}else{
				// no operation at this time, do not initialize jcarousel
			}
		}else{
			alert('the carousel DOM object and the options object need to be explicitly defined.');
		}
	}
}
