contentMode = false;
loading = "";
cache = {};
var page;
StateNames = {AL: "Alabama", AK: "Alaska", AZ: "Arizona", AR: "Arkansas", CA: "California", CO: "Colorado", CT: "Connecticut", DE: "Delaware", DC: "Washington, D.C.", FL: "Florida", GA: "Georgia", HI: "Hawaii", ID: "Idaho", IL: "Illinois", IN: "Indiana", IA: "Iowa", KS: "Kansas", KY: "Kentucky", LA: "Louisiana", ME: "Maine", MD: "Maryland", MA: "Massachusetts", MI: "Michigan", MN: "Minnesota", MO: "Missouri", MS: "Mississippi", MT: "Montana", NE: "Nebraska", NV: "Nevada", NH: "New Hampshire", NJ: "New Jersey", NM: "New Mexico", NY: "New York", NC: "North Carolina", ND: "North Dakota", OH: "Ohio", OK: "Oklahoma", OR: "Oregon", PA: "Pennsylvania", RI: "Rhode Island", SC: "South Carolina", SD: "South Dakota", TN: "Tennessee", TX: "Texas", UT: "Utah", VT: "Vermont", VA: "Virginia", WA: "Washington", WV: "West Virginia", WI: "Wisconsin", WY: "Wyoming"};

var mouse_is_inside_stats = false; 

function formatName(contact) {
	var f = contact.FirstName,
		m = contact.Initial,
		l = contact.LastName,
		full = "";
	if (f && f != "")
		full = f;
	if (m && m != "") {
		if (full != "")
			full += " ";
		full += m + ".";
	}
	if (l && l != "") {
		if (full != "")
			full += " ";
		full += l;
	}
	return full;
}


function selectMenu2(menu, submenu) {
$("#menuBar *, #nav" + menu + " a").removeClass("selected");

$("#nav" + menu + " > a[id=" + submenu + "]").addClass("selected");			
}

function selectMenu(menu, submenu) {
	$(".page, .navigation").hide();
	$("#menuBar *, #nav" + menu + " a").removeClass("selected");
	if (menu) {
		if (!contentMode)
			setContentMode(true);
		$("#menu" + menu)
			.addClass("selected")
			.attr("href", submenu);
		$("#nav" + menu).show();
		if (submenu)
			$("#nav" + menu + " > a[href=" + submenu + "]").addClass("selected");
	} else if (contentMode) {
		setContentMode(false);
	}
}

function inArray(needle, haystack) {
	for (var i in haystack) {
		if (haystack[i] == needle)
			return true;
	}
	return false;
}


function hideiframe(){
	$('#pageMainFrame').hide(1000);
	$('#loader').show();
	alert("loading");
}


/****************************************************************************************************************************************************/
/* Document Ready                                                                                                                          */
/****************************************************************************************************************************************************/

 $(document).ready(function() {

$('.company_images').live('click',function(){
	
	var $newimgid = $(this).attr('id');
	$('.image_holder').css("display","none");
	$('#'+$newimgid+'_holder').attr("style", {display: "block"});
	return false;
	
});



/*******************************************************************/
/*  Hide Specs on mouse click                                      */
/*******************************************************************/
	
	 $('#pspecs').hover(function(){mouse_is_inside_stats=true;}, function(){mouse_is_inside_stats=false;});      
	 $("body").mouseup(function(){          
	 if(! mouse_is_inside_stats){ 
	 	if ($("#pspecs").is(":visible")){        
			$( "#pspecs" ).toggle("slow"); 
		} 
	 }
	 }); 
	


/****************************************************************************************************************************************************/
/* Replace Loading Images on Home Page                                                                                                                                     */
/****************************************************************************************************************************************************/

 	var fimageurl = 'http://www.legacyfurniture.us/images/neocon2011/';
	var fnoimageurl = 'http://www.legacyfurniture.us/wp-content/themes/legacy-furniture/images/ads/loading.gif';
	
$('.f_loadingimg').each(function(){ 
 	var titletag = $(this).attr("alt");
	var imgsrc = fimageurl + titletag;
		$(this).attr('src', imgsrc);
 		$(this).error(function() {
  			$(this).attr('src', fnoimageurl);
		});
}); 


/****************************************************************************************************************************************************/
/* Replace Loading Thumbnails Images                                                                                                                                     */
/****************************************************************************************************************************************************/
	
	var pimageurl = 'http://www.legacyfurniture.us/images/product-thumbnails/';
	var pnoimageurl = 'http://www.legacyfurniture.us/wp-content/themes/legacy-furniture/images/no-image.jpg';
	
$('.loadingimg').each(function(){ 
 	var titletag = $(this).attr("alt");
	var imgsrc = pimageurl + titletag;
		$(this).attr('src', imgsrc);
 		$(this).error(function() {
  			$(this).attr('src', pnoimageurl);
		});
}); 	

/****************************************************************************************************************************************************/
/* Replace Loading PDF Images                                                                                                                                     */
/****************************************************************************************************************************************************/
	
	var pdfimageurl = 'http://www.legacyfurniture.us/images/collection_pdfs/';
	var pdfnoimageurl = 'http://www.legacyfurniture.us/images/no-image.jpg';
	
$('.loadingpdfimg').each(function(){ 
 	var pdftitletag = $(this).attr("alt");
	var pdfimgsrc = pdfimageurl + pdftitletag;
		$(this).attr('src', imgsrc);
 		$(this).error(function() {
  			$(this).attr('src', pdfnoimageurl);
		});
}); 

/****************************************************************************************************************************************************/
/* Initializing Thumbnail Rotation on Products Page                                                                                                                                    */
/****************************************************************************************************************************************************/

		var div = $('div.showcase');
		//find ul width
		var liNum = $(div).find('ul').children('li').length;
		var speed = 1000;
		var containerWidth = 355;
		var itemWidth = 88;
		//Remove scrollbars
		$(div).css({overflow: 'hidden'});
		$('div.right-arrow').click(function(e){	
				//alert($(div).scrollLeft()+containerWidth);
				//alert(liNum*itemWidth);
			  if(($(div).scrollLeft()+containerWidth)<(liNum*itemWidth)){
				 $(div).animate({
					scrollLeft: '+='+containerWidth
				  }, speed);
			  }
		});	
		$('div.left-arrow').click(function(e){	
			if(($(div).scrollLeft()+containerWidth)>containerWidth){
					$(div).animate({
					scrollLeft: '-='+containerWidth
				  }, speed);
			}
		});




});






/****************************************************************************************************************************************************/
/* Stretchy menu spacing                                                                                                                            */
/****************************************************************************************************************************************************/

var menuContainer, lastWidth;
function resizeMenuSpacing() {
	if (!menuContainer)
		menuContainer = $("#menuBar");
	var width = menuContainer.width();
	if (lastWidth && lastWidth == width)
		return;
	else
		lastWidth = width;
	var itemWidth = 815, // 102 + 107 + 195 + 148 + 118 + 130 = 800   // without MyLegacy = 670
		marginCount = 12, // 6 * 2 = 12
		margin = Math.floor((width - itemWidth) / marginCount);
	$("a", menuContainer).css({
		marginLeft: margin,
		marginRight: margin,
		top: "0px"
	});
}


/****************************************************************************************************************************************************/
/* Stretchy background on home page                                                                                                                             */
/****************************************************************************************************************************************************/

function resizeBackground2() {
	if (!background2.container) {
		background2.container = $("#homepagecontent");
		background2.image = $("img", background2.container);
	}

	var offset = 33,
		width = background2.container.width(),
		height = background2.container.height(),
		wScale = width / background2.width,
		offsetHeight = height - 2 * offset,
		hScale = offsetHeight / background2.height;
	if (hScale > wScale) {
		var newWidth = background2.width * hScale;
		
		if (offsetHeight > 549 || offsetHeight ){
		background2.image.css({
			height: offsetHeight,
			top: offset,
			width: newWidth,
			left: (width - newWidth) / 2
		});
		}
	} else {
		var newHeight = background2.height * wScale;
		
		if (newHeight > 549){
		background2.image.css({
			height: newHeight,
			top: (offsetHeight - newHeight) / 2 + offset,
			width: width,
			left: 0
		});
		}
	}
}


/****************************************************************************************************************************************************/
/* Adjust Home page buttons according to screen size                                                                                                                                     */
/****************************************************************************************************************************************************/
function adjusthomebuttons() {  

var w = $(window).width();

//alert(b1w);

	if (w <= 1224){ 
		  if($('.button4').attr("style", {display: "block"})){
			$('.button4').css("display","none");
			$('.button1').css("width","33%");
			$('.button2').css("width","33%");
			$('.button3').css("width","33%");
			
		  }
	}else{
		  if($('.button4').attr("style", {display: "none"})){
			$('.button4').css("display","block");
			$('.button1').css("width","25%");
			$('.button2').css("width","25%");
			$('.button3').css("width","25%");
			$('.button4').css("width","25%");
		  }
	}
}

/****************************************************************************************************************************************************/
/* Search bar                                                                                                                                       */
/****************************************************************************************************************************************************/

function initSearch() {
	$("#searchForm").submit(function() {
		var val = $("input[type=text]", this).val();
		if (val != "" && val != "Search Products")
			$.bbq.pushState("#page=search&keywords=" + val.replace("&", " "));
		return false;
	});
	$("#searchForm input[type=text]").focus(function() {
		if ($(this).val() == "Search Products") {
			$(this).val("");
			$(this).css("color", "black");
		}
	}).blur(function() {
		if ($(this).val() == "")
			$(this).val("Search Products");
		if ($(this).val() == "Search Products") {
			$(this).css("color", "#aaa");
		}
	});
}

function resizeProductShots() {
	$("#pageProducts .productshot img").each(function() {
		var p = $(this).parent();
		$(this).css({"max-width": p.width() + "px", "max-height": p.height() + "px"});
	});
}


/****************************************************************************************************************************************************/
/* Check if user is on ipad                                                                                                                                 */
/****************************************************************************************************************************************************/
function isiPad(){    
	return (navigator.platform.indexOf("iPad") != -1);
}
function checkiPad(){
	if(isiPad()){
		$(".applanding").css("width", "75px");
		$(".applanding").css("height", "218px");
		$(".f_loadingimg").css("height", "631px");	
		$(".f_loadingimg").css("width", "392px");
		
	}
}
/****************************************************************************************************************************************************/
/* Initializing Product Image Magnify                                                                                                               */
/****************************************************************************************************************************************************/
function magnifyproduct(){	
	$('.zoom').unbind();
    event.preventDefault();
	rebindmagnify();
}

function rebindmagnify(){
	$('.zoom').jqzoom(options);
    $('.zoom').bind();        
    event.preventDefault();
}

/****************************************************************************************************************************************************/
/* Checking for touch device                                                                                                                        */
/****************************************************************************************************************************************************/

function isTouchDevice(){
	try{
		document.createEvent("TouchEvent");
		return true;
	}catch(e){
		return false;
	}
}

function touchScroll(id){
  if(isTouchDevice()){ //if touch events exist…
      var el=document.getElementById(id);
      var scrollStartPosY=0;
      var scrollStartPosX=0;

      document.getElementById(id).addEventListener("touchstart", function(event) {
        scrollStartPosY=this.scrollTop+event.touches[0].pageY;
        scrollStartPosX=this.scrollLeft+event.touches[0].pageX;
        //event.preventDefault(); // Keep this remarked so you can click on buttons and links in the div
      },false);

      document.getElementById(id).addEventListener("touchmove", function(event) {
        // These if statements allow the full page to scroll (not just the div) if they are
        // at the top of the div scroll or the bottom of the div scroll
        // The -5 and +5 below are in case they are trying to scroll the page sideways
        // but their finger moves a few pixels down or up.  The event.preventDefault() function
        // will not be called in that case so that the whole page can scroll.
        if ((this.scrollTop < this.scrollHeight-this.offsetHeight &&
          this.scrollTop+event.touches[0].pageY < scrollStartPosY-5) ||
          (this.scrollTop != 0 && this.scrollTop+event.touches[0].pageY > scrollStartPosY+5))
              event.preventDefault(); 
        if ((this.scrollLeft < this.scrollWidth-this.offsetWidth &&
          this.scrollLeft+event.touches[0].pageX < scrollStartPosX-5) ||
          (this.scrollLeft != 0 && this.scrollLeft+event.touches[0].pageX > scrollStartPosX+5))
              event.preventDefault(); 
        this.scrollTop=scrollStartPosY-event.touches[0].pageY;
        this.scrollLeft=scrollStartPosX-event.touches[0].pageX;
      },false);
  }
} 

/****************************************************************************************************************************************************/
/* Resize Product image on product page                                                                                                             */
/****************************************************************************************************************************************************/

function resizemainproductimage(){
	var geth = $(window).height();
	//alert(geth);
	if(geth < 689){ 
		var getnewh = Math.round((geth * .43));
		$("#myimage").css("width", getnewh+"px");
	}
	if(geth >= 689){ 
		$("#myimage").css("width","372px");	
	}
}

/****************************************************************************************************************************************************/
/* Auto run                                                                                                                                         */
/****************************************************************************************************************************************************/

$(function() {
	$(window)
		.bind("resize", resizeMenuSpacing)
		//.bind("resize", resizeBackground2)
		.bind("resize", adjusthomebuttons)
		.bind("resize", checkiPad)
		//.bind("resize", resizeProductShots)
		//.bind("resize", magnifyproduct)
		.bind("resize", resizemainproductimage)
		.trigger("resize");
	initSearch();
});
