$.fn.openCart = function(){
	m = $("<div></div>").css({
		height:'100%',
		width:'100%',
		position:'fixed',
		left:0,
		top:0,
		'z-index':999,
		opacity:0.75,
		'background-color':'#fff'
	});
	i=$('<iframe src="javascript:false;document.write(\'\');" class="jqm"></iframe>').css({'background-color':'#fff',opacity:0.75});
	ie6=$.browser.msie&&($.browser.version == "6.0");
	
	if(ie6){
		
		i.prependTo('body');
		//m.appendTo('body');
	}
	else{
		m.prependTo('body');
	}
	
	$("#shoppingCart").css("z-index", "1000").animate({
		top: "0px"
	}, 250, "swing").addClass("open");
	
	/*$("#contentArea").animate({
		opacity: .2
	})*/
	/*$('#mainNav').css("z-index","9");*/
}

$.fn.closeCart = function(){
	
	i.remove();
	m.remove();
	$("#shoppingCart").animate({
		top: "-186px"
	}, 250, "swing", function(){
		$(this).css("z-index", "");
	}).removeClass("open");
	
	/*$("#contentArea").animate({
		opacity: 1
	})*/
	/*$('#mainNav').css("z-index","100");*/
}
	

$.fn.openSearchContext = function(){
	$("#searchContext ul").slideDown(function(){
		$(this).addClass("open")
		$('#mainNav').css("z-index","9");
	})
}
$.fn.closeSearchContext = function(){
	$("#searchContext ul").slideUp(function(){
		$(this).removeClass("open")
		$('#mainNav').css("z-index","100");
	})
}
$.fn.updateSearchContext = function(clicked){
	console.log()
}





$.fn.loadRecents = function(){
	var json = $.evalJSON($.cookie("pe-recent"));
	
	if(json != null){
		var html = new Array;
		for(x=0; x< json.length; x++){
			html.push("<li><p>"+ json[x].c +"<br /><a href=\""+ json[x].l +"\">"+json[x].t+"</a></p></li>");
		}
		
		$("#fPages").html(html.join(""));
	
		//This is for the recently viewed carousel on every page
		$('#fPages').jcarousel({
			buttonPrevHTML: "<div id=\"prevLinks\">Previous</div>",
			buttonNextHTML: "<div id=\"nextLinks\">Previous</div>",
			scroll: 1,
			animation: 1500,
			easing: "swing"
	    });
	} else {
		$('#fPages').hide();
	}
	
}

$.fn.addRecent = function(obj){
	
	var currentRecents = $.evalJSON($.cookie("pe-recent"));
	//this filters the array and tries to remove the current item if the link matches
	
	if(currentRecents != null){
		currentRecents = jQuery.grep(currentRecents, function(value){
			return value.t != obj.t;
		});
		var newItem = new Array;
		newItem.push(obj);
		var json = newItem.concat(currentRecents);
	
	} else {
		var newItem = new Array;
		newItem.push(obj);
		var json = newItem;
	}
	
	$.cookie("pe-recent", $.toJSON(json), { path: '/', expires: 10 });
}

/*For Press Filter*/
$.fn.openFilterContext = function(list) {
		list.slideDown(function(){
			list.addClass("open")
		})
}
$.fn.closeFilterContext = function(listItem) {
		listItem.slideUp(function(){
			listItem.removeClass("open");
		})
}

function mouseover_topnav(){
	if(!$(this).hasClass("currTab"))
		$(this).addClass("active");
	if($(this).children("div.mn-sub1").length > 0){
		// this opens a first level nav
		$(this).children("div.mn-sub1").show();	
		if($.browser.msie&&($.browser.version == "6.0"))
			$("div.mn-sub1").bbgiframe({opacity : true});
	}
	
	if($(this).children("div.mn-sub2").length > 0){
		//# > 15 
//		if($(this).find("div.mn-sub2 ul li").length > 15){
//			$(this).children("div.mn-sub2").addClass("twoCol");
//		}
		//add MSF feature here by Hugh
		var ind1=0,ind2=0,msf=ind1-ind2;
		ind1=$(this).parent().children().index($(this))+1;
		if($(this).find("div.mn-sub2 ul li").length > 15){
			$(this).children("div.mn-sub2").addClass("twoCol");
			
			if($(this).find("div.mn-sub2 ul li").length % 2 > 0){
				ind2=($(this).find("div.mn-sub2 ul li").length+1) / 2;
			}
			else{
				ind2=$(this).find("div.mn-sub2 ul li").length / 2;
			}
			
			if($(this).find("div.mn-sub2 ul").length < 2){
				$("<ul></ul>").append($(this).find("div.mn-sub2 ul li:gt("+(ind2-1)+")")).appendTo($(this).find("div.mn-sub2"));
			}
		}
		else{
			ind2=$(this).find("div.mn-sub2 ul li").length;
		}
		msf=ind1-ind2;
		if(msf>0){
			$(this).children("div.mn-sub2").height(this.offsetTop+$(this).height());
		}
		// this opens a second level nav
		$(this).children("div.mn-sub2").show();
		if($.browser.msie&&($.browser.version == "6.0"))
			$("div.mn-sub2").bbgiframe({opacity : true});
	}
}

function mouseout_topnav(){
	if(!$(this).hasClass("currTab"))
		$(this).removeClass("active");	
	if($(this).children("div.mn-sub1").length > 0){
		// this closes first level navs
		$(this).children("div.mn-sub1").hide();
	}
	
	if($(this).children("div.mn-sub2").length > 0){
		// this closes second level navs
		$(this).children("div.mn-sub2").hide();
	}
}

/*For Press Filter*/

$(document).ready(function(){
	/*HUGH added on Jan 22 for artf1056486*/
	if($(document).height()>$("body").height()){
		$("body").height($(document).height());
	}
	/* to display the popup normally in Firefox*/
	$('#ctl00_ctl00_ctl00_pageContent_cmsContent_default_searchResults_pnlPopup').appendTo('body');
	$('#ctl00_ctl00_ctl00_pageContent_cmsContent_default_searchResults_mdlPopup_backgroundElement').appendTo('body');	
	
	/*
		Event Manager This should just invoke functions, we should not put alot of logic here.
	*/	
	/*Breadcrumb Implementation*/
	$('#breadCrumb1 a').each(function() {
		if ($(this).attr('href')!= null) {
			if ($(this).attr('href').match('/home\.aspx')) {
				$(this).parent().addClass('first');
				$(this).html('&nbsp;')
			}
		}
	});

	$(".scTab a").click(function(e){
	    e.preventDefault();
		if(!$("#shoppingCart").hasClass("open")){
			$(this).openCart();
		} else {
			$(this).closeCart();
		}
	})
	$("#scClose").click(function(){
		$(this).closeCart();
	})
	
	$("#searchContext").click(function(){
		if(!$("#searchContext ul").hasClass("open")){
			$(this).openSearchContext();
		} else {
			$(this).closeSearchContext();
		}
	})
	$("#searchContext label").click(function(){
		$("#searchContext span.searchText").html($(this).text())
	})
	
	$(".searchTerm").each(function(){
		var initStr = $(this).val();
		var initColor = $(this).css("color");
		
		$(this).focus(function(event){
			if($(this).val()==initStr){
				$(this).val("").css("color","#000");
			}
		})
		.blur(function(event){
			if(jQuery.trim($(this).val())==""){
				$(this).val(initStr).css("color",initColor);
			}
		});
	});
	
    //Commented this as per Hugh's request - global nav not working on Google chrome issue 
    //load the recent page links at the bottom of each page
    //$(this).loadRecents()
	
	
	// this checks to see if the element you hovered over has a subnav, then hides or shows it.
	$("#mainNav li").hoverIntent({
		interval: 100,	
		timeout: 100,
		over: mouseover_topnav, 
		out: mouseout_topnav
	})
	
	
	/*KULDIP: Add code for featured and All Promotions Carousel Implementation */
	$('#featuredPromotions').jcarousel({
		buttonPrevHTML: "<div id=\"previousLinks\">Previous</div>",
		buttonNextHTML: "<div id=\"nextsLinks\">next</div>",
		scroll: 1,
		animation: 1300,
		easing: "swing"
	});
	$('.allPromotions').jcarousel({
		buttonPrevHTML: "<div id=\"previousLinks\">Previous</div>",
		buttonNextHTML: "<div id=\"nextsLinks\">next</div>",
		scroll: 1,
		animation: 1300,
		easing: "swing",
		itemLoadCallback:function(hash){
			// for status counter
			var container = $(hash.container.context).get(0);
			var groups = $(container).find("ul")
			var totalItems = $(container).find("li li").length;
			var offset = hash.first - 1;
			var count = $($(groups).get(hash.first - 1)).find("li").length + $($(groups).get(hash.last - 1)).find("li").length;
			var startingAt = (offset * 3)+1;
			var endingAt = startingAt + count - 1;
			
			//report
			$(".paginationText").html("Showing " + startingAt +"-"+ endingAt + " of " + totalItems);
		}
	});
	
/********************BEGIN:[KG]Drop down Press Release filter************************/
	$(".filteredDropdown").each(function(){
		$(this).click(function(){
			//alert($(this).find('ul').length);
			list = $(this).find('ul');
			if(!list.hasClass("open")){
				list.openFilterContext(list);
			} else {
				list.closeFilterContext(list);
			}
		})
		label = $(this).find('label');
		label.each(function(){
			$(this).click(function(){
				$(this).parent().parent().parent().find('span').html($(this).html());
				$(this).parent().parent().parent().find('input').val($(this).html());				
			});
			//$(this).parent().parent().parent().find('span').html($.cookie("currentFilterValue"));
		});
	});
		
	/*Press Release Expand Collapse Functionality*/
	 if ($('.pressContent p').length) {
		$('.pressContent p').hide();
		if($('.pressContent a').hasClass("collapse")) {
			$('.pressContent a.collapse').toggle(function(){
				$(this).addClass("expand");
				}, function () {
				$(this).removeClass("expand");
			});
			$('.pressContent a.collapse').click(function(){
				$(this).parent().find('p').slideToggle("slow");
			});
		}
/********************END:[KG]Drop down Press Release filter************************/	
	}
	/*Press Release Expand Collapse Functionality*/
	/*
	DEVELOPMENT CODE, DELETE BEFORE RELEASE
	*/
	$("#writeCookie").click(function(){
		
		var rand = Math.floor(Math.random()*1111);
		$(this).addRecent({
			t:'Page Title' + rand,
			c:'Category Title' + rand, 
			l:'link' + rand + '.html'
		});
		
		return false;
	})
	
	$("#readCookie").click(function(){
		//alert($.evalJSON($.cookie("pe-recent")))
		return false;
	})
	
	$("#clearCookie").click(function(){
		$.cookie("pe-recent", null, { path: '/', expires: 10 });
		return false;
	})
	
	if($.browser.safari){
		$("select").css({
			"border":"none",
			"border-width":"1px",
			"border-style":"solid"
			});
		$("select.searchText").removeClass("searchText").css({
			"font-size":"12px",
			"width":"150px",
			"cursor":"pointer"
		});
	}
	
	$(".AspNet-TreeView > ul").addClass("menul1");
	$(".AspNet-TreeView-ChildSelected > ul").addClass("menul2");
	
	
	$('.tabControl').each(function(){
		var tabs = $(this).children(".tabHeader").children("ul");
		var tabContent = $(this).children(".tabContent");
		$(tabs).click(function(e){
			
			$(tabs).children("li").removeClass("active");
			$(e.target).parent("li").addClass("active");
			
			$(tabContent).children(".tab").removeClass("active")
			var href = $(e.target).attr("href")
			$(href).addClass("active");
			
			return false;
		})
		
		$(tabs).jcarousel({
			buttonPrevHTML: "<div class=\"scrollLeft\">Left</div>",
			buttonNextHTML: "<div class=\"scrollRight\">Right</div>",
			scroll: 1,
			animation: 1500,
			easing: "swing"
		});
	});

	$("#overlay").appendTo("body");
	$("#flash").appendTo("body");
	$("#ourMissionFlash").appendTo("body");
	
	$("#flash").jqm({
		trigger: '.timeline-trigger',
		overlay:75
	});
	$("#ourMissionFlash").jqm({
		trigger: '.forTheBetter',
		overlay:75
	});

	/* END DEVELOPMENT CODE */
/*	$('#modalDialogCountrySelector').jqm({
		overlay:75,
		modal: true,
		trigger: '#header #topNav #tnLang a',
		
	});
	$('#shoppingCart').jqm({
		trigger: '#header #topNav #tnLang a',
	});
*/})
//***************BEGIN:[RJ]:Related to search************************
function RemoveText(str,id) {
var id = document.getElementById(id);
if(str.toUpperCase() == id.value.toUpperCase())
{
    id.value='';
}
return false;
}
function InsertText(str,id) {
var id = document.getElementById(id);
if(id.value=='')
{
    id.value=str;
}
return false;
}
function KeyDownHandler(btn)
{
    if (event.keyCode == 13)
    {
        event.returnValue = false;
        event.cancel = true;
        btn.click();
    }
}

function CheckKeyCode(objEvent,btnClick) 
{
var v=document.getElementById(btnClick);
var iKeyCode;

var IsValid = false ; 
if(window.event) // IE
{
iKeyCode = objEvent.keyCode
if(iKeyCode==13)
{
        objEvent.returnValue = false;
        objEvent.cancel = true;
        document.getElementById(btnClick).click();
}

}

else if(objEvent.which) // Netscape/Firefox/Opera
{

iKeyCode = objEvent.which
if(iKeyCode==13)
{
        objEvent.returnValue = false;
        objEvent.cancel = true;
        //if(document.getElementById(btnClick).dispatchEvent)
        //{
        
        document.getElementById(btnClick).click();
        //var e = document.createEvent("MouseEvents"); 
        //e.initEvent("click", true, true);
        //document.getElementById(btnClick).dispatchEvent(e); 
        //}
}

}

}
//**********************************************************************/
