var timer_products_nav = null;
var cur_subnav = null;
var cur_subsubnav = null;
var dropmenu_delay = 1000; // 1 seconds
var cur_slider = 1;
var num_slider_item;
var slides_delays = 5000;
var durations = 600;
var relativePath = "";
var tmp_url = '';

var home_slides = new Array ();

home_slides.push("corian/banner/what-is-corian.jpg");
home_slides.push("zodiaq/banner/what-is-zodiaq.jpg");
home_slides.push("arpa/banner/what-is-arpa.jpg");
home_slides.push("artisan/banner/about-artisan.jpg");
var home_slides_cnt = 0;

function DoHomeSlideShow ()
{
	home_slides_cnt++;
	
	if (home_slides_cnt > home_slides.length-1)
		home_slides_cnt = 0;

	$("#home-banner #next-banner").html ('<img src="img/'+home_slides[home_slides_cnt]+'">');
	
	$('#home-banner #next-banner img').load(function() {	

		$("#home-banner #prev-banner").delay(slides_delays).fadeOut(durations, function () {	
			$("#home-banner #prev-banner").html ($("#home-banner #next-banner").html());
			$("#home-banner #prev-banner").show();
			DoHomeSlideShow ()
		});
			
	});			
}


function PicSlider (pos)
{
	var tmp = cur_slider+pos;
	if (tmp > 0 &&
		tmp <= num_slider_item)
	{	
		cur_slider = tmp;
	
		target = $(".home-slider .list-area .list div:nth-child("+tmp+")")
		$('.home-slider .list-area').scrollTo(target,{duration: 800, axis:'x'})
	}

}

function RefreshProvinceState (value)
{
	$.get("ajax/list-province-state.php?nocache="+(new Date()-0)+"&c="+value, function(data) {
    	$("#province-state").html(data);
 	})	
}

function HideProducts ()
{
	clearTimeout(timer_products_nav)
	$('.products-nav').hide();		
	
	
	HideSubProducts ();
	HideSubSubProducts ();
}

function HideSubProducts ()
{
	if (cur_subnav)
		$(cur_subnav).hide();		
		
	cur_subnav = null;	
}

function HideSubSubProducts ()
{
	if (cur_subsubnav)
		$(cur_subsubnav).hide();	
		
	cur_subsubnav = null;		
}

function InitLightBox ()
{
	$('.content .col2 .detail-pic .medium a').lightBox({ 
		fixedNavigation: false,
		imageLoading: relativePath+'img/lightbox-ico-loading.gif',
		imageBtnClose: relativePath+'img/lightbox-btn-close.gif',
		imageBtnPrev: relativePath+'img/lightbox-btn-prev.gif',
		imageBtnNext: relativePath+'img/lightbox-btn-next.gif'
	})	
}

function LoadFaucetPic (name)
{
	$(".content .col2 .detail-pic .medium").html ('<a href="../img/artisan/faucets/large-'+name+'.jpg" title="'+name.toUpperCase()+'"><img src="../img/artisan/faucets/medium-'+name+'.jpg" width="380" height="370" /></a>')
	InitLightBox ();	
}

function CancelSendToFriendForm ()
{
	$('.send-to-friend').html ('<a onclick="ShowSendToFriendForm(\''+tmp_url+'\')">Send to Friend</a>')	
}

function ProcessSendToFriendForm ()
{
	var data 				= new Object ();
	var has_error			= false;
	data.your_email 		= $('#your-email').val(); 
	data.your_msg			= $('#your-msg').val();
	data.your_friend_email	= $('#your-friend-email').val();
	data.url				= $('#url').val();
	
	if (data.your_email == "")
	{
		alert("Please enter your e-mail.");
   		$('#your-email').focus();
    	return (false);		
	}
	else if (!IsEmailAddress(data.your_email))
	{
		alert("Please enter a valid e-mail.");
   		$('#your-email').focus();
    	return (false);	
	}
	
	if (data.your_friend_email == "")
	{
		alert("Please enter your friend e-mail.");
   		$('#your-friend-email').focus();
    	return (false);		
	}
	else if (!IsEmailAddress(data.your_friend_email))
	{
		alert("Please enter a valid e-mail.");
   		$('#your-friend-email').focus();
    	return (false);	
	}
	
	if (!has_error)
	{
		$.post(relativePath+'ajax/send-to-friend.php',{data:$.toJSON(data)}, function(data) {
			
			$('.send-to-friend').html ('Thanks for forwarding this to your friends!')	
		});
	}
}

function IsEmailAddress(email)
{
	var result = false
	var theStr = new String(email)
	var index = theStr.indexOf("@");
	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	}
	return result;
}

function ShowSendToFriendForm (url)
{
	tmp_url = url;
	var html = '<form>';
	html	+= '* - required. <br>';
	html	+= 'Your Email: *<br>';
	html	+= '<input type="text" id="your-email" style="width:200px"><br><br>';
	html	+= 'Your Friend\'s Email: *<br>';
	html	+= '<input type="text" id="your-friend-email" style="width:200px"><br><br>';
	html	+= 'Your Message: <br>';
	html	+= '<textarea cols="40" rows="5" id="your-msg" style="width:200px">You might find this information from Willis useful.</textarea><br><br>';
	
	html	+= '<input type="hidden" id="url" value="'+url+'">';
	html	+= '<a onclick="ProcessSendToFriendForm ()">Send</a> &nbsp; &nbsp; ';
	html	+= '<a onclick="CancelSendToFriendForm ()">Cancel</a>';
	html	+= '</form>';
	
	$('.send-to-friend').html(html)
}


// check to see if it is in e-mail format

	function isemailAddr(email)
	{
	  var result = false
	  var theStr = new String(email)
	  var index = theStr.indexOf("@");
	  if (index > 0)
	  {
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	  }
	  return result;
	}


var RE_SSN = /^\s*[a-ceghj-npr-tvxy]\d[a-ceghj-npr-tv-z](\s)?\d[a-ceghj-npr-tv-z]\d\s*$/i;

function checkSsn(Postal){
 if (!RE_SSN.test(Postal)) {
  alert("Please enter a valid Postal Code in the following format M4Y 1N8 to continue.");
/*  window.location = "http://markettowncondos.com/register.php"*/
      document.subform.Postal.focus();
   return (true);
 } 
}
	
	
	function submitIt(){
	
	// fill in first and last name, telephone and e-mail
 if(document.subform.subject.value == ""){
     alert("Please enter your subject.");
     document.subform.subject.focus();
     return false;
    }
	
	 
  if(document.subform.name.value == ""){
     alert("Please enter your  name.");
     document.subform.name.focus();
     return false;
    }
	

	
	 if (document.subform.email.value == "")
  {
    alert("Please enter your email.");
    document.subform.email.focus();
    return (false);
  }

  if (!isemailAddr(document.subform.email.value))
  {
    alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    document.subform.email.focus();
    return (false);
  }
   
  if (document.subform.email.value.length < 3)
  {
    alert("Please enter at least 3 characters in the email field.");
    document.subform.email.focus();
    return (false);
  }

	

	if(document.subform.message.value == ""){
     alert("Please enter your message.");
     document.subform.message.focus();
     return false;
    }
	
	
	return true;
}

$(document).ready(function() {	
	if ($.browser.safari)
	{
		$('.content .col1 div a sup').css('vertical-align','text-top');	
	}

	num_slider_item = $('.home-slider .list-area .list .item').size();
	if (num_slider_item)
		$(".home-slider .list-area .list .item:nth-child("+num_slider_item+")").css("margin-right","0px");
	
	$('.home-slider .list-area .list').width((225*num_slider_item-10));


	$(".content .col2 .text .qmark").tipTip({maxWidth: "300px", edgeOffset: 10});

	// Resize width of the subnav
	var subnavs = new Array ("corian","zodiaq","artisan")
	for (var i=0; i < subnavs.length; i++)
	{
		$('#'+subnavs[i]+'-nav').width($('#'+subnavs[i]+'-nav').width()+20);
		$('#'+subnavs[i]+'-nav div span').css("left",$('#'+subnavs[i]+'-nav').width()-15);
		
	}
	
	
	$('.main-nav a').mouseover (function () {
		if ($(this).attr("class") == "products")
		{
			clearTimeout(timer_products_nav);
			$('.products-nav').show();		
			HideSubProducts ();
			HideSubSubProducts ();
		}
		else
			HideProducts ();
	});
	$('.main-nav .products').mouseout (function () {
		timer_products_nav = setTimeout("HideProducts ()",dropmenu_delay);
	});
	
	$('.products-nav').mouseover (function () {
		clearTimeout(timer_products_nav);
		HideSubSubProducts ();
	});	
	$('.products-nav').mouseleave (function () {
		timer_products_nav = setTimeout("HideProducts ()",dropmenu_delay);
	});
	
	$('.products-nav div').mouseover (function () {
		var tmp = $(this).attr("class");
		if (cur_subnav != '#'+tmp+'-nav')
		{
			$(cur_subnav).hide();
			cur_subnav = '#'+tmp+'-nav';	
		}
		$('#'+tmp+'-nav').show();
	});
	
	$('.sub-nav').mouseover (function () {
		clearTimeout(timer_products_nav);
	});
	$('.sub-nav').mouseleave (function () {
		timer_products_nav = setTimeout("HideProducts ()",dropmenu_delay);
	});
	
	
	$('.sub-nav div').mouseover (function () {
	
		var tmp = ($(this).parent().attr("id")).split("-");
		
		if ($(this).attr("id"))
		{
			$(cur_subsubnav).hide();
			var tmp_id = "#"+tmp[0]+"-"+$(this).attr("id")+"-nav";
			
			$('#'+tmp[0]+'-'+$(this).attr("id")+'-nav').css ("top",$("#"+tmp[0]+"-nav #"+$(this).attr("id")).offset().top-2);
			$('#'+tmp[0]+'-'+$(this).attr("id")+'-nav').css ("left",$("#"+tmp[0]+"-nav").position().left + $("#"+tmp[0]+"-nav").width());	

			
			$(tmp_id).show();
			cur_subsubnav = tmp_id;
		}
		else if (cur_subsubnav && '#'+$(this).parent().attr("id") != cur_subsubnav)
		{
			$(cur_subsubnav).hide();
			cur_subsubnav = null;
		}
	});
	
	var lightbox_list = new Array ();
	lightbox_list.push (".cube-list");
	lightbox_list.push (".cube-list-mod");
	lightbox_list.push (".rect-list");
	lightbox_list.push (".rect-list-2");
	lightbox_list.push (".rect-list-3");
	for (var i=0; i < lightbox_list.length; i++)
	{	
		$('.content .col2 .text '+lightbox_list[i]+' .item .pic a').lightBox({ 
			fixedNavigation: false,
			imageLoading: relativePath+'img/lightbox-ico-loading.gif',
			imageBtnClose: relativePath+'img/lightbox-btn-close.gif',
			imageBtnPrev: relativePath+'img/lightbox-btn-prev.gif',
			imageBtnNext: relativePath+'img/lightbox-btn-next.gif'
		})		
		
	}
	
	$('.content .col2 .text .single-item').lightBox({ 
			fixedNavigation: false,
			imageLoading: relativePath+'img/lightbox-ico-loading.gif',
			imageBtnClose: relativePath+'img/lightbox-btn-close.gif',
			imageBtnPrev: relativePath+'img/lightbox-btn-prev.gif',
			imageBtnNext: relativePath+'img/lightbox-btn-next.gif'
		})		
	
	if ($('.content .col2 .text .faucet .item').size())
	{
		$(".content .col2 .text .faucet .item:nth-child(4n)").css("margin-right","0px");
	}
	
	if ($('.content .col2 .text .cube-list .item').size())
	{
		$(".content .col2 .text .cube-list .item:nth-child(5n)").css("margin-right","0px");	
	}

	if ($('.content .col2 .text .rect-list .item').size())
	{
		$('.content .col2 .text .rect-list .item:nth-child(4n)').css("margin-right","0px");	
	}
	
	if ($('.content .col2 .text .rect-list-3 .item').size())
	{
		$('.content .col2 .text .rect-list-3 .item:nth-child(3n)').css("margin-right","0px");	
	}
	
	if ($('.content .col2 .text .rect-list-2 .item').size())
	{
		$('.content .col2 .text .rect-list-2 .item:nth-child(2n)').css("margin-right","0px");	
	}


	if ($('.content .col2 .text .bathroom-sink .item').size())
	{
		$('.content .col2 .text .bathroom-sink .item').each(function(index) {
			$('.content .col2 .text .bathroom-sink #'+$(this).attr("id")+' div a').lightBox({ 
				fixedNavigation: false,
				imageLoading: relativePath+'img/lightbox-ico-loading.gif',
				imageBtnClose: relativePath+'img/lightbox-btn-close.gif',
				imageBtnPrev: relativePath+'img/lightbox-btn-prev.gif',
				imageBtnNext: relativePath+'img/lightbox-btn-next.gif'
			});
		});
	}

	$('.cube-list .item .pic .border').each (function(index) {
		var tmp = ($(this).children("a").attr("href")).replace("large-","small-");
		$(this).css("background-image","url("+tmp+")");
	});

	$('.cube-list-mod .item .pic .border').each (function(index) {

		var tmp = ($(this).children("a").attr("href")).replace("large-","small-");
		$(this).css("background-image","url("+tmp+")");
	});


	if ($('.content .col2 .text #accessories .item').size())
	{
		$('.content .col2 .text #accessories .item').each(function(index) {
			$('.content .col2 .text #accessories #'+$(this).attr("id")+' div a').lightBox({ 
				fixedNavigation: false,
				imageLoading: relativePath+'img/lightbox-ico-loading.gif',
				imageBtnClose: relativePath+'img/lightbox-btn-close.gif',
				imageBtnPrev: relativePath+'img/lightbox-btn-prev.gif',
				imageBtnNext: relativePath+'img/lightbox-btn-next.gif'
			});
		});
	}

	if ($('.content .col2 .text .kitchen-sink .item').size())
	{
		$('.content .col2 .text .kitchen-sink .item').each(function(index) {
			$('.content .col2 .text .kitchen-sink #'+$(this).attr("id")+' .c2 a').lightBox({ 
				fixedNavigation: false,
				imageLoading: relativePath+'img/lightbox-ico-loading.gif',
				imageBtnClose: relativePath+'img/lightbox-btn-close.gif',
				imageBtnPrev: relativePath+'img/lightbox-btn-prev.gif',
				imageBtnNext: relativePath+'img/lightbox-btn-next.gif'
			})	
  		});
	}
	InitLightBox ();
	
	if ($('#home-banner').size())
		DoHomeSlideShow();
});


