// JavaScript Document
// firebug degradiation
if (! ("console" in window) || !("firebug" in console)) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group"
                 , "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.console = {};
    for (var i = 0; i <names.length; ++i) window.console[names[i]] = function() {};
}

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{

  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function WebSearch(){
			document.location = 'http://tupperwarebrands.bgtpartners.com/search.html?keywords='+$('#search').attr('value');		
}

$(document).ready(function(){
	$('#search, #search_btn').keypress(function(e){
		if(e.which==13){ WebSearch();	}
	});
	$('#search').focus(function(){
	   if($(this).val().toUpperCase()=='SEARCH') $(this).val('');
	}).blur(function(){
	   if($(this).val()=='' || $(this).val().toUpperCase()=='SEARCH') $(this).val('SEARCH');
	});
	$('#search_btn').click(function(){WebSearch();});
	$('.print-this-content').click(function (){
		if($(this).attr("href").indexOf("sustainibility_qa.html") > -1) {
			popUpWindow($(this).attr("href"),0,0,850,700);
			return false;
		}
		else {
			popUpWindow(window.location + '?print=true',0,0,850,700);
		}
	});
	
	var pageName = new String(window.location);	
	
	var params = pageName.toLowerCase();
	if( params.indexOf('?')!=-1 && params.indexOf('print') ){
		var url = params.substring(0, params.indexOf('?'));	
		params = params.substring(params.indexOf('?')+1);		
		params = params.substring(params.indexOf('print'));
		params = params.split('&');
		if( params[0].indexOf('=')!=-1){
			var values = params[0].split('=');
			if( values[1]=='true'){
				/*
				var oLink = document.createElement('link');
				oLink.setAttribute('href','/common/css/print.css');
				oLink.setAttribute('rel','stylesheet');
				oLink.setAttribute('type','text/css');
				$('html head').append(oLink);*/
				$('body').css({background:"#fff",padding: "0px 10px" });
				$('#container').css({width:"auto",margin:"0px"});
				$('.main_bg_internal form').css('background','none');
				$('#logo_img').css('margin-top','10px')
							  .attr({ src:'/common/images/tupperware_brands_print_logo.gif', alt: "Tupperware Brands", title: "Tupperware Brands" });
				if(pageName.indexOf("qa.html") > -1) {
				$("#breadcrumb").css("display", "none");	
				}
				else {
					$('#breadcrumb').html("<strong>URL: </strong><span>" + url + "</span>");
				}
				$('#branding .right, .mainTbl tfoot, #content-title, .mainTbl #col-left, #content-title, .print-this-content').css("display", "none");
				$("#content-main, #breadcrumb").css("position", "static");
				$("#col-body").css("overflow", "visible");
				$(".left, .right, #col-content-wide").css("float", "none");
				$("#content-tbl #col-content-wide p").css({"display": "block"});
				
				window.print();
			}
		}
	}
	
	pageName = pageName.substr(pageName.lastIndexOf("/")+1);
	if(pageName.indexOf("#") > -1)
	 pageName = pageName.replace("#",'');
	//console.debug("%s", pageName);

	$("#nav-main li:first-child, #lhn li:first-child").addClass("first-child");
	$("#nav-main li:last-child, #lhn li:last-child, #nav-footer ul li:last").addClass("last-child");
	
	$("#lhn > li:first").addClass("first-child");
	$("#lhn > li").addClass("topLevel");
	
	$("#lhn a[href$='"+pageName+"']").each( function () {
		//$(this).parents("li").addClass("selected"); 
		$(this).parents("li");
	});
	
	$("#lhn li").each( function() { 
    	if($(this).text().length > 25 && $(this).text().indexOf("Foundation") > -1)
      		$(this).removeClass("last-child").addClass("last-child-tall");
	});
	
	$(".opportunityForm .table-row div:first-child").addClass("first-child");
	$(".opportunityForm .table-row div:last-child").addClass("last-child");
	$(".opportunityForm .table-row div:only-child").removeClass("first-child").removeClass("last-child").addClass("only-child");
	
	
	if(pageName == "tupperware.html") {
		$("#col-content-wide .section-divider").css("width", "100%").removeClass("section-divider").removeAttr("style");	
	}
	else if(pageName == "tw-international.html") {
		$("#lhn a[href$='contact.html']").parent().addClass("selected");	
	}
	

	$(".contact .table-row .table-col").equalizeCols();
	
	$(".landing-link-group .table-row .table-col").equalizeCols();
	$(".view-sites-holder").insertAfter("#breadcrumb");
	
	$(".view-sites-holder").siblings("#breadcrumb").css("marginBottom", 0);
		
	$("#sites option").each( function(a) {
		if($(this).val().search(pageName) > -1 ) {
			$("#sites")[0].selectedIndex = a;
			return false;
		}				 
	});
	

	$('#sites, #view-sites, .dropdownlist').selectbox();
	

	$(".foundation .arrow p").hide();
	$(".foundation .arrow li").addClass("closed");
	$(".foundation .arrow a").toggle(
		function() {
			$(this).parent().removeClass("closed");
			$(this).siblings("p").slideToggle("slow", function() { 
				$(this).parent().addClass("open");				
			});
			
		},
		function() {
			$(this).siblings("p").slideToggle("slow", function () { 
				$(this).parent().removeClass("open").addClass("closed");	
			});
			
		}
	 );

	$("#col-content-wide .vision").each( function() { 
		$(this).click( function(evt) { 
			evt.preventDefault();
			console.debug("%s %s", $(this).attr("class"), $(this).attr("href"));
			var pageUp = popUpWindow($(this).attr("href"), 0, 0, 808, 850);
			return false;
		});
	});

	$("#int-phone-listing tr:even").addClass("even");
	
	$("#int-phone-listing td").hover( 
		function () {
			$(this).parent().addClass("hover");
		},
		function () {
			$(this).parent().removeClass("hover");
		}
	);


	if($.browser.msie ) {
		if($.browser.version == 6) {
		$(".highlight-box p").addClass("clearfix");
		$("#col-content-wide .pageIntro > div").css("float", "left");
		$(".table-3col h3+h4").css("margin-top", "-5px");
		$("ul.dog + p").css("margin-top", "10px");
		$(".pageIntro h1+h2").css({color: "#035E8F", font: "bold 14px/16px Helvetica, Arial, sans-serif", "margin-bottom": "10px",	"margin-top": "-10px"});
		$(".pageIntro > div").css({"float": "left", "width": "601px"}); 
		$("#col-content-wide .pageIntro > div").css("width", "601px");
		$("#col-body .sitemap ul+h2, #col-body .sitemap h2+h2").css("margin-top","16px");
		$("#col-body .sitemap .table-col li > a").css("color", "#136DC3");
		$("#col-body .sitemap .table-col li > a[href^='http']").css({"color": "#A7A3D6", "font-style": "italic"});
		$(".colImages .imgleft + p").css("margin-left", "162px;");
		}
		$("ol.qa li").slice(9).find("h3").css("padding-left", "8px");
	}	


});

function validate_form()
{

		var errMessage = "";

		$(".opportunityForm .required").each( function () {
			
			if($(this).val()=="") {
				errMessage += "<li>" + $(this).prev().text().replace("*", ''); + "</li>";
				$(this).parent().addClass("error");
			}
			else {
				$(this).parent().removeClass("error");
			}
		});

		
		if(errMessage!="")
		{
			errMessage = "<p>The following fields are missing:</p><ul>" + errMessage + "</ul>";
			$("#lblerrmsg").html(errMessage);
			return false;
		}
		else
			return true;                                                                                                                        
}


/* =jQuery Plugins*/
(function($) {
/* =plug-in : equal-heights */
  
	$.fn.equalizeCols = function() {
        var height = 0,
			reset = $.browser.msie ? "1%" : "auto";

        return this
			.css("height", reset)
			.each(function() {
			    height = Math.max(height, $(this).outerHeight(true));
			})
			.css("height", height)
			.each(function() {
			    var h = $(this).outerHeight(true);
			    if (h > height) {
			        $(this).css("height", height - (h - height));
			    };
			});

    };
	
})(jQuery);