$(document).ready(function() {

	if(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) {
		$('#menu-list li').hover(function(){
			$('select').hide();
		}, function(){
			$('select').show();
		});
	}
	
	//index promo hover opacity
	$(".homepage-banners a img, #product-carousel li a img, .inspiration-image-medium a, .inspiration-image-small a img, .inspiration-trend-box a img").hover(function() {
		$(this).stop().animate({"opacity": "0.7"}, "fast");
	},
	function() {
		$(this).stop().animate({"opacity": "1"}, "fast");
	});
	
	//show shop selector on hover
	$('div#topbar div.active').hover(function() {
		$(this).children('.menu').css('display','block');
	},function() {
		$(this).children('.menu').css('display','none');
	});
        
	//show language selector on hover
	$('div#topbar div.language').hover(function() {
		$(this).children('.menu').css('display','block');
	},function() {
		$(this).children('.menu').css('display','none');
	});
	
	//fancybox modal settings
	$(".do_overlay").fancybox({
		'hideOnContentClick': false,
		'scrolling' : 'no',
		'centerOnScroll' : true
	});
    
	//setup toggleable menu
	if($('.left-categories').length) {		
		$('.left-categories ul').hide();
		$('.left-categories a.has_child').click(function() {
			($(this).hasClass('extended') ? $(this).removeClass('extended') : $(this).addClass('extended'));
			$(this).next().slideToggle('normal');
		});
		
		$('.left-categories a.active').addClass('extended');
		$('.left-categories a.active').next().slideToggle('normal');
	}
	
	//setup carousel
	if($("#jcarousel").length) {
		$('#jcarousel').jcarousel({
			'scroll' : 6,
			'wrap' : 'circular'
		});
	}
	
	if($(".homepage-banners").length) {
		$(".homepage-banners img:last").css('margin', '0');
	}
	
	//begin ajax search on field input
    $('input[name="q"]').liveSearch({url: '/lib/requests/search_results.php?q='});
    
    //use title tag as form hints on all input fields
	jQuery(document.body).formHints();
	
	if($('img.lazy').length) {
		$('img.lazy').asynchImageLoader({
			offset:160
		}).asynchImageLoader({
			event: "focus"
		});
	}
});
    
/**
* Add products to cart using AJAX calls
*/
function add_to_cart(id, color, size, quantity) {

	var cart_url = '/checkout/cart';
	var params = "id=" + id + "&color=" + color + "&size=" + size + "&quantity=" + quantity;
			
	$.ajax({
		type: "GET",
		url: "/lib/requests/add_to_cart.php",
		data: params,
		success: function(obj){
			var obj = jQuery.parseJSON(obj);
					
			//update cart status
			$.ajax({
				type: "GET",
				url: "/lib/requests/update_cart_status.php",
				data: '',
				success: function(response){
					var response = jQuery.parseJSON(response);
							
					$('html,body').animate({scrollTop: 0}, 400);
							
					$("#cart_items").html(response.in_cart+' '+(response.in_cart == 1 ? (global_language = 'da' ? 'Vare' : 'Item') : (global_language = 'da' ? 'Varer' : 'Items')));
					$("#cart_price").html(response.cart_value);
							
					//animate bag
					$("#shopping_bag").fadeIn(100).animate({top:"-=10px"},300).animate({top:"+=10px"},300).animate({top:"-=10px"},300).animate({top:"+=10px"},300).animate({top:"-=10px"},300).animate({top:"+=10px"},300);
				}
			});	
		}
	});
}
	
/**
* Reset modal window countdown (automatic cart redirect)
*/
function reset_modal_countdown() {
	$('#countdown_dashboard').stopCountDown();
	$('#countdown_dashboard').setCountDown({targetOffset: {'day': 0, 'month': 0, 'year': 0, 'hour': 0, 'min': 0,'sec': 15}});
}

/**
* Check newsletter subscription using AJAX calls
*/
function subscribe() {
		
	if(email = $("#newsletter").val()) {

		$.ajax({
			type: "GET",
			url: "/lib/requests/subscribe_ubivox.php",
			data: "email="+email,
			success: function(response) {
				
				var response = jQuery.parseJSON(response);
				if(response.message) alert(response.message);
				$("#newsletter").val('');
				
				return true;
			}
		});
	}
}

function confirm_submit(dialog) {
	
	return (confirm(dialog) ? true : false);
}
