var product = {
	id : null,
	name : null,
	image : null,
	category_id : null,
	color_id : null,
	quantity : null,
	print_design : null,
	no_colors : null,
	pack : false,
	adds : null,
	
	checkData : function() {
		var checked = true;
		
		if(this.quantity >= 100 && this.quantity < 5000)
			jQuery("#calculator #qQuantity").removeClass("error");
		else { 
			checked = false;
			jQuery("#calculator #qQuantity").addClass("error");
			alert(calculator.lang['quantity_error']);
		}		
		return checked;
	},
	
	setDefaults : function() {
		this.id = null;
		this.name = null;
		this.image = null;
		this.category_id = null;
		this.color_id = null;
		this.quantity = null;
		this.print_design = null;
		this.no_colors = null;
		this.pack = false;
	}
};

var calculator = {
	status : 0,
	width : 518,
	height : 524,
	content_height: 430,
	opacity : 0.7,
	image_height : 150,
	thumbnail_height : 70,
	step : 0,
	fade_speed : "slow",
	lang : Array(8),
	
	// Trigger - after click on the element you will run the application
	trigger : function(){
		jQuery("li a").each(function(){
			if(jQuery(this).text() == "Kalkulator" || jQuery(this).text() == "Calculator") {
				jQuery(this).attr("href","javascript:calculator.center();calculator.load();");
			}
		});
	},
	
	load : function(){
		this.setDefaults();
		this.getLanguage();
		if(this.status==0){
			jQuery("#calculatorBackground").css({
				"opacity": this.opacity
			});
			jQuery("#calculatorBackground").fadeIn(this.fade_speed);
			jQuery("#calculator").fadeIn(this.fade_speed);
			this.status = 1;			
		}
	},

	disable : function(){
		if(this.status==1){
			jQuery("#calculatorBackground").fadeOut(this.fade_speed);
			jQuery("#calculator").fadeOut(this.fade_speed);
			this.status = 0;
		}
	},
	
	center : function(){
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var top = 0;
		var left = 0;
		
		if(windowHeight > this.height)
			top = windowHeight/2-this.height/2;
		if(windowWidth > this.width)
			left = windowWidth/2-this.width/2;
		
		jQuery("#calculator").css({
			"position": "absolute",
			"top": top,
			"left": left,
			"width" : this.width+"px",
			"height" : this.height+"px"
		});
		
		jQuery("#calculator #submit").css({"left" : this.width/2-40});
		
		//Hack : force for IE6
		jQuery("#calculatorBackground").css({
			"height": windowHeight
		});
	},
	
	getLanguage : function(){
		jQuery.getJSON("calculator.php",{stTable: 'language', fAjax: 'true'}, function(data){
			calculator.lang['name']=data.name;
			calculator.lang['quantity_error']=data.quantity_error;
			calculator.lang['indicator_loading']=data.indicator_loading;
			calculator.lang['choose_product']=data.choose_product;
			calculator.lang['choose_color']=data.choose_color;
			calculator.lang['print_options']=data.print_options;
			calculator.lang['summary']=data.summary;
		});
	},
	
	getCategories : function(){
		var categories = '';
		
		jQuery.getJSON("calculator.php",{stTable: 'categories', fAjax: 'true'}, function(data){
			for(var i = 0; i < data.length; i++){
				categories += '<option value=' + data[i].cat_k1_id + '>' + data[i].cat_name + '</option>';
			}
			jQuery("#calculator #categories").html(categories);
		});
	},
	
	getProducts : function(category){
		jQuery("#calculator #products").html("<div id='indicator'><img src='plugins/calculator/images/indicator.gif' alt='indicator'/><h2>" + this.lang['indicator_loading'] + "</h2></div>");
		var stProducts = '';
		var stOnClick = '';
		var oData = new Object();
		
		product.category_id = jQuery(category).val();
		jQuery.getJSON("calculator.php",{stTable: 'products', iCategory: jQuery(category).val(), fAjax: 'true'}, function(data){
			for(var i = 0; i < data.length; i++){
				oData = { id : data[i].pro_k1_id, name : data[i].pro_name, image : data[i].pro_image };
				stOnClick = "product.color_id=null; product.id=" + oData.id + "; product.name='" + oData.name + "'; product.image='" + oData.image + "'; calculator.getPrintOptions();";
				if(data[i].colors)
					stOnClick = "product.id=" + oData.id + "; calculator.getColors(" + oData.id + "); calculator.step=1;";
				stProducts += '<li class="product"><a href="#"  title="' + oData.name + '" onClick="' + stOnClick + '"><img src="plugins/calculator/images/' + oData.image + '" alt="' + oData.name + '"/></a></li>';
			}
			jQuery("#calculator ul#products").html(stProducts);
			jQuery("#calculator #products #indicator").hide();
		});
	},
	
	getColors : function(id){
		jQuery("#calculator #products").html("<div id='indicator'><img src='plugins/calculator/images/indicator.gif' alt='indicator'/><h2>" + this.lang['indicator_loading'] + "</h2></div>");
		var stProducts = '';
		var stOnClick = '';
		var oData = new Object();
		
		jQuery.getJSON("calculator.php",{stTable: 'products', iProduct: id, fAjax: 'true'}, function(data){
			for(var i = 0; i < data.colors.length; i++){
				oData = { name : data.pro_name, image: data.colors[i].att_value, color_id : data.colors[i].att_k1_id }
				stOnClick = "product.color_id=" + oData.color_id + "; product.name='" + oData.name + "'; product.image='" + oData.image + "'; calculator.getPrintOptions();";
				stProducts += '<li class="product"><a href="#" title="' + oData.name + '" onClick="' + stOnClick + '"><img src="plugins/calculator/images/' + oData.image + '" alt="' + oData.name + '"/></a></li>';
			}
			jQuery("#calculator ul#products").html(stProducts);
			if(data.colors.length > 0)
				return true;
			else
				return false;
			jQuery("#calculator #products #indicator").hide();
		});
	},
	
	getPrintOptions : function(){
		var adds;
		calculator.step=2;
		jQuery("#qAdditional option:selected").each(function() { jQuery(this).attr("selected",""); });
		jQuery("#calculator #product img.main").attr("src","plugins/calculator/images/" + product.image);
		jQuery("#calculator #product img.main").attr("alt",product.name);
		jQuery("#calculator #product div.description").text(product.name);
		jQuery("#calculator #questions").show();
		
		
		jQuery("#calculator #questions").load("calculator.php?stTable=questions&fAjax=true" +
				"&qQuantity=" + product.quantity +
				"&qPrintDesign=" + product.print_design +
				"&qColors=" + product.no_colors +
				"&qPackage=" + product.pack
		);
		//"&qAdds[12]=1&qAdds[23]=2&qAdds[45]=3"
	},
	
	setDefaults : function(){
		calculator.step = 0;
		product.setDefaults();
		
		jQuery("#calculator #products, #calculator #categories").show();
		jQuery("#calculator #questions, #calculator #calculatorPrevious, #calculator #calculatorNext, #calculator #product").hide();
		
		jQuery("#calculator #categories").attr("value","1");
		jQuery("#calculator #categories").trigger("change");
	}
};

jQuery(document).ready(function(){
	// Prepare trigger
	calculator.trigger();
	calculator.getLanguage();
	
	// Get categories list
	calculator.getCategories();

	// Event : window resized
	jQuery(window).resize(function(){
		calculator.center();
	});
				
	// Event : char 'x' clicked
	jQuery("#calculator #calculatorClose").click(function(){
		calculator.disable();
	});
	
	// Event : close icon clicked
	jQuery("#calculator #calculatorRefresh").click(function(){
		calculator.setDefaults();
	});
	
	// Event : area outside the popup clicked
	jQuery("#calculatorBackground").click(function(){
		calculator.disable();
	});
	
	// Event : key pressed
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && calculator.status==1){
			calculator.disable();
		}
		if(e.keyCode==13 && calculator.step==2 && calculator.status==1){
			jQuery("#calculator #submit").trigger("click");
			return false;
		}
	});
	
	// Event : category changed
	jQuery("#calculator #categories").change( function(){
		product.setDefaults();
		jQuery("#calculator").trigger("click");
		calculator.getProducts(this);
	});
	
	//jQuery("#calculator #qQuantity").unbind("change");
	jQuery("#calculator #qQuantity").change( function(){
		product.quantity = jQuery(this).val();
	});

	
	// Event : calculator area clicked
	jQuery("#calculator").click(function(){
		var query = new Object();
		
		jQuery("tbody").css({"width":"100%"});
		jQuery("#calculator #calculatorPrevious, #calculator #calculatorNext").unbind("click");
		
		switch( calculator.step ){
			case 0:
				query = { stTable: 'products', iProduct: product.id, fAjax: 'true'};
				
				jQuery("#calculator h1").text(calculator.lang['choose_product']);
				jQuery("#calculator #categories").show();
				jQuery("#calculator #calculatorPrevious, #calculator #submit, #calculator #text").hide();

				if(product.id!=null){
					jQuery("#calculator #calculatorNext").show();
					jQuery("#calculator #calculatorNext").click(function(){
						jQuery.getJSON("calculator.php", query, function(data){
							if(product.color_id){
								calculator.getColors(product.id)
								calculator.step = 1;
							} else {
								calculator.step = 2;
							}
							jQuery("#calculator").trigger("click");
						});
					});
				} else {
					jQuery("#calculator #calculatorNext").hide();
				}
				break;
			case 1:
				jQuery("#calculator h1").text(calculator.lang['choose_color']);
				jQuery("#calculator #categories, #calculator #calculatorPrevious").show();
				jQuery("#calculator #submit, #calculator #text").hide();
				jQuery("#calculator #calculatorPrevious").click(function(){
					calculator.getProducts(jQuery("#calculator #categories"));
					calculator.step=0;
				});				
				if(product.color_id!=null){
					jQuery("#calculator #calculatorNext").show();
					jQuery("#calculator #calculatorNext").click(function(){
						calculator.step = 2;
					});
				} else {
					jQuery("#calculator #calculatorNext").hide();
				}
				break;
			case 2:
				jQuery("#calculator h1").text(calculator.lang['print_options']);
				jQuery("#calculator #submit").attr("disabled","");
				jQuery("#calculator #products, #calculator #categories, #calculator #calculatorNext, #calculator #submit, #calculator #text").hide();
				jQuery("#calculator #submit, #calculator #questions, #calculator #product, #calculator #calculatorPrevious").show();
				if(product.color_id != null) {
					jQuery("#calculator #calculatorPrevious").click(function(){
						calculator.getColors(product.id);
						calculator.step=1;
						jQuery("#calculator #questions, #calculator #product").hide();
						jQuery("#calculator #products").show();
					});
				} else {
					jQuery("#calculator #calculatorPrevious").click(function(){
						calculator.getProducts(jQuery("#categories"));
						calculator.step=0;
						jQuery("#calculator #questions, #calculator #product").hide();
						jQuery("#calculator #products").show();
					});
				}
				break;
			case 3:
				jQuery("#calculator h1").text(calculator.lang['summary']);
				jQuery("#calculator #submit, #calculator #calculatorNext, #calculator #products").hide();				
				jQuery("#calculator #questions, #calculator #product, #calculator #text").show();
				jQuery("#calculator #calculatorPrevious").click(function(){
					calculator.step = 2;
					calculator.getPrintOptions();
				});
				break;
		}
	});

	// Event : submit button clicked
	jQuery("#calculator #submit").click(function(){
		//jQuery("#calculator #questions").html("<div id='indicator'><img src='plugins/calculator/images/indicator.gif' alt='indicator'/><h2>" + calculator.+ "</h2></div>");
		var options = new Array();
		var post_vars = new Object();
		
		product.quantity = jQuery("#calculator #qQuantity").val();
		product.print_design = jQuery("#calculator #qPrintDesign").val();
		product.pack = jQuery("#calculator #qPackage").val();
		product.no_colors = jQuery("#calculator #qColors").val();
		jQuery("#calculator .checkList input:checked").each( function(id){
			var checkbox = jQuery("#calculator .checkList input:checked").get(id);
			options[checkbox.id] = checkbox.value;
		 });
		product.adds = options;
		
		if(product.checkData()) {
			post_vars = { stTable: "calculation", fAjax : true, iProduct : product.id, iAttribute : product.color_id,
					      qQuantity : product.quantity, qPackage : product.pack, qPrintDesign : product.print_design,
					      qColors : product.no_colors, 'ch[]' : options };
			jQuery.post("calculator.php", post_vars, function(data){
				calculator.step = 3;
				jQuery("#calculator #questions").html(data);
				jQuery("#calculator").trigger("click");
			});
		}
		return false;
	});
});
