 // Detect touch input
 var is_touch_device = 'ontouchstart' in document.documentElement;
 
 //Keep the cart count updated
 setInterval('updateCartTotal()', 2000)
 setInterval('big_screen()', 500)

 // Detect Big Screen
 function big_screen() {
 	return $(document).width() > 1300;
 }
 
 $(document).ready(function() {
 	moveSocialLinks();
 });

 $(window).resize(function() {
 	moveSocialLinks();
 });

 // $(window).scroll(function() {
 // 	if ($(window).scrollTop() > $('#productdetaildetailscontent').offset().top) {
 // 		$("#travel-bookmarks").show();
 // 	} else {
 // 		$("#travel-bookmarks").hide();
 // 	}
 // });

 if(!is_touch_device) { //Disable Hover functionality if this is a touch device
 	$('.category, .subcategories').live('mouseenter', function() {  //Hover a category
	  var holder = $(this).attr('id');
	  var subcat = ".subcategories#" + holder
	  $(subcat).show();
	  $('#top-sale').stop(true, true).delay(100).slideUp(); //Hide the "Sale" tab
	 });

	 $('.category, .subcategories').live('mouseleave', function() {
	  var holder = $(this).attr('id');
	  var subcat = ".subcategories#" + holder
	  $(subcat).hide();
	  $('#top-sale').stop(true, true).delay(100).slideDown();
	 });
 }

 $('.category').live('click', function(){ //Switch for touch functionality
 	if (is_touch_device) {
 		var holder = $(this).attr('id');
	  var subcat = ".subcategories#" + holder
	  $('.subcategories').not(subcat).hide();
	  $(subcat).slideToggle(200);
 	} else {
 		location.href = $(this).attr("data-link");
 	}
 });

 $('#logo').live('click', function(){
 	location.href = '/index.cfm';
 });

 $('#top-sale').live('click', function(){
 		location.href = '/sale_page.cfm';
 });

 $('#searchbar').live('focus', function() {
 	if ($(this).attr('value') == 'Search...') {
 		$(this).attr('value', '');
 		$(this).attr('class', '');
 	}
 })

 $('#searchbar').live('blur', function() {
 	if ($.trim($(this).attr('value')) == '') {
 		$(this).attr('value', 'Search...');
 		$(this).attr('class', 'watermark');
 	}
 })


// SLIDESHOW FOR HOMEPAGE
function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5500 ); //Interval for the slides
});

$("#slideshow img").live('click', function(){
	location.href = $(this).attr("data-link");
});

$("#cart-popper, #close-popup-cart").live('click', function() {
	$('#popup-cart').slideToggle(200);
});

$(document).ready(function() {
	updateCartTotal();
});

function updateCartTotal() {
	var quantityCounter = 0;
	$(".productcartquantity").each(function() {
		quantityCounter = quantityCounter + parseInt($(this).attr("value"));
	});
	$("#cartQtyTotal").html(quantityCounter);
}

$("#update.button").live("click", function() {
	updateCart();
});

$('.thumb').live('click', function(){
	if (fileExists($(this).attr("data-large"))) {
		$('#mainImage').attr("src", $(this).attr("data-large"));
	}
	return false;
});

$('.add-this-to-cart').live('click', function() {
	addItemToCart($(this).attr("data-item"))
});

$('.bookmark').live('click', function() {
	goToByScroll($(this).attr("data-link"))
});

function fileExists(url)
{
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
}

function moveSocialLinks() {
	if ($(window).width() > 1300) {
		$('#shareonsocialnetworks').hide();
		$('#side-social').show();
	} else {
		$('#shareonsocialnetworks').show();
		$('#side-social').hide();
	}
}

function goToByScroll(id){
     	$('html,body').animate({scrollTop: $("#"+id).offset().top},2000);
}

$('#choose-shipping').live('change', function(){
	$('#Shipping_Total').html($('#choose-shipping option:selected').attr('data-cost'));
	var total_html = $('#Item_Total').html();
	var total = Number(total_html.replace(/[^0-9\.]+/g,""));
	var ship_ugly = $('#choose-shipping option:selected').attr('data-cost');
	var ship = Number(ship_ugly.replace(/[^0-9\.]+/g,""));
	var new_total = '$' + (ship + total).toFixed(2);
	$('#Total_Total').html(new_total);
})

$('#brand-picker').live('change', function() {
	location.href = '/' + $('#brand-picker option:selected').attr("value")
});


// $(document).ready(function() {
// 	$("#scroller").scrollable().navigator();
// });
