
var W3CDOM = (document.createElement && document.getElementsByTagName);

function initFileUploads() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	fakeFileUpload.appendChild(document.createElement('input'));
	var image = document.createElement('img');
	image.src='/public/images/przegladaj.jpg';
	fakeFileUpload.appendChild(image);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}

$(document).ready(function(){
	initFileUploads();
	$("#color").colorPicker();
	
	var type = $("input[name=zmniejsz]:checked").val();
	zmniejsz(type);
	/*
	if ($("#btn_przeksztalc").length > 0) {
		$("#btn_przeksztalc input").bind("mouseover",
			function(object){
				$(this).css("background-image","url(/public/images/przeksztalc_over.jpg)").css("cursor","pointer");
			}
		);
		$("#btn_przeksztalc input").bind("mouseout",
			function(object){
				$(this).css("background-image","url(/public/images/przeksztalc.jpg)");
			}
		);
	}
	*/
	if ($("#link_regulamin").length > 0) {
		$("#link_regulamin input").bind("change",
			function(object){
				if ($(this).attr("checked"))
				{
					$("#btn_przeksztalc input").unbind("mouseover");
					$("#btn_przeksztalc input").unbind("mouseout");
					
					$("#btn_przeksztalc input").css("background-image","url(/public/images/przeksztalc.jpg)").css("cursor","pointer");
						
					$("#btn_przeksztalc input").bind("mouseover",
						function(object){
							$(this).css("background-image","url(/public/images/przeksztalc_over.jpg)").css("cursor","pointer");
						}
					);
					
					$("#btn_przeksztalc input").bind("mouseout",
						function(object){
							$(this).css("background-image","url(/public/images/przeksztalc.jpg)");
						}
					);
					
					$("#btn_przeksztalc input").bind("click",
						function(object){
							$("#main_form").submit();
						}
					);
				}
				else
				{
					$("#btn_przeksztalc input").css("background-image","url(/public/images/przeksztalc_bw.jpg)").css("cursor","pointer");
					
					$("#btn_przeksztalc input").unbind("mouseover");
					$("#btn_przeksztalc input").unbind("mouseout");
					
					$("#btn_przeksztalc input").bind("mouseover",
						function() {
							$("#btn_przeksztalc em").animate({opacity: "show", top: "-92"}, "slow");
						}
					);
						
					$("#btn_przeksztalc input").bind("mouseout",
						function() {
							$("#btn_przeksztalc em").animate({opacity: "hide", top: "-115"}, "fast");
						}
					);
					
					$("#btn_przeksztalc input").unbind("click");		
				}
			}
		);
	}
	
	$("#btn_przeksztalc input").bind("mouseover",
		function() {
			$("#btn_przeksztalc em").animate({opacity: "show", top: "-92"}, "slow");
		}
	);
		
	$("#btn_przeksztalc input").bind("mouseout",
		function() {
			$("#btn_przeksztalc em").animate({opacity: "hide", top: "-115"}, "fast");
		}
	);
	
	if ($("#add_text").length > 0) {
		
		txtDef = "wpisz tekst lub pozostaw puste...";
		var title = $("#text").attr("title");
		var color = "#a2a2a2";
		if (title != "")
		{
			txt = title;
			color = "#000";
		}
		else
		{
			txt = txtDef;
		}	
		
		//txt = "wpisz tekst lub pozostaw puste...";
		$("#add_text #text").val(txt).css("color",color).css("font-size","1.0em");
		$("#add_text #text").bind("focus",function() {
			if ($(this).val() == txtDef)
				$(this).val("").css("color","#000");
		});
		$("#add_text #text").bind("blur",function() {
			if ($(this).val() == "")
				$(this).val(txtDef).css("color","#a2a2a2").css("font-size","1.2em");
		});
	}
	
	if ($("#effect_boxes").length > 0) {
		$("input[name=zmniejsz]").bind("click", function(){
			var type = $(this).val();
			zmniejsz(type);
		});
		$("input[name=obrot]").bind("click", function(){
			var type = $(this).val();
			var src = "";
			
			if (type == "brak") {
				src = "/public/images/normal.png";
				//$("#obroc .effect_values").hide();
			} else if  (type == "prawo") {
				src = "/public/images/right.png";
				//$("#obroc .effect_values").show(); 
			} else if  (type == "lewo") {
				src = "/public/images/left.png";
				//$("#obroc .effect_values").show(); 
			} else if  (type == "180") {
				src = "/public/images/180.png";
				//$("#obroc .effect_values").show(); 
			}
			
			$("#obroc #rotate_image").attr("src",src);
		});
		$("input[name=efekt]").bind("click", function(){
			var type = $(this).val();
			var src = "";
			
			if (type == "brak") {
				//$("#efekt .effect_values").hide();
				src = "/public/images/normal.png";
			} else if  (type == "czarne") {
				src = "/public/images/grey.png";
				//$("#efekt .effect_values").show(); 
			} else if  (type == "sepia") {
				src = "/public/images/sepia.png";
				//$("#efekt .effect_values").show(); 
			} else if  (type == "odwroc") {
				src = "/public/images/reverse.png";
				//$("#efekt .effect_values").show(); 
			}
			
			$("#efekt #effect_image").attr("src",src);
		});
	}
	
	if ($("#font_wrapper").length > 0) {
		fonts();
	}
});

function zmniejsz(type)
{
	var options = new Array();
			
	if (type == "brak") {
		$("#zmniejsz_value_wrapper select option").remove();
		$("#zmniejsz .effect_values").hide(); 
	} else if  (type == "procent") {
		$("#zmniejsz_value_wrapper select option").remove();
		options.push(new Array("10", "10 %"));
		options.push(new Array("25", "25 %"));
		options.push(new Array("50", "50 %"));
		options.push(new Array("75", "75 %"));
		
		$("#zmniejsz .effect_values").show(); 
	} else if  (type == "szerokosc") {
		$("#zmniejsz_value_wrapper select option").remove();
		options.push(new Array("128", "128 pikseli"));
		options.push(new Array("320", "320 pikseli"));
		options.push(new Array("640", "640 pikseli"));
		options.push(new Array("800", "800 pikseli"));
		options.push(new Array("1024", "1024 pikseli"));
		
		$("#zmniejsz .effect_values").show();
	} else if  (type == "wysokosc") {
		$("#zmniejsz_value_wrapper select option").remove();
		options.push(new Array("240", "240 pikseli"));
		options.push(new Array("480", "480 pikseli"));
		options.push(new Array("600", "600 pikseli"));
		options.push(new Array("800", "800 pikseli"));
		
		$("#zmniejsz .effect_values").show();
	}
	
	$("#zmniejsz_value_wrapper > *").remove();
	$("#zmniejsz_value_wrapper").append($("<select></select>").attr("name", "zmniejsz_value").attr("id", "zmniejsz_value"));
	$.each(options, function(value, key) {
		$("#zmniejsz_value").append($("<option></option>").attr("value",key[0]).text(key[1])); 
	});
	
	$("#zmniejsz_value").sexyCombo({emptyText: "Wybierz rozmiar...", filterFn: function(value, option){return true;}});
	
	var zmniejsz_value = $("#zmniejsz_value_wrapper").attr("title");
	if (zmniejsz_value != "")
	{
		$("input[name=zmniejsz_value__sexyCombo]").val(zmniejsz_value);
		$("#zmniejsz_value_wrapper").attr("title", "");
	}
}

function fonts()
{
	var options = new Array();
	
	options.push(new Array("Arial", "Arial"));
	options.push(new Array("Arial Black", "Arial Black"));
	options.push(new Array("Comic Sans MS", "Comic Sans MS"));
	options.push(new Array("Courier New", "Courier New"));
	options.push(new Array("Georgia", "Georgia"));
	options.push(new Array("Impact", "Impact"));
	options.push(new Array("Lucida Console", "Lucida Console"));
	options.push(new Array("Tahoma", "Tahoma"));
	options.push(new Array("Times", "Times"));
	options.push(new Array("Verdana", "Verdana"));
	options.push(new Array("MS Sans Serif", "MS Sans Serif"));
	
	$("#font_wrapper").append($("<select></select>").attr("name", "font_type").attr("id", "font_type"));
	$.each(options, function(value, key) {
		$("#font_type").append($("<option></option>").attr("value",key[0]).text(key[1])); 
	});
	
	$("#font_type").sexyCombo({dropUp: true, filterFn: function(value, option){return true;}});
	
	$("#font_wrapper div.sexy li").each(function(li) {
		var font = $(this).text();
		//alert($(this).text());
		$(this).css("font-family",font);
	});
	
	$("#font_wrapper div.sexy li").bind("click", function(){
		var font = $("#font_wrapper div.sexy input").val();
		$("#font_wrapper div.sexy input").css("font-family",font);
		$("#font_wrapper #text").css("font-family",font);
	});
	
	$("div.combo input").attr("readonly", "readonly");
}