// JavaScript Document

// for product list page
$(function () {
	$("a.switch_thumb").toggle(function(){
	  $(this).addClass("swap"); 
	  $("ul.display").fadeOut("fast", function() {
	  	$(this).fadeIn("fast").removeClass("thumb_view"); 
		 });
	  }, function () {
      $(this).removeClass("swap");
	  $("ul.display").fadeOut("fast", function() {
	  	$(this).fadeIn("fast").addClass("thumb_view");
		});
	}); 
});

// add to cart and check out box
$(function () {
  
  var msie6 = $.browser == 'msie' && $.browser.version < 7;
  
  if (!msie6) {
	  var div = $('#FloatingElement');  
	  if(div.length){
			
			var top = div.offset().top - parseFloat(div.css('margin-top').replace(/auto/, 0));
			$(window).scroll(function (event) {
			  // what the y position of the scroll is
			  var y = $(this).scrollTop();
			  
			  // whether that's below the form
			  if (y >= top) {
				// if so, ad the fixed class
				div.addClass('fixed');
			  } else {
				// otherwise remove it
				div.removeClass('fixed');
			  }
			});
	  }
  }  
});

// product tabs
$(function () {
		var tabContainers = $('div.tabs > div');
		tabContainers.hide().filter(':first').show();
		
		$('div.tabs ul.tabNavigation a').click(function () {
				tabContainers.hide();
				tabContainers.filter(this.hash).show();
				$('div.tabs ul.tabNavigation a').removeClass('selected');
				$(this).addClass('selected');
				return false;
		}).filter(':first').click();
});

// hightlight current page like
$(function(){
	page = window.location.pathname;
	
	if(!page) {
		page = 'index.html';
	}
	$('.product-filter li a, .home-title a').each(function(){
		var href = $(this).attr('href');
		if ( (href == page) || (href == '') ) {
			$(this).addClass('on');
		} else {
			$(this).removeClass('on');
		}
	});
});



// for product page
jQuery(document).ready(function() {
	if ($('.input_qty').length){								
	  	$('.input_qty').click(function(e) {
					if(this.value == 0)
						this.value = 1;
					
					this.select();
		});
	
	
	 $('.input_qty').blur(function(e) {
		if(!(this.value == parseInt(this.value)))
			this.value = 1;
		});
	}
	
	//facebox popup
	$('a[rel*=facebox]').facebox();

   
});




// facebox auto close
var timeoutId =0;

function IsEmpty(v){
	return  ((v.val() == null) || (v.val().length == 0));
}

function fail(v){
	$(v).after("<span class='error'> < Invalid</span>");
	$(v).css("border","solid #ff0000 1px");
}

function checkname(v){
	if  (!IsEmpty(v) && v.val().length >= 2){
		$(v).css("border","solid #006600 1px");
		return true;
	}else
		fail(v)
	return false	
}

function checkzip(v){
	var regex = /((^\d{5}([- |]\d{4})?$)|(^[A-Z]\d[A-Z][- |]\d[A-Z]\d$))/;
	if (!IsEmpty(v) && regex.test(v.val())){
		$(v).css("border","solid #006600 1px");
		return true;
	}else{
		fail(v)
	}
	return false
	
}

function checkphone(v){
	var regex = /^(\()?(\d{3})?([\)])?([ -.])?(\d{3})([-\. ])?(\d{4})$/;     
	if (!IsEmpty(v) && regex.test(v.val())){
		$(v).css("border","solid #006600 1px");
		return true;
	}else
		fail(v)
	return false
}

function checkemail(v){
	var regex = /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/;    
	if (!IsEmpty(v) && regex.test(v.val())){
		$(v).css("border","solid #006600 1px");
		return true;
	}else
		fail(v)
	return false
}	

function close() {
	$("#facebox").slideUp();

	if(timeoutId != 0)
		window.clearTimeout ( timeoutId );
		
	$("#facebox").unbind()		
  return false
}	


//visitor log
jQuery(window).load(function () {
	$.post("/visitor/Vlog.php", { title: document.title, url: document.URL, referral: document.referrer} );
	
	
})


// quick comment question - qcq
// quick comment question - qcq
var qcq_google_stats = true;
var textarea_txt = 'Write your question or comment here...'
$(function () {
	$("#textarea_qcq").focus(
		function(){
			if($(this).val() == textarea_txt){
				$(this).height(70).val('');	
				$(".hide").show();
			}
			if(qcq_google_stats){
				qcq_google_stats = false;
				googlePageTracker("/contact/qcq.htm")
				
			}
		}
	);
	
	$("#textarea_qcq").blur(
		function(){
			if($(this).val() == ''){
				$(this).val(textarea_txt);	
			}
		}
	);
	
	
	$("#qcq_reset").click(
		function(){
			$("#textarea_qcq").height(14).val(textarea_txt);	
			$(".hide").hide();
		}
	);
});


function googlePageTracker(url){
	pageTracker._trackPageview(url)	
}
			

function qcq(){
	if(checkname($("#qcq_firstname")) && checkname($("#qcq_lastname")) && checkemail($("#qcq_email")) && checkzip($("#qcq_zipcode"))){
		$.post("/contact/qcq", { firstname: $("#qcq_firstname").val(), lastname: $("#qcq_lastname").val(), zipcode: $("#qcq_zipcode").val(), phone: $("#qcq_phone").val(), email: $("#qcq_email").val(), url: document.URL, comment: $("#textarea_qcq").val()});
		$("#div_qcq").html('<div style="text-align:center;padding:7px">We will contact you shortly</div>')
		googlePageTracker("/contact/qcq_thankyou.htm")
	}	
	
}
// end quick comment question


$.getScript("http://s7.addthis.com/js/250/addthis_widget.js#pub=ardinfurniture");