$(document).ready(function(){
	
	/*
	 *
	 * productViewer
	 * bei eheringe, accessoires sonstiges, uhren, schmuck
	 *
	 */
	
	$.fn.productViewer = function() {
		var pv = $(this);
		pv.after('<div id="productViewer_large"></div>');
		
		$("div", pv).wrapAll("<div />");
		
		/*grosses bild und beschreibung laden*/
		$("div div a:not(.productViewer_pagebutton)", pv).click(function() {
			var urlstring = "/content/productviewer_beschreibung.php?"+
							"producttype="+pv.attr("class")+
							"&searchstring="+$(this).attr("href").replace("#", "")+
							"&imgpath="+$("img", $(this)).attr("src").replace("_small.jpg", "_large.jpg");
			
			$.get(urlstring, function(data) {
				$("#productViewer_large").empty().append(data);
			});
		});
		
		/*blaettern*/
		$("div div a.productViewer_pagebutton", pv).click(function() {
			if ($(this).hasClass("productViewer_lastitem")) {
				$("> div", pv).css("margin-left", function() {return parseInt($("> div", pv).css("margin-left")) - 421 + "px"});
			} else {
				$("> div", pv).css("margin-left", function() {return parseInt($("> div", pv).css("margin-left")) + 421});
			}
		});
		
		/*beschreibung anzeigen bei mouseover*/
		$("#productViewer_large").mouseover(function() {
			$("div", $(this)).show();
		}).mouseout(function() {
			$("div", $(this)).hide();
		});
		
		
		/*deep linking*/
		$(window).hashchange( function(){		
			if (location.hash.replace( /^#/, '' ) == '') {
				$('div div a:first', pv).trigger('click');
			} else {
				$('div div a[href="'+location.hash+'"]', pv).trigger('click');
			}
		});
		$(window).trigger('hashchange');		
	}
	
	$("#productViewer").productViewer();
	
	/*
	 *
	 * Accessoires Schreibgeraete
	 *
	 */

	$(".accessoires_schreibgeraet_container").mouseenter(function() {
		$(".accessoires_schreibgeraet_beschreibung", $(this)).show()
	}).mouseleave(function() {
		$(".accessoires_schreibgeraet_beschreibung", $(this)).hide()
	});

	/*
	 *
	 * Contact Form Validation
	 *
	 */
	 
	$("#contactform").submit(function() {
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		if ($("input[name='name']", this).val() == "" || $("input[name='email']", this).val() == "" || $("input[name='betreff']", this).val() == "" || $("textarea", this).val() == "" || !emailReg.test($("input[name='email']", this).val())) {
			$("#form_validation_failed").show();
			$("#form_validation_passed").hide();
			return false;
		}
	});

	/*
	 *
	 * Index Photoshow
	 *
	 */
	 
	 $("#photoshow").cycle({ 
		fx:    'fade', 
		speed:  800,
		timeout: 3000
	 });
	
	/*
	 *
	 * Index Newsscroll
	 *
	 */
	
	var moving = false;
	var timer = true;
	
	function move(direction) {
		var currentmargin = Math.abs(parseInt($("#newsitemsinner").css("margin-top")));
		
		if (moving == false) {return;}
		if (currentmargin == ($("#newsitemsinner").height() - $('#newsitems').height()) && direction == "up") {return;}
		if (currentmargin == 0 && direction == "down") {return;}
		
		if (direction == "up") {
			$("#newsitemsinner").css("margin-top", parseInt($("#newsitemsinner").css("margin-top")) - 1);
		} else if (direction == "down") {
			$("#newsitemsinner").css("margin-top", parseInt($("#newsitemsinner").css("margin-top")) + 1);
		}

		var timer = setTimeout(function() {move(direction);}, 10);
	}
	
	function stopmove() {
		moving = false;
		clearTimeout(timer);
	}
	
	function startmove(direction) {
		moving = true;
		move(direction);
	}
	
	$("#news_scrolldown").mouseover(function() {startmove("up")});	
	$("#news_scrollup").mouseover(function() {startmove("down")});
	
	$("#news_scrollup").mouseout(function() {stopmove();});
	$("#news_scrolldown").mouseout(function() {stopmove();});
	
	/*
	 *
	 * Standort Map
	 *
	 */
	 
	$(".standort_map#donauzentrum").gMap({ markers: [{ latitude: 48.242374,
                              longitude: 16.434742,
                              html: "Interjuwel Donauzentrum",
                              popup: false }],
							  zoom: 15 })

	$(".standort_map#wipplingerstrasse").gMap({ markers: [{ latitude: 48.213571,
                              longitude:16.367033,
                              html: "Interjuwel Wipplingerstraße",
                              popup: false }],
							  zoom: 15 })

	$(".standort_map#meiselmarkt").gMap({ markers: [{ latitude: 48.198388,
                              longitude:16.319689,
                              html: "Interjuwel Meiselmarkt",
                              popup: false }],
							  zoom: 15 })
 });
