/**
 * Function
 * https://github.com/diegoguevara/onHashChange-jQuery-Plugin/blob/master/onhashchange.js
 */
(function($){
    var _actualHashValue = '';
    $.fn.hashChange = function(hashChangeFunction){
        if ("onhashchange" in window){
            $(window).bind( "hashchange", function(e) {
                hashChangeFunction();
            });
        }
        else{
            _actualHashValue = window.location.hash;
            setInterval ( function(){
                if ( _actualHashValue != window.location.hash){
                    _actualHashValue = window.location.hash;
                    hashChangeFunction();
                }
            },500 );
        }
    }
})(jQuery);


/**
 * SLIDER
 */
jQuery(function($)
{
	/** PREV /NEXT **/
	navigation();
	$(window).hashChange(navigation);
	$('[href='+window.location.hash+']').click();
	
	/**
	 * CODASLIDER
	 */
	$('#coda-slider-1').codaSlider(
	{
		autoSlide:false,
		dynamicArrows: false,
		dynamicTabs: false
	});

	
	/**
	 * SLIDER
	 */
	$('.box-acitve').each(function(index)
	{
	  $(this).bind("mouseover",function()
		{
	  	$(this).stop(true,false);
	  	$(this).animate({opacity:0},400);
		});
	  $(this).bind("mouseout",function()
		{
	  	$(this).stop(true,false);
	  	$(this).animate({opacity:1},400);
		});
	});
	/**
	 * SLIDER2
	 */
	$('.box-acitve2').each(function(index)
	{
	  $(this).bind("mouseover",function()
		{
	  	$(this).stop(true,false);
	  	$(this).animate({opacity:0},400);
		});
	  $(this).bind("mouseout",function()
		{
	  	$(this).stop(true,false);
	  	$(this).animate({opacity:1},400);
		});
	});
	
	/** FORMULAR **/
	var form =  $('#Name, #Strasse, #Kundennummer_0falls_vorhanden1, #E-Mail-Adresse, #Telefonnumer_0optional1, #Nachricht, #PLZ9_Ort');
	
	form.bind("focusin focusout change click", function(event)
	{
		var name = $(this).attr('id').split("_").join(" ");
		name = name.split("0").join("(");
		name = name.split("1").join(")");
		name = name.split("9").join(",");
		if(event.type != "focusin")
		{
			if($(this).val() == name)
				$(this).val('');
			else if($(this).val() == "")
				$(this).val(name);
			
			if(event.type == "focusout" && $(this).val() == "")
				$(this).val(name);	
		}
	});
	
	$('.qty_source').bind('focusout change',call_calc);
	call_calc();
	
	
	/** IMAGESLIDER **/
	$("ul.slider").cycle(
	{
		fx: 'fade',
		timeout: 3000
	});
	
	/** FANCYBOX **/
	$("a.fancybox").fancybox();
	
	/** WEINLISTE **/
	if($('#weinliste_form'))
	{
		$('#weinliste_form').submit(function(event)
		{
			$('#weinliste').validationEngine('hide');
			$('#ordersubmit').validationEngine('hide');
			
			if(parseInt($('#qty').html()) > 0)
			{
				$('#qty').validationEngine('hide');	
				if($("#weinliste_form").validationEngine('validate'))
				{
					$.ajax({
					  type: 'POST',
					  url: 'request.php',
					  data: $(this).serialize(),
					  success: function(data)
					  {
						if(data==="true")
						{
							$('#weinliste').slideUp("slow");
							$('#weinliste').validationEngine('showPrompt', 'Vielen Dank für Ihre Anfrage.', 'pass', true);
						}
						else
						{
							$('#ordersubmit').validationEngine('showPrompt', 'Anfrage konnte nicht versendet werden.', 'error', true);	
						}
					  },
					  dataType: 'html'
					});
				}
			}
			else
			{
				$('#qty').validationEngine('showPrompt', 'Warenkorb enthält keine Artikel.', 'error', true)	;
			}
			return false;
		});
	}
	
	$(window).hashChange(winlist);
	
	/** KONTAKT **/
	if($('#kontakt_form'))
	{
		$('#kontakt_form').submit(function(event)
		{
			if($("#kontakt_form").validationEngine('validate'))
			{
				$.ajax({
				  type: 'POST',
				  url: 'request.php',
				  data: $(this).serialize(),
				  success: function(data)
				  {
					if(data==="true")
					{
						$('#kontaktsubmit').slideUp("slow");
						$('#kontakt_form').validationEngine('showPrompt', 'Vielen Dank für Ihre Anfrage.', 'pass', true);
					}
					else
					{
						$('#kontakt_form').validationEngine('showPrompt', 'Anfrage konnte nicht versendet werden.', 'error', true);	
					}
				  },
				  dataType: 'html'
				});
			}
			return false;
		});
	}
});

function call_calc()
{
	var total = 0;
	var qty	  = 0;
	$('.qty_source').each(function(index, value)
	{
		//$(this).val();
		if($(this).val() > 0)
		{
			total += parseInt($(this).val()) * parseFloat($(this).attr('id').split("|")[2]);
			qty   += parseInt($(this).val());
		}
		$('#qty').html(qty);
		$('#total').html(total.toFixed(2));
	});
}

function goToByScroll(_anker)
{
	if (arguments.length != 0)
    	$('html,body').animate({scrollTop: $("#"+_anker).offset().top - 220},'slow');
}


function winlist()
{
	/**
	 * WEINLISTE
	 */
	 if(window.location.hash.indexOf('#7-weinliste',0) != -1)
	 {
		 $('#content').animate(
		 {
			 height:197
		 },
		 1000,
		 function()
		 {

		 });
		 $('#weinliste').slideDown("slow");
		 var _anker = window.location.hash.replace('#','');
		 if (_anker != '7-weinliste' && _anker != '')
		 	goToByScroll(_anker);		 
	 }
	 else
	 {
		$('#content').animate( {height:394} );
		$('#weinliste').slideUp("slow");
	 }
}

/** prev/next **/
function navigation()
	{
		$('.panel').each(function(index,value)
		{
			if("#" + (index + 1) == window.location.hash)
			{
				$(this).find('.next').animate({opacity:1},500);
				$(this).find('.prev').animate({opacity:1},500);
			}
			else
			{
				$(this).find('.next').animate({opacity:0},700);
				$(this).find('.prev').animate({opacity:0},700);
			}	
		});

		winlist();
	}

/** GOOGLE **/
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22240628-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
