// JavaScript Document

var mExt = 0, myPage = 1, scrollY, scrollX;

$(document).ready( function() {
	$.get('./content/shop/initialize.class.php',{'set':'1'});	
	
	$(window).hashchange( function() {
		var myHash = location.hash.split("#");
			myHash = myHash[1];
		
		dataRequest( myHash );
	});
	
	$("a.nav").each(function()
	{
		$(this).removeAttr('href');
	});
	
	
	$(".navigation").children("li").mouseenter(function() {
		$(this).css("background-color","#FFC61A");
		$(this).css("color","#6B5000");
		
		if($(this).children("ul"))
		{
			$(this).children("ul").css("display","block");
		}
	});
	
	$(".navigation").children("li").mouseleave(function() {
		$(this).css("background-color","#FFD966");
		$(this).css("color","#636363");
		
		if($(this).children("ul"))
		{
			$(this).children("ul").css("display","none");
		}
	});
	
});

/*function onMouseOver(id) {
	document.getElementById(id).style.backgroundColor = "#FFC61A";
	document.getElementById(id).style.Color = "#6B5000";
	
	if(document.getElementById(id+'1')) {
		document.getElementById(id+'1').style.display = "block";
	}
}

function onMouseOut(id) {
	document.getElementById(id).style.backgroundColor = "#FFD966";
	document.getElementById(id).style.Color = "#636363";		
	
	if(document.getElementById(id+'1')) {
		document.getElementById(id+'1').style.display = "none";
	}
}

function onMouseOverSub(id) {
	document.getElementById(id).style.backgroundColor = "#FFD966";
	document.getElementById(id).style.Color = "#6B5000";
	
	if(document.getElementById(id+'1')) {
		document.getElementById(id+'1').style.display = "block";
	}
}

function onMouseOutSub(id) {
	document.getElementById(id).style.backgroundColor = "#FFE699";
	document.getElementById(id).style.Color = "#636363";		
	
	if(document.getElementById(id+'1')) {
		document.getElementById(id+'1').style.display = "none";
	}
}*/

function shopBack() 
{	
	dataRequest( 28 , "" , overviewNavigation( myPage ) );
	
}

function overviewNavigation(page)
{
	myMarginLeft = (page - 1) * 535;
	myPage = page;
	
	$(".articleOverview").animate({'marginLeft':'-'+myMarginLeft+'px'},300); 
	$("html,body").animate({scrollTop:"170px"},300);
}

function dataRequest( myHash ) {	
	var ready1 = false,
		ready2 = false,
		mainContent = false,
		id = null,
		sid = null,
		ext = null,
		handler = null;
		
		myHash = myHash.split("&");
		
	for( i = 0; i < myHash.length; i++)
	{
		var ar = myHash[i].split("=");
		
		switch( ar[0] )
		{
			case 'pID':
				id = ar[1];
			break;
			case 'sid':
				sid = ar[1];
			break;
			case 'ext':
				ext = ar[1];
			break;
			case 'handle':
				handler = ar[1]();
			break;
		}
	}
	
	$.get("content/counterTitle.php",{'pID':id },function(data)
	{
		$.get("counter/counter.php",{'mode':data });
	});
		
	$(".mainContent").html("<img src=\"img/load.gif\" width=\"20px\" border=\"0px\" /> Laden...");
	
	$.get("content/mainContent.php", { 'pID':id , 'ext':ext } , function(data) 
	{
		if( ready1 == true && ready2 == true )
		{
			$(".mainContent").html(data);
			handler;
			
			$("a.nav").each(function()
			{
				$(this).removeAttr('href');
			});
		} else mainContent = data;
	});
	
	$.get("content/mainHeadline.php", {'pID':id} , function(data) 
	{
		$(".mainHeadline").html(data);
		
		if( ready2 == true && !( !mainContent ) )
		{
			$(".mainContent").html(mainContent);
			handler;
			
			$("a.nav").each(function()
			{
				$(this).removeAttr('href');
			});
		}
		ready1 = true;
	});
	
	$.get("content/mainSubnavigation.php", {'pID':id} , function(data) 
	{
		$(".mainSubnavigation").html(data);
		
		if( ready1 == true && !( !mainContent ) )
		{
			$(".mainContent").html(mainContent);
			handler;
			
			$("a.nav").each(function()
			{
				$(this).removeAttr('href');
			});
		}
		ready2 = true
	});
}

function contactConfirm() {
	var name = $('#coName').val(),
		prename = $('#coPrename').val(),
		mail = $('#coMail').val(),
		subject = $('#coSubject').val(),
		message = $('#coMessage').val(),
		street = $('#coStreet').val(),
		zip = $('#coZip').val(),
		city = $('#coCity').val(),
		phone = $('#coPhone').val(),
		coError = document.getElementById('coError');
	var er = 0;
	
	if(name == "") {
		document.getElementById('erName').style.color =	"#FF4D4D";
		er = 1;
	} else {
		document.getElementById('erName').style.color = "#737373";
	}
	
	if(prename == "") {
		document.getElementById('erPrename').style.color = "#FF4D4D";
		er = 1;
	} else {
		document.getElementById('erPrename').style.color = "#737373";
	}
	
	if(mail == "") {
		document.getElementById('erMail').style.color = "#FF4D4D";
		er = 1;
	} else {
		$.post("./conent/check.php", {'mail':mail}, function(data)
		{
			if(data == "false")
			{
				document.getElementById('erMail').style.color = "#FF4D4D";
				er = 1;
			}
		});
	}
	
	if(subject == "") {
		document.getElementById('erSubject').style.color = "#FF4D4D";
		er = 1;
	} else {
		document.getElementById('erSubject').style.color = "#737373";
	}
	
	if(message == "") {
		document.getElementById('erMessage').style.color = "#FF4D4D";
		er = 1;
	} else {
		document.getElementById('erMessage').style.color = "#737373";
	}
	
	if(er == 0)
	{
		$.post("./content/contactFormSend.php", {'name':name,'prename':prename,'mail':mail,'subject':subject,'message':message,'street':street,'zip':zip,'city':city,'phone':phone}, function(data)
		{
			$(".mainContent").html(data);
		});
	}
}

function previewOpen(file,type) {
	if( typeof( window.pageYOffset ) == 'number' ) {
        scrollY = window.pageYOffset;
        scrollX = window.pageXOffset;
		window.pageYOffset = 0;
		window.pageXOffset = 0;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        scrollY = document.body.scrollTop;
        scrollX = document.body.scrollLeft;
		document.body.scrollTop = 0;
		document.body.scrollLeft = 0;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        scrollY = document.documentElement.scrollTop;
        scrollX = document.documentElement.scrollLeft;
		document.documentElement.scrollTop = 0;
		document.documentElement.scrollLeft = 0;
    }
	
	switch(type) 
	{
		case 'image':
			var html = "<img src='./img/"+file+"' border='0' /><br /><br /><a onClick='javascript:previewClose();'>Schlie&szlig;en</a>";
			document.getElementById('preview').innerHTML = html;
		break;
		case 'pdf':
			var html = "<iframe src='./objects/"+file+"' width='95%' height='60%' ></iframe><br /><br /><a onClick='javascript:previewClose();'>Schlie&szlig;en</a>";
			document.getElementById('preview').innerHTML = html;
		break;
		
	}
	
	document.getElementById('body').style.overflowY = "hidden";
	document.getElementById('previewShadow').style.display = "block";
	document.getElementById('preview').style.display = "block";
}

function previewClose() {
	document.getElementById('body').style.overflowY = "scroll";
	document.getElementById('previewShadow').style.display = "none";
	document.getElementById('preview').style.display = "none";
	if( typeof( window.pageYOffset ) == 'number' ) {
		window.pageYOffset = scrollY;
		window.pageXOffset = scrollX;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		document.body.scrollTop = scrollY;
		document.body.scrollLeft = scrollX;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		document.documentElement.scrollTop = scrollY;
		document.documentElement.scrollLeft = scrollX;
    }
}

function download(file) {
	downloadTarget.location.href='./content/download.php?src=../objects/'+file;
}

function scrollToTop() {
	$("html,body").animate({ scrollTop : 0 }, 1000);
}

function newsletterSend() 
{
	var mail = $("#newsMail").val(); var myHtml;
	
	$.post("./content/userForNews.php",{'mail':mail}, function(data) {
		if(data == "mailError")
		{
			$("#newsMail").css("border","1px solid red");
		}
		else
		{
			myHtml = "<h2>Newsletter Bestellen</h2>";
			myHtml += "Vielen Dank f&uuml;r Ihre Registrierung. Wir haben Ihnen eine E-Mail mit dem Aktivierungslink gesendet";
			
			$(".mainContent").html(myHtml);	
		}
	});
}
