/***********************************************************************************************************************

jbrowser.js : Website Functionality

Author:          Bruce Arnold (bruce@webredesignmiami.com)
Copyright:       2011 (c) Bruce Arnold | Web3.0DesignMiami.com  
License:         All Rights Reserved

************************************************************************************************************************/

/***********************************************************************************************************************
* PRELOAD
************************************************************************************************************************/

// PRELOAD IMAGES
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
jQuery.preLoadImages(
"./images/brushes-combs-spinner.gif",
"./images/semantic_website_design.gif", 
"./images/addtocart.png",
"./images/clear.gif",
"./images/thumb_bg3.png",
"./images/Bone_Combs_Hair_Brushes.png",
"./images/Hairsense.com.gif",
"./images/HairsenseBoneCombs.com.gif",
"./images/Seamless_Bakelite_Detangler_Rakes.gif",
"./images/bone-combs-bg1.png",
"./images/bone-combs-bg2.png",
"./images/bonecombs_hairbrushes_bg1.png",
"./images/bonecombs_hairbrushes_bg2.png",
"./images/hairbrushes-bg1.png",
"./images/hairbrushes-bg2.png",
"./images/paypal_secure_checkout.png"
);

/***********************************************************************************************************************
* ON DOCUMENT READY
************************************************************************************************************************/

// TWITTER UPDATE
function loadSearch1() {
	$.ajax({
	type: "GET",
	url: "./php/search.php",
	cache: false,
	beforeSend: function(html) 
	{
  	  document.getElementById("insert-search").innerHTML = ''; 
	  $('#update1').removeClass('hide');
	  $('#update1').html('<img src="./images/semantic_website_design.gif" width="42" height="42" alt="Loading..." />');	               
      },
	success: function(html)
	{
	  $('#update1').attr('class','show');
	  $("#insert-search").append(html);
	  $('#update1').attr('class','hide');
	  $(function() {
		$("#insert-search a").attr("target", "_blank");
	  });
	}
	});
}

// BLOG UPDATE
function loadBlog1() {
	$.ajax({
	type: "GET",
	url: "./php/blog1.php",
	cache: false,
	beforeSend: function(html) 
	{
 	  document.getElementById("blog-insert1").innerHTML = '';
	  $('#blog-update1').removeClass('hide');
	  $('#blog-update1').html('<img src="./images/semantic_website_design.gif" width="42" height="42" alt="Loading..." />');	               
	},
	success: function(html)
	{
	  $('#blog-update1').attr('class','show');
	  document.getElementById("blog-insert1").innerHTML = html; 
	  $('#blog-update1').attr('class','hide');
	}
	});
}

// FORM CHECK
var retVal = false; 
function check_it(the_form){
  $(window).scrollTop(0); 
  check_it2(the_form);
  return retVal;
}
function check_it2(the_form){
    var headerOffset = 157;
    if (the_form.Contact_Name.value == '') {
        alert('Please enter a Contact Name');
        $('html,body').animate({ scrollTop: ( $("#Contact_Name").offset().top - headerOffset ) },
           { duration: 'slow', easing: 'swing'});
        the_form.Contact_Name.focus(); 
        retVal = false;  
        return false;  
    }
    var illegalChars1= /[\(\)\<\>\,\;\:\\\/\"\@\[\]]/
    if (the_form.Contact_Name.value.match(illegalChars1)) {
        alert('Contact name contains illegal characters');
        $('html,body').animate({ scrollTop: ( $("#Contact_Name").offset().top - headerOffset ) },
           { duration: 'slow', easing: 'swing'});
        the_form.Contact_Name.focus(); 
        retVal = false;  
        return false;  
    }
    var illegalChars2= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
    if (the_form.email.value.match(illegalChars2)) {
        alert('The email address contains illegal characters');
        $('html,body').animate({ scrollTop: ( $("#email").offset().top - headerOffset ) },
           { duration: 'slow', easing: 'swing'});
        the_form.email.focus();
        retVal = false;  
        return false;  
    }
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(the_form.email.value))) { 
        alert('Please enter a valid email address');
        $('html,body').animate({ scrollTop: ( $("#email").offset().top - headerOffset ) },
           { duration: 'slow', easing: 'swing'});
        the_form.email.focus();
        retVal = false;  
        return false;  
    }
    if(the_form.email.value != the_form.email_confirm.value){
        alert('Email address does not match Email Confirm.');
        $('html,body').animate({ scrollTop: ( $("#email").offset().top - headerOffset ) },
           { duration: 'slow', easing: 'swing'});
        the_form.email.focus();
        retVal = false;  
        return false;  
    }
    var checkdigit4 = parseInt(the_form.checkdigit1.value)+ parseInt(the_form.checkdigit2.value);
    if(parseInt(the_form.checkdigit3.value) != checkdigit4 ){
        alert('Invalid Spam Check Digit.');
        $('html,body').animate({ scrollTop: ( $("#checkdigit3").offset().top - headerOffset ) },
           { duration: 'slow', easing: 'swing'});
        the_form.checkdigit3.focus();
        retVal = false;  
        return false;  
    }
    retVal = true; 
}

// ANIMATE
function animate1() {
  // THUMBNAILS
  $(".product-thumb").hover(function() {
	$(this).css({'z-index' : '10'});
	$(this).find('img').addClass("hover").stop()
		.animate({
			top: '-23px', 
			left: '-23px', 
			width: '143px', 
			height: '143px',
			padding: '13px'
		}, 100);
	
	} , function() {
	$(this).css({'z-index' : '0'});
	$(this).find('img').removeClass("hover").stop()
		.animate({
			top: '0', 
			left: '0', 
			width: '113px', 
			height: '113px',
			padding: '5px'
		}, 200);
  });
  // LIGHTBOX INIT
	$('.product-thumb a').lightBox({fixedNavigation:false});
  // FORMS INIT 
  $('#Contact').submit(function() {
	return check_it(this)
  });
  $("#submit").hover(
    function () {
 	$(this).css('padding','2px 0px 0px 2px'); 
    },
    function () {
 	$(this).css('padding','1px'); 
    }
  );
  // CLICKABLE DIVS
  $('.product-clickable').live('click', function() {
      window.open($(this).find("a").attr("href"));
      return false;
  });
  $(".comb-brush").hover(
    function () {
      $(this).css('backgroundPosition','0 0');
 	$(this).css('cursor','pointer'); 
    }, 
    function () {
      $(this).css('backgroundPosition','300px 0px');
	$(this).css('cursor','auto'); 
    }
  );
  // IMAGE ENLARGEMENTS
  $("#bonecombs-hairbrushes").hover(
    function () {
 	$(this).css('height','78px'); 
 	$(this).css('width','478px'); 
 	$(this).css('padding','1px'); 
  },
    function () {
 	$(this).css('height','80px'); 
 	$(this).css('width','480px'); 
 	$(this).css('padding','0px'); 
  }
  );
  $("#detangler-rakes").hover(
    function () {
 	$(this).css('height','78px'); 
 	$(this).css('width','183px'); 
 	$(this).css('padding','1px'); 
  },
    function () {
 	$(this).css('height','80px'); 
 	$(this).css('width','185px'); 
 	$(this).css('padding','0px'); 
  }
  );
  $("#paypal-1").hover(
    function () {
 	$(this).css('height','48px'); 
 	$(this).css('width','178px'); 
 	$(this).css('padding','1px'); 
  },
    function () {
 	$(this).css('height','50px'); 
 	$(this).css('width','180px'); 
 	$(this).css('padding','0px'); 
  }
  );
  $("#paypal-verified-1").hover(
    function () {
 	$(this).css('height','44px'); 
 	$(this).css('width','44px'); 
 	$(this).css('padding','1px'); 
  },
    function () {
 	$(this).css('height','46px'); 
 	$(this).css('width','46px'); 
 	$(this).css('padding','0px'); 
  }
  );
  $("#ebay-1").hover(
    function () {
 	$(this).css('height','44px'); 
 	$(this).css('width','109px'); 
 	$(this).css('padding','1px'); 
  },
    function () {
 	$(this).css('height','46px'); 
 	$(this).css('width','111px'); 
 	$(this).css('padding','0px'); 
  }
  );
  $("#norton-safe-web-1").hover(
    function () {
 	$(this).css('height','44px'); 
 	$(this).css('width','76px'); 
 	$(this).css('padding','1px'); 
  },
    function () {
 	$(this).css('height','46px'); 
 	$(this).css('width','80px'); 
 	$(this).css('padding','0px'); 
  }
  );
  $("#contact-hairsense-1").hover(
    function () {
 	$(this).css('height','44px'); 
 	$(this).css('width','67px'); 
 	$(this).css('padding','1px'); 
  },
    function () {
 	$(this).css('height','46px'); 
 	$(this).css('width','69px'); 
 	$(this).css('padding','0px'); 
  }
  );
  $("#snailmail-1").hover(
    function () {
 	$(this).css('height','44px'); 
 	$(this).css('width','48px'); 
 	$(this).css('padding','1px'); 
  },
    function () {
 	$(this).css('height','46px'); 
 	$(this).css('width','50px'); 
 	$(this).css('padding','0px'); 
  }
  );
  $(".brushes-combs").hover(
    function () {
 	$(this).css('padding','3px 1px 1px 3px'); 
  },
    function () {
 	$(this).css('padding','2px'); 
  }
  );
  $(".addtocart1").hover(
    function () {
 	$(this).css('padding','2px 0px 0px 2px'); 
  },
  function () {
 	$(this).css('padding','1px'); 
  }
  );
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

// DOCUMENT READY
$(document).ready(function(){
  // CORNERS
  DD_roundies.addRule('.comb-brush', '20px', true);
  DD_roundies.addRule('#hair-detangling', '20px', true);
  DD_roundies.addRule('#hair-salon-tools', '20px', true);
  DD_roundies.addRule('.formLayout', '20px', true);
  // LINKS
  var anchors = document.getElementsByTagName("a"); 
  for (var i=0; i<anchors.length; i++) { 
    var anchor = anchors[i]; 
    // EXTERNAL
    if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "external" || anchor.getAttribute("rel") == "external nofollow" )){ 
      anchor.target = "_blank"; 
    }
    // ADD2FAVS
    if ( anchor.className == "add2Favs" ){
      anchor.onclick = function(){
        var titlef = document.title;
        var urlf = location.href;
        if (window.sidebar) { 
          window.sidebar.addPanel(titlef, urlf,""); 
        } 
        else if( document.all ) {
          window.external.AddFavorite( urlf, titlef);
        } 
        else if( window.opera && window.print ) {
          return true;
        }
        return false;
      }
    }
    // ADD2SOCIAL
    if ( anchor.className == "add2Social" ){
      anchor.onclick = function(){
        var titlef = document.title;
        var urlf = location.href;
        window.open('http://www.addthis.com/bookmark.php?v=250&username=&url='+encodeURIComponent(urlf)+'&title='+encodeURIComponent(titlef)+'','_blank');
        return false;
      }
    }
  } 
  // TWITTER UPDATE INIT 
  if ( document.getElementById('web30-twitter-4') ) {
	loadSearch1();
	setInterval("loadSearch1()",45000);
  }
  // BLOG UPDATE INIT
  if ( document.getElementById('blog-ajax-1') )
  {
	loadBlog1();
	setInterval("loadBlog1()",45000);
  }
  // ANIMATE
  animate1();
});

/***********************************************************************************************************************
* ON WINDOW LOAD
************************************************************************************************************************/

// FORM FOCUS
sfFocus = function() { 
  var sfEls1 = document.getElementsByTagName("input"); 
  for (var i=0; i<sfEls1.length; i++) { 
    if( (sfEls1[i].getAttribute("type") != "checkbox") &&
        (sfEls1[i].getAttribute("type") != "radio") )
    {
      sfEls1[i].onfocus=function() { this.className="sfFocus"; } 
      sfEls1[i].onblur=function() { this.className="formbox"; } 
    } 
  } 
  var sfEls2 = document.getElementsByTagName("select"); 
  for (var i=0; i<sfEls2.length; i++) { 
    sfEls2[i].onfocus=function() { this.className="sfFocus"; } 
    sfEls2[i].onblur=function() { this.className="formbox"; } 
  } 
  var sfEls3 = document.getElementsByTagName("textarea"); 
  for (var i=0; i<sfEls3.length; i++) { 
    sfEls3[i].onfocus=function() { this.className="sfFocus"; } 
    sfEls3[i].onblur=function() { this.className="formbox"; } 
  } 
} 
if (window.attachEvent) window.attachEvent("onload", sfFocus); 

function loaded3(){
	$("h1").addClass("spotlight");
	$('#Contact_Name').focus();
}

function loaded2(){
	$('#bone-combs-hair-brushes-overlay').attr('class', 'hide');
	setTimeout('loaded3()', 500);
}

function loaded1(anchor){
	var headerOffset = 157;
	var url = document.location.toString();
	var anchor = url.split('#')[1];  
	var anchorTop = $("#"+anchor).offset().top - headerOffset;
	$('html,body').animate({ scrollTop: ( anchorTop ) }, 500, 'linear', function() {
		setTimeout('loaded2()', 500);
	}); 
}

// WINDOW LOAD
$(window).load(function() {  
	$('body').css('backgroundColor','#cc0099');
	$('#bone-combs-hair-brushes-container').attr('class', 'show');
	$('#bone-combs-hair-brushes').animate({
		top: "0px"
  	}, 500, 'swing', function() {
		//
		$('#combs-brushes-1').animate({
			paddingTop: '137px'
	  	}, 500, 'swing', function() {
			//
			var url = document.location.toString();
			var anchor = url.split('#')[1];  
			if ( document.getElementById(anchor) ) {
				setTimeout('loaded1()', 500);
			}else{
				setTimeout('loaded2()', 500);
			}

		});
		//
	});
});


/***********************************************************************************************************************
* ON WINDOW UNLOAD
************************************************************************************************************************/

$(window).bind('beforeunload', function() { 
  	$(window).scrollTop(0); 
	$('#bone-combs-hair-brushes-overlay').attr('class', 'show');
});

/***********************************************************************************************************************
* ON WINDOW RESIZE
************************************************************************************************************************/

$(window).resize(function() {
  var pageWidth = $(document).width();
  var viewportWidth = $(window).width();
  if ( pageWidth > viewportWidth ) {
    $('body').css('width',pageWidth);
  }else{
    $('body').css('width','auto');
  }
});


