$(document).ready(function() {
	
	/* js is enabled, remove the "no-js" class from the header and replace with "js" */
	var d = document.documentElement;
	d.className = d.className.replace(/\bno-js\b/,"js");
	
	$('a.menu').mouseover(function() {
		if ($('.alert-message').is(':visible')) {
			return;
			//closeStatusProgressBar('alert-message');
		}
		$('a.menu').parent("li").removeClass('open');
		$(this).parent("li").toggleClass('open');
		$("#login-email").focus();
	});
	$('ul.menu-dropdown').mouseleave(function() {		
		$(this).parent("li").removeClass('open');
	});
	$('a.menu').mouseleave(function(e) {
		offsetTop = $(this).offset().top;
		height = $(this).height();
		if (e.pageY < offsetTop + height) {
			$(this).parent("li").removeClass('open');
		}
	});
//	if ($('.container form.form-stacked').length > 1) {
//		element = $('.container form.form-stacked div.input').first();
//		$(element).focus();
//	}
	
	//Country selector
	$('#country_selector').bind('change',function() {
		var country = $(this).val();
		$.ajax({
			url: '/ajax_json/change_country?c=' + country,
			dataType: 'json',
			async: false,
			type: 'GET',
			success:function(response){
				if(response.alert!==undefined) alert(response.alert);
				else document.location = response.redirect;
			}
		});	
	});

	$('.container form#checkout-3-form').submit(function() {
		$('.container form#checkout-3-form button').attr('disabled', 'disabled'); $('.container form#checkout-3-form button').addClass('disabled');
	});
	
	function load_feed(nextFeed){
		$('#news-box').html($('#ajax-loader').html());
		$.getJSON('/ajax_json/get_news_feed?next_feed=' + nextFeed, function(data) {
			$('#news-box').html('<h2 class="inline">' + $.fn.jquery_lang('home-whats_new') + '</h2>&nbsp;&nbsp;<a class="title inline" href="'+data.url+'">'+data.title+'</a></h2>' + data.description);
		});
	}

	if ($('#front-box ul.slider').length > 0) {
		$('ul.slider').snuko_slider({left:'control-front-box-left', right:'control-front-box-right', auto: true});

		var nextFeed = 1;

		$('#news-box-left-control, #news-box-right-control').click(function() {
			this.id == 'news-box-left-control' ? --nextFeed : ++nextFeed;
			load_feed(nextFeed);
		});

		load_feed(0);
	}
});
	
function setNotification(msg, status, icon) {
	$(".alert-message").removeClass('error').removeClass('info').removeClass('success');
	$(".alert-message").addClass(status);
	$(".alert-message .alert-icon").removeClass('success_icon').removeClass('ajax_loader').removeClass('error_icon');
	$(".alert-message .alert-icon").addClass(icon);
	message = '<ul>';
	
	for (i = 0; i < msg.length; i++) {
		message += '<li>' + msg[i] + '</li>';
	}
	message += '</ul>';
	$(".alert-message .message").html(message);
	$(".alert-message").fadeIn("normal");
}

function setNotification2(msg, status) {
	$(".block-message").removeClass('error').removeClass('info').removeClass('success');
	$(".block-message").addClass(status);
	message = '<p>';
	
	for (i = 0; i < msg.length; i++) {
		message += '<strong>' + msg[i] + '</strong>';
	}
	message += '</p>';
	$(".block-message").html(message);
	$(".block-message").fadeIn("normal");
}

function closeStatusProgressBar(type) {
	$("." + type).fadeOut("normal");
}

/* Popup Start */

function showPopup() {
	$(function() {
		$("#dialog").dialog({
			bgiframe: true,
			width: '200',
			height: 'auto',
			resizable: false,
			autoOpen: false,
			modal: true,
			title: "",
			open: function(event, ui) {
//				$("body").css("overflow", "hidden");
				//$("body").css("overflow", "hidden");
			},
			close: function(event, ui) {
				cancel();
			}
		});
		$('#dialog').dialog('open');
	});
}

function requestPopup(obj) {
	cancel();
	$('#dialog').html($('#ajax-loader').html());
	showPopup();
	url = $(obj).attr('href');
	$('#dialog').load(url, function(responseText) {
		$('#dialog').dialog('option', {
			'width': $('#dialog #popup-width').width()
		});
		$('#dialog').dialog('option', {
			'position': 'center'
		});
	});
}

function cancel(refresh) {
	$('#dialog').html('');
	$('#dialog').dialog('destroy');
	if (typeof refresh != 'undefined') {
		if (window.location.href.indexOf('dashboard') >= 0) {
			window.location = refresh;
		}
	}
//	$("body").css("overflow", "auto");

	//$("body").css("overflow", "auto");

	return false;
}

function popupLink(obj) {
	url = $(obj).attr('href');
	window.location = url;
	return false;
}
/* Popup End */

function submitJSONForm(msg, obj) {
	setNotification2([msg], 'info');
	formId = '#' + obj.id;
	$(formId + ' div').removeClass('error');
	$.ajax({
		url: $(obj).attr('action'),
		dataType: 'json',
		async: false,
		type: 'POST',
		data: $(obj).serialize(),
		success:function(response){
			$(formId + ' div').removeClass('error');
			if (response.bad_fields) {
				$.each(response.bad_fields, function(index, value) {
					var id = '#' + value + '-field';
					$(id).addClass('error');
				});
				setNotification2(response.messages, 'error');
			}
			if (response.success) {				
				setNotification2([response.success_msg], 'success');
				if (!response.redirect) {
					$(obj).slideUp('slow', function() {
						setTimeout("cancel();", 1000);
					});
				}
			}
			if (response.redirect) {
				document.location=response.redirect;
			}
			
			if(response.remove!==undefined){
				$.each(response.remove, function(index, selector){
					$(selector).remove();
				});
			}
			if(response.html!==undefined){
				$.each(response.html, function(id, value){
					$('#'+id).html(value);
				});
			}			
		}
	});
	return false;
}
