﻿	var isInFocus = false;
	var isSearchClicked = false;
	var selectedregion;
		
	var selectedlanguage;
	$(document).ready(function() {

	/* international panel support */
	

// populate from cookie code if 'remember language' checkox is present

	//$("#util_change_country .checkbox input").checked=false;
	try{
	//selectedregion = readCookie('selectedregion');
		//selectedlanguage = readCookie('selectedlanguage');
		//if ((selectedregion == null) || (selectedregion.length == 0) ||
		//(selectedlanguage == null) || (selectedlanguage.length == 0))
		//{
			selectedregion="NorthAmerica";
			selectedlanguage="English";
		//}
		regionChange(selectedregion, false);
	languageChange(selectedlanguage);
		
		}
		catch(err)
		{
		}
	
   // bind region list
	$("#RegionsList li a").click(function ()
	{
		var _id=$(this).attr('id');
		var _idl = _id.length;
		var _id2 = _id.substring(2, _idl);
		regionChange(_id2, true);
	} );

	// bind language list
	$("div.region li a").click(function() {
		$(this).removeClass("current");
		var id= $(this).attr('class');
		languageChange(id);

	});
	
	// bind 'remember language' checkox
	/*$("#util_change_country .checkbox input").click (function()
	{	
		var x = readCookie('selectedregion');
		if ($(this).is(':checked'))
		{
					
			createCookie('selectedregion', selectedregion, 7);
			createCookie('selectedlanguage', selectedlanguage, 7);
		}
	});*/

     // bind change site button
     $("a#btnChangeSites").click (function()
     {
    
    var newLoc= changeSite();
      // return false;
     $("a#btnChangeSites").attr("href", newLoc);

      }
     );

    
	/* end of international panel support */


	//apply shadow
	var shade = $("#top_image .shade div");
	if (shade.html())
	{

	if (shade.html().length != 0)
	$("#top_image .shade").show();
		}

	//initialize the menu
	$(function() {

	$('#menu_bar ul.nav').show();
	if (!($.browser.msie && $.browser.version < 7))
	$('#menu_bar').addClass("sf-shadow");		

	$('#menu_bar ul.nav').css("visibility","hidden").css("display","block");
	$('#menu_bar li.topnav').hover(
	function () {

	$(this).stop().delay(100).queue(function() {$(this).addClass("sfHover").find(".nav").css("visibility","visible");$(this).dequeue});
	
//	$(this).stop().delay(2000).addClass("sfHover").find(".nav").css("visibility","visible");
	
	
	}, 
	function () {
	$(this).stop().removeClass("sfHover").find(".nav").css("visibility","hidden");

	}

	);

	});
	$("#menu_bar a.title").click(function() {return false; });

	// now the search change
	$("#header_search input").focus(function() {
		$(this).parent().addClass("in");
	});
	
	// Handle the normal search textbox on WhereToBuy page in IE
	$("div.without_gsc input").focus(function() {
		isInFocus = true;
		$(this).parent().addClass("in");
		$("#header_search_button").attr("style", "background: url('/images/backgrounds/bg_header_search.gif') no-repeat scroll bottom right transparent !important");
	});

	// Currently this click event is not used
	$("#header_search a").click(function() {

		var strSearch = $("#header_search input").val();
		if (strSearch.length == 0)
		{
			alert("please enter the search term");
			
			$("#header_search input").focus();
		}
		else
		{
			var z = getRootURL()+"Pages/"+"SearchResults.aspx?q=" + strSearch;

			window.location.href =z;
			
			$("#header_search").addClass("in");
			$("input.gsc-input").removeAttr("style");
			$("#header_search_button").attr("style", "background: url('/images/backgrounds/bg_header_search.gif') no-repeat scroll bottom right transparent !important");
		}
		
	});

	/*
	$("#header_search input.gsc-input").live("focus", function() {
		$("#header_search").addClass("in");
		$("input.gsc-input").removeAttr("style");
		$("#header_search_button").attr("style", "background: url('/images/backgrounds/bg_header_search.gif') no-repeat scroll bottom right transparent !important");
	});	
	*/
	
	// Use this event for both the cases (with and without google custom search control)
	$("#header_search_button a").click(function() {
		var z;
		var strSearch = "";
		if(isWhereToBuyPageInIE == false)
		{
			strSearch = $("#header_search input.gsc-input").val();
			
			if (strSearch.length == 0)
			{
				alert("please enter the search term");
				isInFocus = true;
				$("#header_search input.gsc-input").focus();
			}
			else
			{
				z = getRootURL()+"Pages/"+"SearchResults.aspx?q=" + strSearch;
	
				window.location.href =z;
				isSearchClicked = true;
				
				$("#header_search").addClass("in");
				$("input.gsc-input").removeAttr("style");
				$("#header_search_button").attr("style", "background: url('/images/backgrounds/bg_header_search.gif') no-repeat scroll bottom right transparent !important");
			}
		}
		else
		{		
			// Take the value from the normal textbox and not from the google search control textbox
			strSearch = $("div.without_gsc input").val();
			
			if (strSearch.length == 0)
			{
				alert("please enter the search term");
				isInFocus = true;
				$("div.without_gsc input").focus();
			}
			else
			{
				z = getRootURL()+"Pages/"+"SearchResults.aspx?q=" + strSearch;
	
				window.location.href =z;
				isSearchClicked = true;

				$("div.without_gsc input").addClass("in");
				$("#header_search_button").attr("style", "background: url('/images/backgrounds/bg_header_search.gif') no-repeat scroll bottom right transparent !important");
			}
		}

	});

	// IE png fix
	var shimpath = getRootURL()+'images/x.gif';
	$('.content').supersleight({ shim: shimpath });

	// focus highlighting
	var elements = $("input[type!='submit'], textarea, select. checkbox");
	elements.focus(function() {
		$(this)/*.parents('p')*/.addClass('highlight');

	//   $(this).attr("value", "");
		$(this).parents("span.rounded").addClass('input_wrapper_highlight');
		// next two lines are for ie support
		$(this).parents("span.rounded").eq(0).children('div').eq(0).children('div').addClass("highlight");
		$(this).parents("span.rounded").eq(0).children('div').eq(2).children('div').addClass("highlight");
		$(this).parents("span.rounded").removeClass('input_wrapper');
	});
	elements.blur(function() {
		$(this)/*.parents('p')*/.removeClass('highlight');
		$(this).parents("span.rounded").addClass('input_wrapper');
		// next two lines are for ie support
		$(this).parents("span.rounded").eq(0).children('div').eq(0).children('div').removeClass("highlight");
		$(this).parents("span.rounded").eq(0).children('div').eq(2).children('div').removeClass("highlight");
		$(this).parents("span.rounded").removeClass('input_wrapper_highlight');
	});

	// focus highlighting
	var hot_callouts = $(".hot_callout");

	hot_callouts.click(function(e) {

		var href = $(this).find("a").attr("href");
	//   alert(href);
		window.location = href;

	}
	)

	hot_callouts.hover(
	function() {
		$(this).css("cursor", "pointer");

	},
	function() {
	$(this).css("cursor", "default");
	}
	);

	//click for productList
	$(document).bind('click', function(e) {
		var $clicked = $(e.target); // get the element clicked

		if (!($clicked.is('#util_product_list') || $clicked.parents().is('#util_product_list') || $clicked.is('a.lnk_util_products')) && $("#util_product_list").is(':visible')) {
		toggleProducts("a.lnk_util_products");
		}

		if (!($clicked.is('#productdropdown')||$clicked.is('#opendroplink')||$clicked.parents().is('#productdropdownwrap')) && $("#productdropdown").is(':visible')) {
				$('#productdropdown').css("z-index","0");
				$('#productdropdown').hide();
				//$('#opendroplink').removeClass("opened");
				$('#dropdowntitle').removeClass("opened");
				$('productdropdownwrap').removeClass("opened");
				$('#opendroplink').html(selectText);
		}

		if(isWhereToBuyPageInIE == false)
		{
						// Get the control that triggered the event. This is required to know 
						// whether the user clicked one of the the autocomplete search results.
						var clicked_id = $clicked.attr("id");
						if(clicked_id == "")
						{
							clicked_id = $clicked.parents().attr("id");
						}
						//if($clicked.attr("id") == "csc-completion-selected")
						if(clicked_id == "csc-completion-selected")
						//if($clicked.is('#csc-completion-selected') || $clicked.parents().is('#csc-completion-selected'))
						{
							//$("#header_search input").val();
							$("#header_search_button a").click();
							isSearchClicked = true;
						}
						
						if (!($clicked.is('#header_search') || $clicked.parents().is('#header_search'))) {
							if(isSearchClicked == false)
							{
											$('#header_search input').attr("value", "");
											$('#header_search input').parent().removeClass("in");
											// Remove the "in" class if the search box is not in focus
											if(isInFocus == false)
											{
												$('#header_search').removeClass("in");
												$("#header_search_button").attr("style", "background: url('/images/backgrounds/bg_header_search.gif') no-repeat scroll top right transparent !important");
											}
											isInFocus = false;
											$('input.gsc-input').removeAttr("style");
							}
							isSearchClicked = false;
						}
		}
		else
		{
			// Remove the "in" class if the search box is not in focus
			if(isSearchClicked == false)
			{				
				if(isInFocus == false)
				{
					$("div.without_gsc input").attr("value", "");
					$("div.without_gsc input").parent().removeClass("in");
					$("#header_search_button").attr("style", "background: url('/images/backgrounds/bg_header_search.gif') no-repeat scroll top right transparent !important");
				}
				isInFocus = false;
			}
		}
	});

	//links for util hookup
	$("a.lnk_util_country").click(function() { $("#util_country").showInUtil("a.lnk_util_country"); });
	$("a.lnk_util_login").click(function() { $("#util_login").showInUtil("a.lnk_util_login"); });

	$("a.lnk_util_products").click(function() { showProducts("a.lnk_util_products"); });

	//rounded corners
	$('.rounded').corners();


	// initalize function for zoom display
		$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 10, /* padding for each side of the picture */
		opacity: 0.65, /* Value betwee 0 and 1 */
		showTitle: false, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_square', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function() { }
		});

	// initalize function for video display
		$("a[rel^='prettyVideo']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 10, /* padding for each side of the picture */
		opacity: 0.65, /* Value betwee 0 and 1 */
		showTitle: true, /* true/false */
		/*overlayClass: 'pp_overlay_dark',*/
		allowresize: false, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_square', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function() { }
		});

			$("#fabricsframe").dialog({ 
				autoOpen: false,  
				modal: true, 
				width: 830, 
				height: 650,  
				resizable: false , 
				dialogClass: 'color_modal' });

			$("#toolsframe").dialog({ 
				autoOpen: false,  
				modal: true, 
				width: 870, 
				height: 650,  
				resizable: false , 
				dialogClass: 'color_modal' });

			$("#videoframe").dialog({ 
				autoOpen: false,  
				modal: true, 
				width: 520, 
				height: 300,  
				resizable: false , 
				dialogClass: 'color_modal',
				beforeclose: function(event, ui) {$('#videoframe iframe').attr("src","/pop/Pages/video.aspx?t=") ;return true; }

			});

			$("#videoframelarge").dialog({ 
				autoOpen: false,  
				modal: true, 
				width: 670, 
				height: 510,  
				resizable: false , 
				dialogClass: 'color_modal',
				beforeclose: function(event, ui) {$('#videoframelarge iframe').attr("src","/pop/Pages/videolarge.aspx?t=") ;return true; }

			});

		//initialize dialog
		$("#dialog").dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,
		resizable: false,
		buttons: { Ok: function() {$(this).dialog('close');}}
		});

			$("#accordion").accordion( { collapsible: true } );
			
			$("a.tooltip_like").tooltip();

	});	

	function regionChange(region, b_changelanguage)
	{

	selectedregion = region;
	$('div.region').hide();
	var regionid = "#"+ region;
	var regionLinkId = "#r_"+ region;
	$('#RegionsList a').removeClass("current");
	$(regionLinkId).addClass("current");
	$(regionid).show();
	if (b_changelanguage)
	{
	var firstLink =$(regionid + " li").eq(1).find("a");
	//firstLink.removeClass("current").addClass("current");
	selectedlanguage =  firstLink.attr("class");
	languageChange(selectedlanguage);
	}
	}
	
	function languageChange (language)
	{

	$('div.region').find('a').removeClass("current");
		var regionid = "#"+ selectedregion;
		selectedlanguage = language;
		var currentLink = regionid+ " li a."+language;
		$(currentLink).addClass ("current");
		}
	
	
	
	

	function changeSite()
	{
	try{

	if(selectedregion == "")
		selectedregion = "NorthAmerica";
	var newLocation;
	switch (selectedregion)
	{

	case "NorthAmerica":
		/*switch(selectedlanguage)
		{
		case "English":
			newLocation="http://www.ls345extended.com";
			break;
		default:
			newLocation="http://www.ls345extended.com";
		}*/

		hideMe('#util_country', '.lnk_util_country')
		break;
	default:
		newLocation = "http://classic.lutron.com/internationalredirector/default.aspx?country="+ selectedregion +"&language="+ selectedlanguage;
		break;
	}
	

	//window.location(newLocation);

	return newLocation;
	}catch (err)
	{//alert ('error');
	//alert(err);
	}


	}

	function stopVideo(){

		alert('stop video');
		return true;
	}

	function openModal() {
		$('#dialog').dialog('open');
	}
		function encodeUrl(c)
	{
	var o='';
	var x=0;
	c=c.toString();
	var r=/(^[a-zA-Z0-9_.]*)/;
	while(x<c.length)
	{
	var m=r.exec(c.substr(x));
	if(m!=null && m.length>1 && m[1]!='')
	{
		o+=m[1];x+=m[1].length;
	}
	else
	{
		if(c[x]==' ')o+='+';
		else
		{
		var d=c.charCodeAt(x);
		var h=d.toString(16);
		o+='%'+(h.length<2?'0':'')+h.toUpperCase();
		}
		x++;
	}
	}
	return o;}
		
	function decodeURL(s)
	{
	var o=s;
	var binVal,t;
	var r=/(%[^%]{2})/;
	while((m=r.exec(o))!=null && m.length>1 && m[1]!='')
	{
	b=parseInt(m[1].substr(1),16);
	t=String.fromCharCode(b);
	o=o.replace(m[1],t);
	}return o;
	}

	function getRootURL() {
	var baseURL = location.href;
	var rootURL = baseURL.substring(0, baseURL.indexOf('/', 7));

	// if the root url is localhost, don't add the directory as cassani doesn't use it
	if (baseURL.indexOf('localhost') == -1) {
		return rootURL + "/";
	} else {
		return rootURL + "/";
	}
	}

	function processRegTM(text)
	{
	var newStr = replaceAll(text,'®', "<span class='subR'>&reg;</span>");
	newStr = replaceAll(newStr,'™', "<span class='subTM'>TM</span>");
	return newStr;

	}

	function replaceAll(text, strA, strB)
	{
	if(text!=null)
	{
	while ( text.indexOf(strA) != -1)
	{
		text = text.replace(strA,strB);
	}
	}
	return text;
	}
	
	/* cookie functions */
	function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function donothing(){
 return;
}


$(window).load(function (){
	if(isWhereToBuyPageInIE == false)
	{
		$("#header_search input.gsc-input").removeAttr("style");
	
		$("#header_search input.gsc-input").focus(function() {
			$("#header_search").addClass("in");
			$("input.gsc-input").removeAttr("style");
			$("#header_search_button").attr("style", "background: url('/images/backgrounds/bg_header_search.gif') no-repeat scroll bottom right transparent !important");
		});
	}
});