/**
 * Print Order mini APP
 * (C)2009 Batailley Laurent for Byzance Design inc.
 * 
 */
var G = (function(){
/*Privates*/
    function getPosition(arrayName,arrayItem)
    {
    for(var i=0;i<arrayName.length;i++){ 
       if(arrayName[i]==arrayItem)
            return i;
    }
    };
    function checkEmail(value) {
	if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value))
	{return (true)};
    };
/*App Singleton*/
var o = {};
o.init = function(phpVals){
		this.boxmax = [394,308];
		this.minSize = 8;
		this.currVal = this.minSize; //valeur hauteur ou largeur changeante
		this.maxSize = 16;
		this.startSize = 8;
		this.showVals = ["inch.", "mm."]; //[0] est la valeur de d�part
		this.shipDests = ["intl","canada","usa"];
		this.shipDest = "intl"; //
		this.currPanel = 1; //Panel start 1
		this.shipTab = [[15,20,25],[8,12,16],[11,15,20]]; //order = shipDests order		
		this.C = {};
		this.Order = {};
		//
		this.minImageSize = this.getPixelSize(this.minSize);
		this.imageID = phpVals.ID;
		this.imageW = phpVals.W;
		this.imageH = phpVals.H;
		this.imageFilename = phpVals.FN;
		this.priceRates = [0,.41,.42,.43];
		this.imagePriceRate = this.priceRates[parseInt(phpVals.P)];		
		this.imageRatio = this.imageW / this.imageH;		
		this.imageOrientation = this.getOrientation();
		this.drawBox();
		this.insertImage();
		if (this.imageOrientation === "vert") {
		    this.selHeight = Math.round(this.minSize / this.imageRatio);
		} else {
		    this.selHeight = this.minSize;
		}
		this.updateValues(this.minSize);
		if (this.imagePriceRate != 0){this.initPanel(this.currPanel);}else {$("#insideNav").hide();this.initPanel(6);}		
		this.setUpListeners();
		setTimeout(function(){$("#loadingMask").fadeOut();},2000);
	};
o.getPixelSize = function(Size){
		var pxSize;
		pxSize = Math.round((this.boxmax[0] * Size)/(this.maxSize));
		return pxSize;
	}
o.getOrientation = function(){
		if (this.imageRatio >= 1) {return "horiz"}
		else {return "vert"}
	};
o.drawBox = function(){
		if (this.imageOrientation === "vert"){
		   $("#davisual").css({
			    height:this.boxmax[0],
			    width:Math.round(this.imageRatio * this.boxmax[0]),
			    top:"49px",
			    left: "101px"
			}
		    );
		    $("#davisual").css({"margin-left":Math.round((this.boxmax[1]- (Math.round(this.imageRatio * this.boxmax[0])) )/2)+"px"});
		    $("#blocapp").addClass("vertBG");		
		}
		else {
		    if (this.imageRatio == 1) {
				var temp = this.boxmax[0];
			this.boxmax[0] = this.boxmax[1];
			   $("#davisual").css({
				    width:this.boxmax[1],
				    height:this.boxmax[1],
				    top:90+((temp - this.boxmax[1])/2),
				    left: 101 + ((temp - this.boxmax[1])/2)
				}			
			    );
		    $("#davisual").css({"margin-left":Math.round((this.boxmax[1]- (Math.round(this.imageRatio * temp)) )/2)+"px"});
			$("#davisual").css({"margin-top":Math.round((this.boxmax[1]-( Math.round(temp / this.imageRatio) ))/2)+"px"});
		    			   
		    }
		    else {
			   $("#davisual").css({
				    width:this.boxmax[0],
				    height:Math.round(this.boxmax[0] / this.imageRatio),
				    top:"90px",
				    left:"62px"				
				}			
			    );
			    $("#davisual").css({"margin-top":Math.round((this.boxmax[1]-( Math.round(this.boxmax[0] / this.imageRatio) ))/2)+"px"});
		    };			
		    //
		    $("#blocapp").addClass("horizBG");						
		}
		//
		$('#daslider').slider({
				range: false,
				animate:true,
				min: this.minSize,
				max: this.maxSize,
				step:.5,
				change: function(){o.sliderMove();}
			});		
	};
o.insertImage = function(){
		$("#davisual").append($("#imagecache"));
		$("#imagecache").css({
		   position:"absolute",
		   top:0,
		   left:0
		});
		this.redrawImage(this.minSize);

	}
o.redrawImage = function(val){
		if (this.imageOrientation === "vert") {
		$("#imagecache").animate({
			top:(this.boxmax[0] - this.getPixelSize(val))/2,
			left:((this.imageRatio * this.boxmax[0]) - (this.imageRatio * this.getPixelSize(val)))/2,		    
			width:Math.round(this.imageRatio * this.getPixelSize(val)),
			height: this.getPixelSize(val)
		});
		} else {

		$("#imagecache").animate({
			width:this.getPixelSize(val),
			height: Math.round(this.getPixelSize(val) / this.imageRatio),
			top:((this.boxmax[0]/this.imageRatio)-(this.getPixelSize(val)/this.imageRatio))/2,
			left: (this.boxmax[0]-this.getPixelSize(val))/2			

		});
		}	    
	}
o.sliderMove = function(){
		var val;
		val = $("#daslider").slider('option', 'value');
		this.currVal = val;
		this.redrawImage(val);
		this.updateValues(val);
	};
o.updateValues = function(val){
		//Picture Id
		$(".show_picId").html(this.imageID);
		//Converter
		$("#conv").html("convert in "+o.showVals[1]);
		//Max-Min Vals
		if (this.showVals[0] === "mm."){
		    $(".show_minval").html(this.convertTomm(this.minSize));
		    $(".show_maxval").html(this.convertTomm(this.maxSize));
		}
		else {
		    $(".show_minval").html(this.minSize);
		    $(".show_maxval").html(this.maxSize);
		};		
		//Shipping
		$("#ship_"+this.shipDest).addClass("select");
		//Sizes
		if (this.imageOrientation === "vert") {
		    this.selWidth = val;
		    this.selHeight = Math.round((val / this.imageRatio)*100)/100;		    
		} else {
		    if (this.imageRatio == 1){//Square bad math round
			this.selHeight = val;
			this.selWidth = val;
		    } else {
			this.selWidth = Math.round((val * this.imageRatio)*100)/100;
			this.selHeight = val;
		    }

		}
		if (this.showVals[0] === "mm."){
		    $(".show_h").html(this.convertTomm(this.selHeight) +" mm.");
		    $(".show_w").html(this.convertTomm(this.selWidth)+" mm.");
		}
		else {
		    $(".show_h").html(this.selHeight +" inch.");
		    $(".show_w").html(this.selWidth+" inch.");
		};
		//Print Price
		this.pPrice = Math.round((this.selWidth*this.selHeight)*this.imagePriceRate);		
		$(".show_printPrice").html(this.pPrice);
		//Shipping Price
		this.sPrice = this.calcShipping(val);
		$(".show_shipPrice").html(this.sPrice);
		//Grand total
		this.currTotal = this.pPrice + this.sPrice;
		$(".show_totalPrice").html(this.currTotal);
	};
o.calcShipping = function(value){
	    var place;
	    if ((value>=8) && (value <=11.5)){place = 0;};
	    if ((value>=11.51) && (value<=13.5)){place = 1;};
	    if ((value>=13.51) && (value<=16)){place = 2;};	   
	    return (this.shipTab[getPosition(this.shipDests, this.shipDest)][place]);
	    	}
o.convertTomm = function (val){
	    var valmm;
	    valmm = Math.round(val * 25.4);
	    return valmm;
	};
o.initPanel = function (reqpanelNb){
		
	    if (parseInt(this.currPanel) == 2 && parseInt(reqpanelNb) !== 1)
	    {
		var test = this.checkDaForm();
		if (test === true){
		    //recup les vals du form
		    this.getFormValues();
		    //
		    this.writeFormValues();
		    this.switchPanel(reqpanelNb);		    
		    }
		    else {return;}
	    }
	    else {
		if (parseInt(reqpanelNb) === 3){
		    var test = this.checkDaForm();
		    if (test === true){
			//recup les vals du form
			this.getFormValues();
			//
			this.writeFormValues();
			this.switchPanel(reqpanelNb);		    
			}
			else {this.switchPanel(2);}		    
		    }	
		    else{this.switchPanel(reqpanelNb);};
		};
	};
o.switchPanel = function(reqpanel){
	    $("#step_"+this.currPanel).removeClass("stepselCurr");
	    $("#step_"+reqpanel).addClass("stepselCurr");	
	    $("#panel1, #panel2, #panel3").fadeOut("fast");
	    $("#panel"+reqpanel).fadeIn("fast");	    
	    this.currPanel = reqpanel;	
    };
o.checkDaForm = function (){
	    //clean up .error
	    $("#daform input, #daform span").removeClass("error");
	    $(".warning").fadeOut("fast");
	    var status = true;
	    $("#daform input").each(
		function(){
		    if ($(this).val() === ""){
			$(this).addClass("error").prev().addClass("error");
			$(".warning").fadeIn("fast");
			status = false;
		    };
		}		
	    );
	    if (!checkEmail($("#inpEmail").val())){$("#inpEmail").addClass("error").prev("span").addClass("error");status = false;$(".warning").fadeIn("fast");}	
	    return status;
	};
o.getFormValues = function(){
	    this.C = {
		    name: $("#inpName").val(),
		    email: $("#inpEmail").val(),
		    adr: $("#inpAdr").val(),
		    city: $("#inpCity").val(),
		    pcode: $("#inpPcode").val(),
		    state: $("#inpState").val(),
		    country: $("#inpCountry").val(),
		    comm: $("#comm").val(),		    
		};	
    };
o.writeFormValues = function (){
	    $.each(this.C, function(p,val){
		$(".show_C"+p+"").text(val);
	    });
	};
    //---------//
o.setUpListeners = function (){
	    $("#conv").toggle(
		function(){
		    o.showVals.reverse();
		    o.updateValues(o.currVal);		    	    
		},		
		function(){		    
		    o.showVals.reverse();
		    o.updateValues(o.currVal);
		}

	    );
	    $("#shipChoice span").click(function(){
		    o.shipDest = $(this).attr("id").split("_")[1];
		    $("#shipChoice span").removeClass("select");
		    $(this).addClass("select");
		    o.updateValues(o.currVal);
		}
	    );
	    $("#insideNav div").hover(
		function(){$(this).addClass("stepsel");},
		function(){$(this).removeClass("stepsel");}
	    );	    	
	    $("#insideNav div").click(function(){o.initPanel($(this).attr("id").split("_")[1]);});
	    $("#daform input,#daform textarea")
		.focus(function(){$(this).addClass("inpsel")})
		.blur(function(){$(this).removeClass("inpsel")})
		;
	    $(".nextBtn").click(function(){
		o.initPanel(parseInt(o.currPanel)+1);
		return false;
	    });
		$("#infoBtn").click(function(){
			$("#blocapp").fadeOut();
			$("#addInfos").fadeIn();
		});
		
	    $("#exitBtn").click(function(){
			$("#blocapp").fadeIn();
			$("#addInfos").fadeOut();
		});
		$("#submit").click(function(){
			o.Order = {
				client: o.C,
				imageID: o.imageID,
				printW:[o.selWidth,o.convertTomm(o.selWidth)],
				printH:[o.selHeight,o.convertTomm(o.selHeight)],
				filename: o.imageFilename,
				ship:o.shipDest,
				printCost:o.pPrice,
				shipCost:o.sPrice,
			} 
			o.dataStr = JSON.stringify(o.Order);
			$.post(
				"/assets/s_ordr.php",
				{
					data:o.dataStr
				},
				o.orderCB,
				"json"
			);
			$(this).text("Sending...").animate({color:"#555",backgroundColor:"#ccc"},"normal");
		});		
	};
o.orderCB = function (data, textstatus){
	if (data){
		o.initPanel(parseInt(o.currPanel)+1);
		$("#insideNav").fadeOut("fast");
	} else {
		o.initPanel(parseInt(o.currPanel)+2);
	}		
}
//---------------------------------------------------------------------------------
//--------------
  return o;
  })();

