$.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="/blank.htm" 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()
}




//Commented Pe-Recent Cookie as it is no more required
//$.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");
		})
}

//Mouse out and Mouse Over events of menu are controlled by this script
function toggleMenu(element, action) {    

    if (typeof ClickTaleExec == "function") {
        var elId = element.id;        
        if (elId) {
            ClickTaleExec("toggleMenu(document.getElementById('" + elId + "')," + action + ")");
        }
    }

    if (action == "MouseOver") {
        if (!$(element).hasClass("currTab"))
            $(element).addClass("active");
        if ($(element).children("div.mn-sub1").length > 0) {
            // this opens a first level nav
            $(element).children("div.mn-sub1").show();
            if ($.browser.msie && ($.browser.version == "6.0"))
                $("div.mn-sub1").bbgiframe({ opacity: true });
        }

        if ($(element).children("div.mn-sub2").length > 0) {            
            //add MSF feature here by Hugh
            var ind1 = 0, ind2 = 0, msf = ind1 - ind2;
            ind1 = $(element).parent().children().index($(element)) + 1;
            if ($(element).find("div.mn-sub2 ul li").length > 15) {
                $(element).children("div.mn-sub2").addClass("twoCol");

                if ($(element).find("div.mn-sub2 ul li").length % 2 > 0) {
                    ind2 = ($(element).find("div.mn-sub2 ul li").length + 1) / 2;
                }
                else {
                    ind2 = $(element).find("div.mn-sub2 ul li").length / 2;
                }

                if ($(element).find("div.mn-sub2 ul").length < 2) {
                    $("<ul></ul>").append($(element).find("div.mn-sub2 ul li:gt(" + (ind2 - 1) + ")")).appendTo($(element).find("div.mn-sub2"));
                }
            }
            else {
                ind2 = $(element).find("div.mn-sub2 ul li").length;
            }
            msf = ind1 - ind2;
            if (msf > 0) {
                $(element).children("div.mn-sub2").height(element.offsetTop + $(element).height());
            }
            // this opens a second level nav
            $(element).children("div.mn-sub2").show();
            if ($.browser.msie && ($.browser.version == "6.0"))
                $("div.mn-sub2").bbgiframe({ opacity: true });
        }
    }

    else {
        if (!$(element).hasClass("currTab"))
            $(element).removeClass("active");
        if ($(element).children("div.mn-sub1").length > 0) {
            // this closes first level navs
            $(element).children("div.mn-sub1").hide();
        }

        if ($(element).children("div.mn-sub2").length > 0) {
            // this closes second level navs
            $(element).children("div.mn-sub2").hide();
        }
    }
}

function mouseover_topnav() {    
    toggleMenu(this, "MouseOver");
}

function mouseout_topnav() {
    toggleMenu(this, "MouseOut");
}

/*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(window.location.host + '/default\.xhtml')) {
                $(this).parent().addClass('first');
                $(this).html('&nbsp;')
            }
        }
    });

    $(".scTab a").click(function(e) {
        e.preventDefault();
        if (!$("#shoppingCart").hasClass("open")) {
            $(this).openCart();
        } else {
            $(this).closeCart();
        }
    })

    $(".scTabNonEcom 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", "#333");
            }
        })
		.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
    */
    //Commented Pe-Recent Cookie as it is no more required
    //	$("#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
    });

    /* Formatting of  all <table id=ChartA> */
    $('table#ChartA tbody tr:nth-child(even)').addClass('alt');
    $('table#ChartA tbody tr td:last-child').addClass('lastColumn');
    $('table#ChartA tbody tr:last-child th').addClass('lastRow');
    $('table#ChartA tbody tr:last-child td').addClass('lastRow');
    $('table#ChartA tbody tr:last-child td:last').removeClass('lastRow').addClass('lastRow lastColumn');

    /* Formatting of  all <table id=ChartB> */
    $('table#ChartB tbody tr:nth-child(even)').addClass('alt');
    $('table#ChartB tbody tr td:last-child').addClass('lastColumn');
    $('table#ChartB tbody tr:last-child th').addClass('lastRow');
    $('table#ChartB tbody tr:last-child td').addClass('lastRow');
    $('table#ChartB tbody tr:last-child td:last').removeClass('lastRow').addClass('lastRow lastColumn');

    /* Formatting of  all <table id=ChartC> */
    $('table#ChartC tbody tr:nth-child(even)').addClass('alt');
    $('table#ChartC tbody tr td:last-child').addClass('lastColumn');
    $('table#ChartC tbody tr:last-child th').addClass('lastRow');
    $('table#ChartC tbody tr:last-child td').addClass('lastRow');
    $('table#ChartC tbody tr:last-child td:last').removeClass('lastRow').addClass('lastRow lastColumn');

    /* Formatting of bullet list in div.productLit */
    $('div.productLit ul.two-col li:nth-child(even)').addClass('odd');

    /* 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); 
        //}
}

}

}
//**********************************************************************/

