$(document).ready(function()
{
    function addMega(){
        $(this).addClass("hovering");
        }

      function removeMega(){
        $(this).removeClass("hovering");
        }

    var megaConfig = {
         interval:50,
         sensitivity: 1,
         over: addMega,
         timeout: 100,
         out: removeMega
    };

    

    $("li.mm").hoverIntent(megaConfig)


	/* orders, buy - prices */
	$('a.buy_bc').append(' - '  + currency + ' ' + price_bc + '');
	$('a.buy_lc').append(' - ONLY '  + currency + ' ' + price_lc + '');
	//$('a.buy_lcb').append(' - '  + currency + ' ' + price_lcb + '');
	$('a.buy_pearly').append(' - '  + currency + ' ' + price_pearly + '');


	/* packlist popup */
	$('div.form_row a.order_packlist').click(function(){

	    var href = $(this).attr('href');

	    $("#dialog_wrapper").dialog({
		    title: '',
		    modal: true,
		    overlay: {
			    backgroundColor: '#333333',
			    opacity: 0.3
		    },
		    buttons: {
			    'CLOSE': function() {
			    $(this).dialog('destroy');
			    }
		    },
		    height: 440,
		    width: 740
		    });

	    $('#dialog_wrapper').load(href);

	    return false;
	});

	
	/* faq links */
	$('.faq_link').click(function(){
		var id = $(this).attr('id');
		$('div.answer').hide();
		$('#holder_' + id).toggle();
		return false;
	});

	/* downloads */
	$('.download').hover( function(){
	     $(this).addClass('download_hover');
	},
	function(){
	     $(this).removeClass('download_hover');
	});


	
	/**/
	$('a.gm_disabled').click(function(){
		var href = $(this).attr('href');
		//alert(href);


		$("#dialog_wrapper").dialog({
	   	  	title: '',
	   	  	modal: true,
			overlay: {
				backgroundColor: '#333333',
				opacity: 0.3
			},
			buttons: {
				'CLOSE': function() {
				$(this).dialog('destroy');
				}
			},
			height: 440,
			width: 740
			});

		//$('#dialog_wrapper').html('<iframe src="' + href + '" frameborder="0" width="670" height="360" scrolling="no" marginheight="0" marginwidth="0" ></iframe>');

		$('#dialog_wrapper').html(' <div id="map_canvas" style="width: 740px; height: 401px; overflow: hidden; scrolling: none;"></div>');

		var map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(47.604832,11.18597), 14);
		map.setUIToDefault();



		return false;
	});
	
	/**/
	$('a.compare').click(function(){
		var href = $(this).attr('href');
		//alert(href);


		$("#dialog_wrapper").dialog({
	   	  	title: '',
	   	  	modal: true,
			overlay: {
				backgroundColor: '#333333',
				opacity: 0.3
			},
			buttons: {
				'OK': function() {
				$(this).dialog('destroy');
				}
			},
			height: 440,
			width: 720
			});

		$('#dialog_wrapper').load(href);
		
		return false;
	});



	/* coupon codes */
	$('#coupon_code').keyup(function(){
		
		var coupon_code = ($(this).val());

		$.ajax({
			url: '/en/ajax/check_coupon_code/' + coupon_code + '/json',
			type:"POST",
			data:"service=" + 'default',
			dataType: "json",
			success: function(result) {
			   if(true==result['status'])
			   {
					$('div#coupon_code_wrapper').load('/en/ajax/check_coupon_code/' + coupon_code);

					$('#coupon_code').addClass('valid');
					$('#coupon_code').removeClass('invalid');
					
			   } else {
				   //alert(result['message']);
				   
					$('div#coupon_code_wrapper').load('/en/ajax/check_coupon_code/' + coupon_code);
					
					//$('#coupon_code').addClass('invalid');
					$('#coupon_code').removeClass('valid');
			   }
			}
		});

		
		
	});
	

	
	$('#pmethod').change(function(){
		pm_display($(this).val());
	});

	function pm_display(pm)
	{
		$('div.pm').hide();
		$('div#pm_' + pm).show();
	}
	
	$('div.pm').hide();
	if($('#pm_h').val())
	{
		pm_display($('#pm_h').val());
	}

	// country selector / states field
	$('#country').change(function(){
		country_display($(this).val())
	});
	function country_display(country)
	{
		$('div.select_state').hide();
		if(country == 'us')
		{
			$('div.select_state').show();
		}
	}
	$('div.select_state').hide();
	if($('#country').val())
	{
		country_display($('#country').val());
	}

	$('#billing_country').change(function(){
		billing_country_display($(this).val())
	});
	function billing_country_display(country)
	{
		//alert(country);
		$('div.select_billing_state').hide();
		if(country == 'us')
		{
			$('div.select_billing_state').show();
		}
	}
	$('div.select_billing_state').hide();
	if($('#billing_country').val())
	{
		billing_country_display($('#billing_country').val());
	}

	$('#cb_billing_like_delivery').change(function(){
		billing_like_delivery($(this).attr('checked'))
	});
	
	function billing_like_delivery(val)
	{
		if(val == true)
		{
			$('#billing_like_delivery').val('1');
			$('#group_billing_address').addClass('disabled');
			$('#group_billing_address').css('display', 'none');
		} else {
			$('#billing_like_delivery').val('0');
			$('#group_billing_address').removeClass('disabled');
			$('#group_billing_address').css('display', 'block');
		}
	}

	if($('#billing_like_delivery').val() == '1') 
	{
		$('#cb_billing_like_delivery').attr('checked', true);
		$('#group_billing_address').addClass('disabled');
		$('#group_billing_address').css('display', 'none');
	} else {
		$('#cb_billing_like_delivery').attr('checked', false);
		$('#group_billing_address').removeClass('disabled');
		$('#group_billing_address').css('display', 'block');
	}
		
	


	// select shipping
	$('#service').change(function(){
		var val = $(this).val();

		$.ajax({
			url: '/en/ajax/set_service',
			type:"POST",
			data:"service=" + val,
			dataType: "json",
			success: function(result) {
			   if(true==result['status'])
			   {
					window.location.reload();
			   } else {
				   alert(result['message']);
			   }
			}
		});

		
		return false;
	});


	

	$('.order_step').removeClass('order_step_active');
	if($('#step').val())
	{
		$('.order_step_' + $('#step').val()).addClass('order_step_active');
	}



	$('a.submit').click(function(){
		$('#order_form').submit();
		return false;
	});

	

	/* disable form elements */
	$('div.disabled').css("display", 'none');

	$( '#region_scroll' ).css('position', 'relative');
	

	$('#submenu li').hover(
			function() { $(this).addClass('hoverb'); hover_intent = setTimeout( function(){ $('#submenu .hover').find('.off').fadeOut(100); } , 10 ); },
			function() { try{clearTimeout(hover_intent);}catch(e){}; $('#submenu .hover').find('.off').fadeIn(100); $(this).removeClass('hoverb'); }
			);


	// wrapper hight
	$('div#intro').height($("div.wrapper").height()-140);


	/* subscriptions */
	$('a#subscribe').live('click', function(event) {
		var href = $(this).attr('href');
		var email = encodeURIComponent($('#email').val());

		/* get checkbox values */
		if ($('#cb_venews').attr('checked'))
		{
			var venews = 1;
		} else {
			var venews = 0;
		}
		if ($('#cb_news').attr('checked'))
		{
			var news = 1;
		} else {
			var news = 0;
		}
		if ($('#cb_offers').attr('checked'))
		{
			var offers = 1;
		} else {
			var offers = 0;
		}
		
		// load div, not the ultra-sexy method... 
		
		$('div.infobox').load('/ajax/subscribe/' + email + '/' + venews + '/' + news + '/' + offers + '/' );
		
		return false;
	});



	// comparison table
	var img_yes = '/media/img/icons/compare_yes.png';
	var img_no = '/media/img/icons/compare_no.png';
	$('table.compare td.indicator.ind_yes').html('<img src="' + img_yes + '" />');
	$('table.compare td.indicator.ind_no').html('<img src="' + img_no + '" />');





	// posts - fancybox
	$(".tooltip a").tooltip({
		bodyHandler: function() {
			return $($(this).attr("href")).html();
		},
		showURL: false
	});
	
	
	
	/* order now */
	
	$("a.menu_pulse").pulse({
	    opacity: [0.2,1]
	}, {
	    duration: 500,
	    times: 999,
	    easing: 'linear'

	});	
	
	
    
});

//$.swapImage(".swapImage", true, true, "mouseenter", "mouseleave");
