var isNewsletter = false ;
$(document).ready(function() {
    set_basket_position();
    bind_event_update_minibasket();
    //$(".shop-bag").click(function(){showMiniBasket(false)});
    $("#country_select").change(function(){
		window.location.replace($(this).val());
    });

	$('#newsletter-email, #newsletter-email2').keypress(function(e) {
		if(!e) e = window.event;
		key_code = (e.keyCode ? e.keyCode : e.which);
		if(key_code == 13) {
	        email = $.trim($(this).val());
	        if(!validEmail_home(email)){
	            show_popup(msg_error,"The email format is invalid!");
	        } else {
            	$("#strEmail").val(email);
	            scroll(0,0);
	            show_newsletter();
	        }
	        return false;
		}
	})


	$(".top_menu").each(function(){
		if($(this).hasClass('current')) {
			$(this).mouseover(function(){
				$(this).addClass('pos-man');
				id = $(this).attr("rel");
				$("#"+id).show();
			})
			$(this).mouseout(function(){
				$(this).removeClass('pos-man');
				id = $(this).attr("rel");
				$("#"+id).hide();
			})
			$(".s-popup").mouseover(function(){
				$(this).show();
				id = $(this).attr("rel");
				if($("#"+id).attr('current_link') == '1') $("#"+id).addClass('pos-man');
				else $("#"+id).addClass('pos-man');
			})
			$(".s-popup").mouseout(function(){
				$(this).hide();
				id = $(this).attr("rel");
				if($("#"+id).attr('current_link') == '1') $("#"+id).removeClass('pos-man');
				else $("#"+id).removeClass('pos-man');
			})
		} else {
			$(this).mouseover(function(){
				$(this).addClass('current pos-man');
				id = $(this).attr("rel");
				$("#"+id).show();
			})
			$(this).mouseout(function(){
				$(this).removeClass('current pos-man');
				id = $(this).attr("rel");
				$("#"+id).hide();
			})
			$(".s-popup").mouseover(function(){
				$(this).show();
				id = $(this).attr("rel");
				if($("#"+id).attr('current_link') == '1') $("#"+id).addClass('pos-man');
				else $("#"+id).addClass('current pos-man');
			})
			$(".s-popup").mouseout(function(){
				$(this).hide();
				id = $(this).attr("rel");
				if($("#"+id).attr('current_link') == '1') $("#"+id).removeClass('pos-man');
				else $("#"+id).removeClass('current pos-man');
			})
		}
	});
    if(isNewsletter) show_newsletter();

    $("#history-back").click(function(){
		if(document.referrer != '') {
			window.location.replace(document.referrer);
		} else {
			window.location.replace(site_url);
		}
    })
});

function set_basket_position(){
    //mini basket popup should show below mini-sum, and next to right side
    padding = 11;
//    _left = $('#wrapper').width() - $('#mini_basket').width() + $('#wrapper').offset().left - padding;
    _left = 660;
    _top = 20;//$('.shop-bag').offset().top + $('.shop-bag').height() + padding;
    $('#mini_basket').css({'top':_top, 'left':_left});
}

function bind_event_update_minibasket(){
    $("#close-mini").click(hideMiniBasket);
    $("#mini_to_basket").click(function(){window.location = site_url+'basket';});
    // $("#mini_to_checkout").click(function(){window.location = shop_secure_site_url+'/checkout';});
    $("#mini_to_checkout").click(function(){window.location = secure_site_url+'/checkout';});
}

function validEmail_home(str){
    var emailRegEx = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if(str.match(emailRegEx)) return true;
    return false;
}


