// JavaScript Document

var fWeite;
var fHoehe;	
var aktQuali;
var switcher;
var MaterialName;
var	MaterialId;


/******* Rundungsfunktion ********/
function number_format( /* in: float   */ number,
                        /* in: integer */ laenge,
                        /* in: String  */ sep,
                        /* in: String  */ th_sep ) {

  number = Math.round( number * Math.pow(10, laenge) ) / Math.pow(10, laenge);
  str_number = number+"";
  var arr_int = str_number.split(".");
  if(!arr_int[0]) arr_int[0] = "0";
  if(!arr_int[1]) arr_int[1] = "";
  if(arr_int[1].length < laenge){
    nachkomma = arr_int[1];
    for(i=arr_int[1].length+1; i <= laenge; i++){  nachkomma += "0";  }
    arr_int[1] = nachkomma;
  }
  /*if(th_sep != "" && arr_int[0].length > 3){
    Begriff = arr_int[0];
    arr_int[0] = "";
    for(j = 3; j < Begriff.length ; j+=3){
      Extrakt = Begriff.slice(Begriff.length - j, Begriff.length - j + 3);
      arr_int[0] = th_sep + Extrakt +  arr_int[0] + "";
    }
    str_first = Begriff.substr(0, (Begriff.length % 3 == 0)?3:(Begriff.length % 3));
    arr_int[0] = str_first + arr_int[0];
  }*/
  return arr_int[0]+sep+arr_int[1];
}
/******* BorderPrint Funktion ********/
function border(obj, format){
	
			if(obj.attr("checked")){
				var borderWidth = $("#cropbox").width()-20;
				var borderHeight = $("#cropbox").height()-20;
				
				$("#cropbox").append('<div id="borderView"></div>');
				$("#borderView").css({
					"width":borderWidth,
					"height":borderHeight
					});
				
				var fW = parseInt($("#formWeite").html());
				var fH = parseInt($("#formHoehe").html());
			  	fW = number_format( fW+6,2,".")+" cm";
				fH = number_format( fH+6,2,".")+" cm";
				$("#formWeite").html(fW);
				$("#formHoehe").html(fH);
				
			}else{
				
				$("#borderView").remove();
				var form = format.split(" x ");
				var fW = form[0];
				var fH = form[1];
			  				
				if( $("#designerBild img").width() > $("#designerBild img").height() ){
					$("#formWeite").html(fW+" cm");
					$("#formHoehe").html(fH+" cm");				
					}else{
						$("#formWeite").html(fH+" cm");
						$("#formHoehe").html(fW+" cm");							
					}
				
				}
}
/******* Croper Funktion ********/
function croper(format){
	
	var f = format.split(" x ");
	var formatWeite = f[0]; 	
	var	formatHoehe = f[1];
	
	var cropWidth;
	var cropHeight;
	var cropTop;
	var cropLeft;
	
	
	if ( $("#designerBild img").width() > $("#designerBild img").height() ){ //Querformat
			
		if( formatWeite == formatHoehe ){
				cropWidth = cropHeight = $("#designerBild img").height();					
			}else{					
				cropHeight = Math.round( $("#designerBild img").width() * formatHoehe/formatWeite );										
				if(cropHeight > $("#designerBild img").height()){						
						cropHeight = $("#designerBild img").height();
						cropWidth = Math.round(cropHeight/formatHoehe*formatWeite);						
					}else{
						cropWidth = $("#designerBild img").width();
					}
			}			
		}else{ //Hochformat			
			if(formatWeite == formatHoehe){							
					cropWidth = cropHeight = $("#designerBild img").width();		
				}else{					
					cropWidth = Math.round($("#designerBild img").height()*formatHoehe/formatWeite);					
					if(cropWidth > $("#designerBild img").width()){						
						cropWidth = $("#designerBild img").width();
						cropHeight = Math.round(cropWidth/formatHoehe*formatWeite);						
						}else{
						cropHeight = $("#designerBild img").height();
						}					
				}			
		}
		if ($("#designerBild img").width() == $("#designerBild img").height()){ //QuadratFormat
			
			if(formatWeite == formatHoehe ){
					cropWidth = cropHeight =  $("#designerBild img").height();				
				}else{					
					cropWidth = $("#designerBild img").height()*formatHoehe/formatWeite;
					cropHeight = $("#designerBild img").height();
				}			
		}	
		
	if($("#posTop").val() != ""){
			cropTop = parseInt($("#posTop").val());
		}else{
			cropTop = Math.round(($("#designerBild img").height()-cropHeight)/2);
		}
	if($("#posLeft").val() != ""){
			cropLeft = parseInt($("#posLeft").val());
		}else{
			cropLeft = Math.round(($("#designerBild img").width()-cropWidth)/2);
		}	

	$("#cropbox").css({
		"width":cropWidth,
		"height":cropHeight,
		"top":cropTop,
		"left":cropLeft,
		"cursor":"move"
	});
		
		$("#posLeft").val(cropLeft);
		$("#posTop").val(cropTop);
		$("#cWidth").val(cropWidth);
		$("#cHeight").val(cropHeight);	
					
	$("#formWeite").css({
		"background":"#444",
		"top":-12,
		"left":Math.round((cropWidth-60)/2)		
		});
		
	$("#formHoehe").css({
		"background":"#444",
		"top":Math.round((cropHeight-50)/2),
		"left":cropWidth-20			
		});
		
	/* Crop Width und Height in hiddenfelder eintrage*/	
	$("#cWidth").val(cropWidth);
	$("#cHeight").val(cropHeight);
	
	/*********** Cropbox Ränder *********************/	
	$("#cropborderTop").css({
					"width":cropWidth,
					"height":cropTop,
					"top": 0,
					"left": 0
				})	
	$("#cropborderBottom").css({
					"width":cropWidth,
					"height":$("#designerBild img").height()-(cropHeight+cropTop),
					"left":0,
					"bottom":0
				})			
	
	$("#cropborderRight").css({
					"width":$("#designerBild img").width()-(cropWidth+cropLeft),
					"height":cropHeight,
					"top": 0,
					"right": 0
				});
	$("#cropborderLeft").css({
					"width":cropLeft,
					"height":cropHeight,
					"top":0,
					"left":0
				});	
	/*********** Cropbox Draggable *********************/		
	$("#cropbox").draggable({		
		"containment": "parent",
		"scroll": false,
		"drag": function(event, ui) {
			
			$("#cropborderTop").css({
					"width":cropWidth,
					"height":Math.round(ui.position.top)
				});
			$("#cropborderBottom").css({
					"width":cropWidth,
					"height":Math.round($("#designerBild img").height()-(cropHeight+ui.position.top))
				});
			$("#cropborderLeft").css({
					"width":Math.round(ui.position.left),
					"height":cropHeight
				});
			$("#cropborderRight").css({
					"width":Math.round($("#designerBild img").width()-(cropWidth+ui.position.left)),
					"height":cropHeight
				});
			},			
		"stop": function(event, ui) { 
			$("#posLeft").val(Math.round(ui.position.left));
		    $("#posTop").val(Math.round(ui.position.top));
			$("#cWidth").val(cropWidth);
			$("#cHeight").val(cropHeight);
		 	}		
		});
			
	border($(".borderPrint"), format);
	
}

/******* printQuali Funktion ********/
function printQuali(weite, hoehe, fweite, fhoehe){
	
	var dpi;
	var qualit;
	
	if( weite > hoehe ){			
		  dpi = Math.round( ( 2.54 * weite ) / fweite );				         
    }else{
		  dpi = Math.round( ( 2.54 * weite ) / fhoehe );
	}	
		  
         switch(true){
                 case dpi >= 120:
                 qualit = '<b style="color:#009933">sehr gut</b>';
                 break;
                 case dpi >= 75:
                 qualit = '<b style="color:#99cc33">gut</b>';
                 break;
                 case dpi >= 50:
                 qualit = '<b style="color:#EEE007">befriedigend</b>';
                 break;
                 case dpi >= 31:
                 qualit = '<b style="color:#ff6600">noch ausreichend</b>';
                 break;
                 case dpi <= 30:
                 qualit = '<b style="color:#cc0000">ungenügend</b>';
                 break;
         }
		 
		 return qualit;

	}

/**********************************/
/*** Format einblenden Function ********/
function formateEinblenden(obj, mat){
	
	$("#overlayBlende").show();
	
	$("#formatWrap").css({
		"top": Math.round($(window).height()/2-300),
		"left": Math.round($(window).width()/2-250)			
	}).fadeIn();
				
	$("#formatWrap").load("/includes/menu/formatMenu.php",function(){ 		
		$("#formatCont").empty().html('<div class="loadingGif"></div>');
		$(".loadingGif").css({
			"top": Math.round($("#formatWrap").height()/2-40),
			"left": Math.round($("#formatWrap").width()/2-40)
			});	
		$("#formatCont").show();	
		$("#formatCont").load("/includes/menu/formatCont.php?material="+mat,function(){
				
				$("#formatList tr").filter(function(index) {
						return index % 2 == 0;
				}).addClass("zweiteZeile");
				
				$("#formatList tr").hover(
					function() {
						$(this).addClass("hoverOn");
						if($(this).hasClass("zweiteZeile")){
							$(this).removeClass("zweiteZeile");
						};
					},
					function() {
						$(this).removeClass("hoverOn");
						$("#formatList tr").filter(function(index) {
							return index % 2 == 0;
						}).addClass("zweiteZeile");
					}
				);					
		});
		/*** Format Filter ***/	
		$(".filterWrap").click(function(){
			$(".filter").fadeOut("fast");
			var id = "#"+$(this).attr("name");
			$(id).show();	
			return false;
		});													   
					
		$(".filter li").hover(
				function(){ 
				$(this).css("backgroundColor","#f55")},
				function(){
				$(this).css("background","#fff") 
		});			
					
		$(".filter li").click(function(){												
			
			var activ = "#activ"+$(this).parent().attr("id");
			var hidden = "#h"+$(this).parent().attr("id");
			var activWert = $(this).html();
			var ul = "#"+$(this).parent().attr("id");
			$(ul).hide();
			
			if($(this).attr("name") == "all"){					
				$(activ).attr("name","all");
				$(activ).html("");
				$(hidden).val("");
			}else{
				$(activ).attr("name",$(this).attr("name"));
				$(activ).html(activWert);
				$(hidden).val($(this).attr("name"));
			}
			
			
			var Abfrage = "material="+mat+"&sf="+$("#hsizeFilter").val()+"&ff="+$("#hformatFilter").val()+"&pf="+$("#hpreisFilter").val();
			$.get("/includes/menu/formatCont.php", Abfrage, function(Ergebnis){
				$("#formatCont").html("<p>"+Ergebnis+"</p>");
			})

			return false;
		});
			
					
  	    });//$("#formatWrap").load();
		
		$("tr.formatLink").live("click",function(){
				var produktId = $(this).attr("name");
				var produktName = $(this).find("td.formatName").attr("name");	
				var produktTitle = $(this).find("td.formatName").html();
				
		if(obj.id == "formatmenu"){				
				var pPreis = $(this).find("td.formatPreis").attr("name");
				var produktPreis = number_format(pPreis,2,".");				
				$("#formatText").html('<span class="fName">'+produktName+' cm</span><span class="fPreis">'+produktPreis+' &euro;</span>');
				
		}else{
			
			var produktPreis = number_format($(this).find("td.formatPreis").attr("name"),2,".");
			var formatRealtion = $(this).find("td.formatGruppe").attr("name");
			var fo = $(this).find("td.formatName").attr("name").split(" x ");
			var produktMenge = parseInt($("#produktMenge").val());
			fWeite = number_format(fo[0],1,".");
			fHoehe = number_format(fo[1],1,".");
					
			$("#FormatHeadline").html(produktTitle);
			$("#desingerStueckPreis").html(produktPreis+" &euro;");
			$("#desingerGesamtPreis").html(produktPreis*produktMenge+" &euro;");
		

			$("#fRelationValue").val(formatRealtion);				
			$("#preisValue").val(produktPreis);	
			$("#produktValue").val(produktId);
			$("#desingerStueckPreis").attr("name",produktPreis);	
			$("#posTop").val("");
			$("#posLeft").val("");
			
			if( $("#designerBild img").width() > $("#designerBild img").height() ){
				$("#formWeite").html(fWeite+" cm");
				$("#formHoehe").html(fHoehe+" cm");
				
			}else{
					$("#formWeite").html(fHoehe+" cm");
					$("#formHoehe").html(fWeite+" cm");
					
			}
				
			var orgBildSize = $("#orgBildSize").val().split(",");
			var orgBildWeite = Math.round(orgBildSize[0]);
			var orgBildHoehe = Math.round(orgBildSize[1]);
			
			aktQuali = printQuali(orgBildWeite,orgBildHoehe,fWeite,fHoehe);
			
			$(".qualit ").html("Druckqualit&auml;t: "+aktQuali);
			
			croper(produktName);
			
			}
		
		$("#hiddenprodukt").attr("value",produktId);
		$("#formatWrap").fadeOut();
		$("#formatCont").hide();
		$("#overlayBlende").hide();
		var newPrice = number_format((produktPreis * produktMenge),2,".");
		
		$("#desingerPreis").html(newPrice+ " &euro;");
		return false;
		});
	}
/**********************************/	

$(document).ready(function(){
	
/********* Form Check *********/
/* Validation Create Account Form */

$("#create_account").validate({	   
    "rules": {
     // simple rule, converted to {required:true}
     	"anrede": "required",
	 	"vorName": "required",
	 	"nachName": "required",
	 	"strasse": "required",
	 	"plz": {
      		"required": true,
      		"number": true
    		},
	 	"ort": "required",
     	"email": {
      		 	"required": true,
       			"email": true
     	},
	 	"country": "required"
   	},
    "messages": {
     	"anrede": "Bitte auswählen!",
	 	"vorName": "Bitte eingeben!",
	 	"nachName": "Bitte eingeben!",
	 	"strasse": "Bitte eingeben!",
	 	"plz": {
      		"required": "Bitte eingeben!",
      		"number": "Bitte nur Ziffern eingeben!"
	   		},
		" ort": "Bitte eingeben!",
     	"email": {
			"required": 'Bitte eingeben!',
			"email": 'Bitte eine korrekte eMail eingeben!'		
		},
	 "country": "Bitte auswählen!"	
   }
       
});

/* Validation Shipping Adress Form */

$("#shipping_adress").validate({
	 "rules": {
     // simple rule, converted to {required:true}
     	"anrede": "required",
	 	"vorName": "required",
	 	"nachName": "required",
	 	"strasse": "required",
	 	"plz": {
      		"required": true,
     		 "number": true
    		},
	 	"ort": "required",
     // compound rule
    	"country": "required"
  	 },
   	"messages": {
     	"anrede": "Bitte auswählen!",
	 	"vorName": "Bitte eingeben!",
	 	"nachName": "Bitte eingeben!",
	 	"strasse": "Bitte eingeben!",
	 	"plz": {
     		 "required": "Bitte eingeben!",
      		"number": "Bitte nur Ziffern eingeben!"
	   	},
	 	"ort": "Bitte eingeben!",
     	"country": "Bitte auswählen!"	
   }
	      
});

/* Validation Send Order Form */

$("#send_order_form").validate({
   "rules": {
     // simple rule, converted to {required:true}
     "payment": "required",
	 "agb": "required"
   },
   "messages": {
     "payment": "Bitte eine Zahlart auswählen!",
	 "agb": {
      "required": "Bitte bestätigen Sie unsere AGB!"     
	   } 
   }
});

/* Validation Kontakt Form */

$("#kontaktForm").validate({
   rules: {
	 anrede: "required",
     vname: "required",
	 nname: "required",
	 email: {
       required: true,
       email: true
     },
	 anfrage: "required"
   },
   messages: {
	 anrede:"Bitte auswählen!",
     vname: "Bitte eintragen!",
	 nname: "Bitte eintragen!",
	 email: {
		required: 'Bitte eingeben!',
		email: 'Bitte eine korrekte eMail eingeben!'		
		},
	 anfrage: "Bitte geben Sie Ihre Anfrage ein!"
   }
});

/*****************************/

$("#overlayBlende").css({
		"background-color"	: "#000",
		"opacity"			: 0.6
});
/* CSS cropBox Rahmen */
$("#cropborderTop").css({
	"opacity":0.7,
	"background":"#333"
});
$("#cropborderRight").css({
	"opacity":0.7,
	"background":"#333"
});
$("#cropborderBottom").css({
	"opacity":0.7,
	"background":"#333"
});
$("#cropborderLeft").css({
	"opacity":0.7,
	"background":"#333"
});
/**********************/
$(".closer").attr("title","Auswahl");
$(".closer").live("click",function(){
		$(this).parent().hide("fast",function(){
			$("#overlayBlende").hide();						   
		});							   
	});

/*************** StuffMenu *******************/
	$("#stuffmenu").click(function(){		
			
		$("#overlayBlende").show();
		
		$("#stuffCont").css({
			"top": Math.round($(window).height()/2-200),
			"left": Math.round($(window).width()/2-250)
			}).fadeIn();		
			
		$("#stuffCont").empty().html('<div class="loadingGif"></div>');
		$(".loadingGif").css({
			"top": Math.round($("#stuffCont").height()/2-40),
			"left": Math.round($("#stuffCont").width()/2-40)
			});
		
		$("#stuffCont").load("/includes/menu/stuffMenu.php",function(){
				
							
			$("a.stuffLink").click(function(){
				
			MaterialName 	= $(this).attr("rel");
			MaterialId 		= $(this).attr("name");
			
			
			$("#infoHeadline").text(MaterialName);
				$("#stuffCont").fadeOut();
				
				if($(this).attr('rev') != ""){					
					$("#subStuffCont").load("/includes/menu/subStuffMenu.php?mat="+$(this).attr('name'),function(){
						$("#subStuffCont").css({
							"top": Math.round($(window).height()/2-200),
							"left": Math.round($(window).width()/2-250)
						}).fadeIn();							
					
					$("a.subStuffLink").click(function(){
						$("#subStuffCont").fadeOut();
						$("#overlayBlende").hide();
						$("#stuffText").text($(this).attr("rel").substr(0,30));
						$("#stuffText").attr("name",$(this).attr("name"));
						$("#formatText").html("Format ausw&auml;hlen!");						
						return false;
						
						});
					});
				}else{			
				
				$("#overlayBlende").hide();
				$("#stuffText").text(MaterialName);
				$("#stuffText").attr("name",MaterialId);
				$("#formatText").html("Format ausw&auml;hlen!");
								
				}
									
				$("#materialBild").css("background","url(css/images/imagePics/"+MaterialName.replace(' ', '_')+".jpg)  no-repeat center center");
	
				$("#materialText").html($("#"+MaterialName.replace(' ', '_')).html());
				return false;
				
			});
			
		});
	});
/********************************************/

	/*************** FormatMenu *******************/
	$("#formatmenu").click(function(){	
		var mat = $("#stuffText").attr("name");	
		formateEinblenden(this, mat);	
	});		

	/************* change FormatMenu *************/
	$("#changeFormat").click(function(){
		var mat = $("#MaterialHeadline").attr("name");
		var artikel = $(this).attr("name");			
		formateEinblenden(this, mat);		
	})
	/********************************************/
	
	/*************** change Menge *******************/
	$("#produktMenge").change(function() {
  		var artikelPreis = $("#desingerStueckPreis").attr("name");
		var summe = number_format(artikelPreis * $(this).val(),2,".");
		$("#desingerGesamtPreis").html(summe+ " &euro;");		
	});
	/********************************************/
	/******** Button Bild löschen *******/
		$(".delButton").click(function(){				
				if(confirm("Möchten Sie dieses Bild wirklich löschen?")){					
					$.get("../includes/delFromCart.php",
							{dId : $(this).attr("rel")},
							function(){ 
								location.href='index.php'
							});
				};				
		});			
	/************************************/
	/*************** Rahmendruck *******************/
	
	$(".borderPrint").change(function() {
  		var format = $("#FormatHeadline").attr("name");
		border($(this), format);
	});
	
	/************************************/

	
/* Angebotsbox */
	$(".angebotBox").click(function(){
			var material = $(this).find("h3").html();
			var materialId = $(this).find("h3").attr("name");
			
			var format = $(this).find(".angebotsFormat b").html();
			var formatId = $(this).find(".angebotsFormat").attr("name");
			
			var preis = $(this).find(".angebotsPreis").html();
			var pid = $(this).attr("id");
			pid = pid.substr(2);
					
		$("#stuffText").text(material);
		$("#stuffText").attr("name",materialId);
		
		$("#materialBild").css("background","url(css/images/imagePics/"+material.replace(' ', '_')+".jpg)  no-repeat center center");
		$("#materialText").html($("#"+material.replace(' ', '_')).html());
		
		$("#formatText").html('<span class="fName">'+format+'</span><span class="fPreis">'+preis+'	</span>');	
		$("#hiddenprodukt").attr("value",pid);	
	});
	
	
});
