// JavaScript Document

var bg_slides = new Array();
var ac_bg_slide = 0;
var slide_buttons = "";
var slideshow = null;

if ($.browser.msie) {
	$(window).load(function() {
		init_canvas();
	});
}

$(document).ready(function() { 
	
	$('a').focus(function() { $(this).blur(); });
	$('input.buttonfield').focus(function() { $(this).blur(); });
	
	$("#contactform").submit(function() {
		return check_contact_form();
	});
	
	// Init content
		if($('#content.content').length>0 && !$('#content.content').hasClass("user")) {
			if($("#contentholder").height()<500) {
				
				$("#contentholder").css({ height: 'auto' });
				$("#contentarea").css({ top: parseInt($("#contentarea").css("top"))+(500-parseInt($("#contentarea").height())) });
				
			} else {
				$("#contentarea").css({ top: parseInt($("#contentarea").css("top"))-40 });
				$("#contentholder").css({ height: 500 });
				init_functions();	
				
			}
		}
		
		// Init Press
		if($('#content.press').length>0) {
			
			if($("#contentholder").height()<500) {
				
				$("#contentholder").css({ height: 'auto' });
				$("#contentarea").css({ top: parseInt($("#contentarea").css("top"))+(500-parseInt($("#contentarea").height())) });
				
			} else {
				$("#contentarea").css({ top: 68 });
				$("#contentholder").css({ height: 500, overflow:"hidden" });
				init_functions();	
			}
		}
		
		// Init kollektion
		if($('#content.kollektion').length>0) {
			if($("#contentholder").height()<500) {
				
				$("#contentholder").css({ height: 'auto' });
				$("#contentarea").css({ top: parseInt($("#contentarea").css("top"))+(500-parseInt($("#contentarea").height())) });
				
			} else {
				$("#contentarea").css({ top: parseInt($("#contentarea").css("top"))-40 });
				$("#contentholder").css({ height: 500 });
				init_functions();	
			}
		}
		
		// Init Basket
		if($('#content.basket').length>0) {
			
			if($("#contentholder").height()<500) { 
				
				$("#contentholder").css({ height: 'auto', 'padding-right': 20 });
				$("#contentarea").css({ top: parseInt($("#contentarea").css("top"))+(500-parseInt($("#contentarea").height())) });
				
			} else {
				$("#contentarea").css({ top: 68 });
				$("#contentholder").css({ height: 500, overflow:"hidden" });
				init_functions();	
			}
		}
		
		// Init Order
		if($('#content.order').length>0) {
			
			if($("#contentholder").height()<500) { 
				
				$("#contentholder").css({ height: 'auto', 'padding-right': 20 });
				$("#contentarea").css({ top: parseInt($("#contentarea").css("top"))+(500-parseInt($("#contentarea").height())) });
				
			} else {
				$("#contentarea").css({ top: 68 });
				$("#contentholder").css({ height: 500, overflow:"hidden" });
				init_functions();	
			}
		}
		
	$(".ceebox").ceebox({ titles:false,width: 640,height:480,video:true });
	
	
	if (!$.browser.msie) {
		init_canvas();	
	}
						   
});

function init_canvas() {
	// init navigation
	$("ul.main li.nav").each(function() {
		$(this).children("a").liquidCanvas("[fill{color:rgba(190, 158, 76, 0.8)}] => menurectLeft{radius:6}");						 
	});
	
	$("ul.main li.subnav").each(function() {
		$(this).children("a").liquidCanvas("[fill{color:rgba(0, 0, 0, 0.7)}] => menurectLeft{radius:6}");						 
	});
	
	$("ul.main li.subsubnav").each(function() {
		$(this).children("a").liquidCanvas("[fill{color:rgba(0, 0, 0, 0.7)}] => menurectLeft{radius:6}");						 
	});
	
	$("ul.sub li").each(function() {
		$(this).children("a").liquidCanvas("[fill{color:rgba(0, 0, 0, 0.7)}] => menurectLeft{radius:6}");						 
	});
	
	$("ul.right li").each(function() {
		$(this).children("a").liquidCanvas("[fill{color:rgba(250, 250, 250, 0.7)}] => menurectRight{radius:6}");						 
	});
	
	$("ul.account li").each(function() {
		$(this).children("a").liquidCanvas("[fill{color:rgba(250, 250, 250, 0.7)}] => menurectRight{radius:6}");						 
	});
	
	$("ul.account_buttons li").each(function() {
		$(this).children("a").liquidCanvas("fill{color:rgba(0, 0, 0, .7)} => roundedRect{radius:6}");					 
	});
	
	//init content
	
	
	
	$("#contentholder.h540").liquidCanvas("fill{color:rgba(255, 255, 255, 0.80)} => roundedRect{radius:6}");
	
	$("#contentholder.h540bg").liquidCanvas("fill{color:rgba(255, 255, 255, 1)} => roundedRect{radius:6}");
	
	$("#contentholder.h540w").liquidCanvas("fill{color:rgba(255, 255, 255, 1)} => roundedRect{radius:6}");
	
	$("#contentholder.h540b").liquidCanvas("fill{color:rgba(255, 255, 255, 1)} => roundedRect{radius:6}");
	
	$("#contentholder.start").liquidCanvas("fill{color:rgba(255, 255, 255, 0.80)} => roundedRect{radius:6}");
	
	$("#contentholder.kollektion").liquidCanvas("fill{color:rgba(255, 255, 255, 1)} => roundedRect{radius:6}");
	
	$("#contentholder.kollektionen").liquidCanvas("fill{color:rgba(255, 255, 255, 1)} => roundedRect{radius:6}");
	
	$("#contentholder.kollektionen_uebersicht").liquidCanvas("fill{color:rgba(255, 255, 255, 1)} => roundedRect{radius:6}");
	
	$(".backbutton").children("a").liquidCanvas("fill{color:rgba(0, 0, 0, 1)} => roundedRect{radius:6}");
	$(".back_account_button").liquidCanvas("fill{color:rgba(0, 0, 0, 1)} => roundedRect{radius:6}");
	
	$(".productinfohover").children("span").liquidCanvas("fill{color:rgba(0, 0, 0, .7)} => roundedRectArrow{radius:6}");
	
	
	$("input.textfield").liquidCanvas("fill{color:rgba(0, 0, 0, .7)} => roundedRect{radius:6}");
	$("textarea.textfield").liquidCanvas("fill{color:rgba(0, 0, 0, .7)} => roundedRect{radius:6}");
	$("select.selectfield").liquidCanvas("fill{color:rgba(0, 0, 0, .7)} => roundedRect{radius:6}");
	$("input.buttonfield").liquidCanvas("fill{color:rgba(0, 0, 0, .7)} => roundedRect{radius:6}");
	$("div.checkboxfield").liquidCanvas("fill{color:rgba(0, 0, 0, .7)} => roundedRect{radius:6}");
	$("a.linkfield").liquidCanvas("fill{color:rgba(0, 0, 0, .7)} => roundedRect{radius:6}");
	
	$("a.button_cart").liquidCanvas("[fill{color:rgba(190, 158, 76, 0.8)}] => roundedRect{radius:6}");	
	$("a.button_my_account").liquidCanvas("[fill{color:rgba(190, 158, 76, 0.8)}] => menurectRight{radius:6}");
	$("a.button_logout").liquidCanvas("[fill{color:rgba(190, 158, 76, 0.8)}] => menurectLeft{radius:6}");
	
		// init bg slides
	if($("#background").hasClass("slide")) {
		init_bg_slides();	
	}
	
	// Get Header Basic
	/*$('#getheaderbasket').load('/shop/index.php?&cl=getheader', function() {
		$("#getheaderbasket").children("span").liquidCanvas("fill{color:rgba(255,255,255, 1)} => roundedRectArrow{radius:6}");
		$("#getheaderbasket").css({ display: "none" });
	});

	$(".button_cart").hover(
		function() {
			$('#getheaderbasket').css({ display: "block" });
		},
		function() {
			$('#getheaderbasket').css({ display: "none" });
		}
	);*/
	
	
	/* ----------
	PRODUKTE
	*/
	
	if (!$.browser.msie) {
		$(".product .productinfohover").each(function() {
			$(this).css({ display: 'none' });
		});
		
		
		
		
	
		
		$(".product").hover(function(e) {
			$(this).children(".productinfohover").css({ top: 50 });
			$(this).children(".productinfohover").css({ left: -30 });
			$(this).children(".productinfohover").fadeIn(200);
			$(this).children(".productinfohover").bind("click", function() {
				document.location.href = $(this).parent().children("a").attr("href");															
			});
		},
		
		function(e) {
			$(this).children(".productinfohover").fadeOut(200);
			$(this).children(".productinfohover").unbind("click");
		});
	
	} else {
		$(".product .productinfohover").each(function() {
			$(this).css({ display: 'none' });
		});
		
	
	}
	
	
}

function init_functions() {

	if($('#content.content').length>0 && !$('#content.content').hasClass("user")) {
		if($("#contentholder").height()<500) {
			$(".Content_sliderBackground").css({ display: 'none' });
		} else {
			
			$('#content.content').smartScroll({
				 prefix:'Content',
				 width: 50,
				 height:460,
				 smart:false,
				 fixWidth:1
			  });
		}
	}
	
	if($('#content.press').length>0) {
		if($("#contentholder").height()<500) {
			$(".Content_sliderBackground").css({ display: 'none' });	
		} else {

			$('#content.press').smartScroll({
				 prefix:'Content',
				 width: 50,
				 height:420,
				 smart:false,
				 fixWidth:1
			  });
		}
	}
	
	if($('#content.basket').length>0) {
		if($("#content.basket").height()<310) {
			$(".Basket_sliderBackground").css({ display: 'none' });	
		} else {

			$('#content.basket').smartScroll({
				 prefix:'Basket',
				 width: 25,
				 height:300, 
				 smart:false,
				 fixWidth:1
			  });
			$("#content.basket").css({ "margin-bottom": 10 });
		}
	}
	
	if($('#content.order').length>0) {
		if($("#content.order").height()<400) {
			$(".Basket_sliderBackground").css({ display: 'none' });	
		} else {

			$('#content.order').smartScroll({
				 prefix:'Basket',
				 width: 25,
				 height:430, 
				 smart:false,
				 fixWidth:1
			  });
			$("#content.order").css({ "margin-bottom": 10 });
		}
	}
	
	/*if(mobile) { //$('.Content_targetContainer').touchScroll(); }
	
	}*/

}

function init_bg_slides() {
	count_slides = $("#background").children(".slide").length;
	slide_z = 99;
	
	slide_buttons = $("<div/>").addClass("slide_buttons");
	
	$("#background .slide").each(function(index) {
		$(this).css({ 'z-index': slide_z });
		slide_z--;
		
		if(index>0) {
			$(this).css({ display: 'none' });	
		}
		
		var button = $("<span/>").addClass("button");
		
		$(slide_buttons).append(button);
		
		bg_slides.push(this);
		
	});
	
	
	
	reinit_slide_buttons(0);
	
	//set Slideshow
	
	slideshow = window.setInterval(function() {
	
		if(ac_bg_slide < bg_slides.length-1) {
			goto_slide(ac_bg_slide+1);	
		} else {
			goto_slide(0, false);	
		}
	
	}, 10000);
	
}

function reinit_slide_buttons(ac) {
	
	$("#buttons.slide canvas").each(function() {
		$(this).remove();									
	});
	
	$("#buttons.slide div").each(function() {
		$(this).remove();									
	});
	
	$("#buttons.slide").append(slide_buttons);
	
	var clear = $("<div/>").addClass("clear");
	$("#buttons.slide").append(clear);
	
	$(".slide_buttons .button").each(function(index) {
		
		if(index==ac) {
			$(this).addClass("active");
		} else {
			$(this).removeClass("active");	
		}
		
		if(!$(this).hasClass("active")) {
			$(this).liquidCanvas("[border{color:rgba(190, 158, 76, 1); width: 1} fill{color:rgba(190, 158, 76, 0.0)}] => roundedRect{radius:4}");
		} else {
			$(this).liquidCanvas("[gradient{from:#7a6431; to:rgba(190, 158, 76, 1)} border{color:rgba(190, 158, 76, .7); width: 1} fill{color:rgba(190, 158, 76, 0.0)}] => roundedRect{radius:4}");	
		}
		
		$(this).click(function() {
			window.clearInterval(slideshow);
			goto_slide(index ,true);
		});
		
	});	
	
	
	
}

function goto_slide(slide ,rein) {
	
	if(ac_bg_slide != slide) {
	
		slide_cut_height = 21;
		var slide_cuts = new Array();
		var slide_cuts_animation = new Array();
		var ani_toggle = true;
		
		$(bg_slides[slide]).css({ display: 'block' });
		
		for(i=0; i<=651; i+=slide_cut_height) {
			var slide_cut = $("<DIV/>").addClass("slide_cut").css({ 
				top: i,
				'background-image': $(bg_slides[ac_bg_slide]).css('background-image'),
				'background-position': '0px -'+i+'px'
			});
			
			slide_cuts.push(slide_cut);
			$("#background").append(slide_cut);
			
			if(ani_toggle) {
				$(slide_cut).animate({ left: -899 }, i+100, function() {
					$(this).remove();																 
				});
			} else {
				$(slide_cut).animate({ left: 899 }, i+100, function() {
					$(this).remove();																 
				});
			}
			ani_toggle = !ani_toggle;
			
		}
		$(bg_slides[ac_bg_slide]).css({ display: 'none' });
		
		ac_bg_slide = slide;
		
		reinit_slide_buttons(slide);
		
		if(rein) {
			slideshow = window.setInterval(function() {
	
				if(ac_bg_slide < bg_slides.length-1) {
					goto_slide(ac_bg_slide+1);	
				} else {
					goto_slide(0);	
				}
			
			}, 10000);	
		}
	}
	
}

function check_contact_form() {
	if($("#field_name").val()=="") {
		alert("Bitte füllen Sie das Feld 'Name' vollständig aus!");
		$("#field_name").focus();
		return false;
	}
	if(!isValidEmailAddress($("#field_email").val())) {
		alert("Bitte geben Sie eine gültige E-Mail Adresse an!");
		$("#field_email").focus();
		return false;
	}
	if($("#field_subject").val()=="") {
		alert("Bitte füllen Sie das Feld 'Betreff' vollständig aus!");
		$("#field_subject").focus();
		return false;
	}
	if($("#field_message").val()=="") {
		alert("Bitte füllen Sie das Feld 'Nachricht' vollständig aus!");
		$("#field_message").focus();
		return false;
	}
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

