


$(document).ready(function(){
	
	$("#product-holzhackschnitzel").click( function() {
		$('.sublabel').html('srm');
		$('.sub1').html('in Schüttraummeter (srm)');
	});
	$("#product-pellets").click( function() {
		$('.sublabel').html('t');
		$('.sub1').html('in Tonnen (t)');
	}); 
	
	$("#errormail").click( function() {
		$('#form-layer').css('display', 'block');
	});
	$(".close").click( function() {
		 $(this).parent().parent().css('display', 'none');
	});
	
	// MISC
	$(".show-hidden-content").click(function() {
		 $(this).parent().parent().next().toggle();
		 $(this).toggleClass("open");
		 return false;
	});

	$("#content-header-image").bind("contextmenu",function(e){
		return false;
	});

	$("a.colorbox, a.thickbox").colorbox({
		maxWidth: "80%",
		maxHeight: "80%",
		close: "<b>Schlie&szlig;en</b>"
	});

	$(".shopselect").click(function(e){
		x = e.pageY-285;
		$("#standort-layer").css('top', x+'px');
		$("#standort-layer").show();
		return false;
	});
	
	$(".shopselect1").click(function(e){
		x = e.pageY+20;
		$("#standort-layer").css('top', x+'px');
		$("#standort-layer").show();
		return false;
	});
	

	// CONTENT BLOCKS
	$(".image-slider .image-slider-images img").live('click', function() {
		//$(".image-slider .image-slider-images").trigger('slideTo', this);
		$("#" + $(this).attr('rel')).show().siblings().hide();
	});

	$(".arrow-forward", ".gallery-container").click( function() {
		if( $(".gallery-image .container img:visible").next().length > 0 )
		{
			//console.log('next');
			showGalleryImage( $(".gallery-image .container img:visible").next() );
		}
		else
		{
			//console.log('first');
			showGalleryImage( $(".gallery-image .container img").first() );
		}
		return false;
	});

	$(".arrow-back", ".gallery-container").click( function() {
		if( $(".gallery-image .container img:visible").prev().length > 0 )
		{
			//console.log('prev');
			showGalleryImage( $(".gallery-image .container img:visible").prev() );
		}
		else
		{
			//console.log('last');
			showGalleryImage( $(".gallery-image .container img").last() );
		}
		return false;
	});

	function showGalleryImage( img )
	{
		$(img).show().siblings().hide();
		$('.image-description').text( $(img).attr('alt') );
	}




	// CONTROLLER

	// standort
	function checkStandort( url ) {

		if( !$.cookie('standort') )
		{
			$('body').data('shopUrl', url );

			$.colorbox({
				href:'/standorte?ajax=true',
				innerWidth:"500",
				innerHeight:"420",
				iframe: false
			});

			return false;
		}

		return true;
	}

	function openShopUrl() {
		if( $('body').data('shopUrl') ) {
			if( $('body').data('shopUrl') == 'login' ) {
				$.colorbox.close();
				$("#login").submit();
			} else {
				location.href = $('body').data('shopUrl');
			}
		}	
	}


	$('a.shop-link').live('click', function(){
		if( checkStandort( $(this).attr('href') ) ) {
			if( parent )
				parent.location.href = $(this).attr('href');
			else
				location.href = $(this).attr('href');
		}
		return false;
	});

	$('select[name=standort-teaser]').change( function() {
		$.cookie('', $('option:selected', this).attr('rel'), {expires:1000});
	});
	$('select[name=standort-ajax]').livequery('change', function() {
		$.cookie('standort', $('option:selected', this).attr('rel'), {expires:1000});
		openShopUrl();
	});
	$(".location-list li.ajax a").livequery('click', function() {
		$.cookie('standort', $(this).attr('rel'), {expires:1000});
		openShopUrl();
		return false;
	});

	$('select[name=standort]').change( function() {
		location.href = $(this).val() ;
		//window.open($(this).val(),"" , "width=1024,height=700,left=100,top=100,resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,location=yes");
		return false;
	});
	

	$('#standort').live('submit', function() {
		var post = $('#standort').serializeArray();
		$.ajax({
			url: '/opencart/?route=checkout/standort',
			data: post,
			dataType: 'json',
			beforeSend: function(){
				$('.location-list, .location-list li, .location-list .failure').addClass('hidden-content');
			},
			statusCode: {
				200: function(data){
					$.each(data.data, function(index, value){
						$('.location-list li#standort-' + value.standort ).removeClass('hidden-content');
					});
					$('.location-list').removeClass('hidden-content');
				},
				204: function(){
					$('.location-list .failure').removeClass('hidden-content');
					$('.location-list').removeClass('hidden-content');
				}
			},
			error: function(){
				$('.location-list .failure').removeClass('hidden-content');
				$('.location-list').removeClass('hidden-content');
			}
		});

		return false;
	});


	$('#btn-plz-search').click( function() {
		$(this).parent('form').submit();
		return false;
	});
	$(".location-list li").click(function() {
		//window.open($(this).attr('rel'), "sstandort", "width=1024,height=700,left=100,top=100,resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,location=yes");
		 location.href = $(this).attr('rel');
		return false;
	});
	

	// Anfrage
	$('#btn-add-product').click( function(e) {
		e.preventDefault();
		self = $(this);
		$.ajax({
			url: self.attr('href'),
			method: 'POST',
			statusCode: {
				200: function(data){
					alert('hier' + data);
				}
			},
			error: function(){
				alert('fail');
			}
		});
	})





	// TEASER

	$('.direct-access .btn-info').click( function() {
		if( $(".direct-access select").val() ) {
			location.href = $(".direct-access select").val();
		}
	});
	$('.direct-access .btn-shop').click( function() {
		if( $(".direct-access select").val() ) {
			$.cookie('standort', $('.direct-access select option:selected').attr('rel') );
			location.href = '/opencart/';
		}
	});

	$('.teaser a.video').colorbox( {
		innerWidth:"600",
		innerHeight:"340",
		scrolling: false,
		html:'<div id="holzarena-video"></div>',
		onComplete: function () {
			swfobject.embedSWF(
				"/flash/f4player.swf",
				"holzarena-video",
				"600",
				"340",
				"9.0.0",
				"expressInstall.swf",
				{
					skin: '/flash/skins/mySkin.swf',
					thumbnail: $(this).attr('thumb'),
					video: $(this).attr('href')
				}
			);
		}
	});


	// openCart
	$('.button-submit').click(function(){
		$(this).parents('form').submit();
		return false;
	});

	$('#cash').click(function(){
		if ($(".Lieferung:checked").val() == "manufacturer.manufacturer"){
			alert('Sie können nur bei Abholung Bar bezahlen.\nDie Lieferart wird geändert.');
			$('.Lieferung').attr('checked', false);
			$('.Abholung').attr('checked', true);
		}
	});
	$('.Lieferung').click(function(){
		if ($("#cash:checked").val() == "cash"){
			$('#cash').attr('checked', false);
			alert('Sie können nur bei Abholung Bar bezahlen.\nBitte wählen Sie eine andere Zahlungsart.');
		}
	});
	
	



	$("a.anchor-login").click(function() {
		$("#login-container").toggleClass("open")
		return false;
	});

	$("#login-container button").click( function() {

		var warning = [];
		/*if( $("#login select[name=shop]").val() == '' )
		{
			warning.push('Bitte wählen Sie Ihren <b>Standort</b>');
		}*/

		if( $("#login-container input[name=email]").val() == '' )
		{
			warning.push('Bitte geben Sie Ihre <b>E-Mail</b> an');
		}
		if( $("#login-container input[name=password]").val() == '' )
		{
			warning.push('Bitte geben Sie Ihr <b>Passwort</b> an');
		}

		if( warning.length == 0 && checkStandort( 'login' ) )
		{
			$("#login-navi").submit();
		}
		else
		{
			$("#login-warning .warning").html( warning.join('<br>') ).show();
		}

		return false;
	})

	//$('#login-navi input:not([type=hidden])').defaultValue();

 });
 

  function sendtextorder(){
 	var message = $('textarea[name=mtext]');
	var data = 'mtext=' + escape(message.val());
	$.ajax({
			url: "http://www.holzarena.de/opencart/sendanfrage.php",
			type: "GET",	
			data: data,
			success: function (reqCode) {
				if (reqCode==1) {
					$('#form').hide();
					$('#antwort').fadeIn('slow');
				} else{
	          		alert(reqCode);
				}
		}
	});
		 
 }
 




