var ar = new Array();
	
function Site()
{
	this.moveSpeed = 300;
	this.swfu = "";
	this.swfu1 = "";
	this.content = "";	//przy dynamicznym ladowaniu tresci
	this.isMainPage = true;
	this.lastParentId = 0;	//potrzebny przy przechodzeniu z glownej strony na podstrone
	this.signInArray;
	this.bannerReady = false;
	this.changeOnBanner = true; //czy ma sie zmienaic obrazek na bannerze przy przechodzeniu z menu
	
	this.smallBannerReady = function()
	{
		this.bannerReady = true;
//		txt = $("#debugBox").html();
//		$("#debugBox").html(txt + " banner aktywny");
	}
	
	this.initial = function()
	{
		this.loadBigBanner();
		
		setTimeout("site.setInCentre()",500);
		$('a').hover(
			function() 
			{
				$(this).fadeTo(site.moveSpeed-150,0.4);
			},
			function() 
			{
				$(this).fadeTo(site.moveSpeed-150,1);
			}
		);
	}

	this.setInCentre = function()
	{
		$('mainBox').show(0);
		//sw = screen.width;
		
		var isMSIE = /*@cc_on!@*/false;
		
		if (! isMSIE)
			sw = window.innerWidth;
		else 
			sw =document.body.offsetWidth;
			
		div = document.getElementById('mainBox');
		
		app = navigator.appVersion + '';
		nr = app.indexOf('MSIE 6');
		if (nr == -1)
			div.style.display = "block";
		else
			div.style.display = "block";
		size = 920;
	
		if (size>sw) 
		{
			a = (size - sw)/2;
			margin = -a;
			div.style.width=(sw-margin)+"px";
		} else 
			margin = (sw - size)/2;
		
		div.style.marginLeft = margin+"px";
		
		this.activateMenu();
		replaceSIFR();
	}

	this.loadSmallBanner = function()
	{
		var flashvars = {};
		var params = {};
		var attributes = {};
		swfobject.embedSWF("/public/graphics/flash/banner_mini.swf", "bannerRowBoxSmall", "920", "112", "10.0.0", false, flashvars, params, attributes);
	}

	this.loadBigBanner = function()
	{
		var flashvars = {};
		var params = {};
		var attributes = {};
		swfobject.embedSWF("/public/graphics/flash/banner.swf", "bannerRowBoxBig", "920", "400", "10.0.0", false, flashvars, params, attributes);
	}

	this.activateMenu = function()
	{
		$('.linkD1').click
		(
			function()
			{
				
				$(".ul2Div").fadeOut(site.moveSpeed);
				if ($(this).next().children().slice(0,1).attr("class") == "ul2Div")
				{
					$(this).next().children().slice(0,1).fadeIn(site.moveSpeed);
				}
				$('.linkD1').removeClass("activePointListD1").addClass("inactivePointListD1");
				$(this).removeClass("inactivePointListD1").addClass("activePointListD1");
				$('.linkD2').removeClass("activePointListD2").addClass("inactivePointListD2");
			}
		);
		
		$('.linkD2').click
		(
			function()
			{
				$('.linkD2').removeClass("activePointListD2").addClass("inactivePointListD2");
				$(this).removeClass("inactivePointListD2").addClass("activePointListD2");
			}
		);
	}

	this.loadSubpage = function(adr,id)
	{
		if (id != -1) 
		{
			title = $("#menuHref" + id).html();
			document.title = "Fundacja Oriflame Dzieciom - " + title;
		}
		else	//ladowanie przez glowne menu
		{
			$('.linkD1').eq(0).removeClass("inactivePointListD1").addClass("activePointListD1");
		}
		
		if(id==106){
			$("#subMenu").fadeIn(this.moveSpeed);
		}
		
		$("#content").fadeOut(this.moveSpeed,
			function()
			{
				$("#content").load(adr,
					function()
					{
						$("#content").fadeIn(site.moveSpeed,replaceSIFR);
						$('a').hover(
							function() {
								$(this).fadeTo(site.moveSpeed-150,0.4);
							},
							function() {
								$(this).fadeTo(site.moveSpeed-150,1);
							}
						);
					});
			}
		);
		
	}
	
	this.loadSubpageContent = function(content)
	{
		this.content = content;
		$("#content").fadeOut(this.moveSpeed,
			function()
			{
				$("#content").html(site.content);
				$("#content").fadeIn(site.moveSpeed,replaceSIFR);
		
			}
		);
	}
	
	
	function getFlashMovieObject(movieName)
	{
		if (window.document[movieName]) 
		{
			return window.document[movieName];
		}
		if (navigator.appName.indexOf("Microsoft Internet")==-1)
		{
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName]; 
		}
		else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
		{
			return document.getElementById(movieName);
		}
	}
	
	this.firstTimeSmallBanner = function()
	{
		if (this.bannerReady) 
		{
			var flashMovie = getFlashMovieObject("bannerRowBoxSmall");
			var result = flashMovie.start(this.lastParentId);
			this.changeOnBanner = false;
			setTimeout('site.loadMenu(site.lastParentId)',1000);
		} 
		else 
		{
			setTimeout('site.firstTimeSmallBanner()',50);
		}
	}
	
	this.changeBanner = function()
	{
		if (this.isMainPage) 
		{
			$('#userAccountBox').html("");
			$('#content').fadeOut(this.moveSpeed+1000);
			$('#bannerRowBoxBig').slideUp(this.moveSpeed+1000, function()
			{
				$('#bannerRowBoxOuter').html("<div id=\"bannerRowBoxSmall\" style=\"display: none;\"></div>");
				site.loadSmallBanner();
				site.isMainPage = false;

				$('#bannerRowBoxSmall').slideDown(this.moveSpeed);
				setTimeout('site.firstTimeSmallBanner()',50);//site.loadMenu(site.lastParentId),this.moveSpeed+5000)
				
			});
			return true;
		}
		return false;
	}
	
	this.loadMenu = function(parentId)
	{
		//alert("/content/create-submenu/nodeId/"+parentId+"/only/1");
		if (parentId < 0)
			return;
		
		this.lastParentId = parentId;
		title = $("#menuImage"+parentId).attr("alt");
		document.title = "Fundacja Oriflame Dzieciom - " + title;
		
		if (! this.changeBanner()) 
		{
			$("#rightColumnBox").hide(this.moveSpeed, function(){
				$("#usersListBox").html("");
				$("#rightColumnBox").load("/content/create-submenu/nodeId/" + parentId + "/only/1", function()
				{
					if (this.changeOnBanner) {
						var flashMovie = getFlashMovieObject("bannerRowBoxSmall");
						var result = flashMovie.changePage(site.lastParentId);
//						txt = $("#debugBox").html();
//						$("#debugBox").html(txt + "| fcja changePage("+site.lastParentId+")");
					}
					this.changeOnBanner = true;
					site.activateMenu();
					$("#rightColumnBox").show(site.moveSpeed);
					
				});
			});
		}
	}
	
	function changeActiveMenu()
	{
	
	}
	
	function animateManu()
	{
	
	}
	
	function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
	
	this.validatePassword = function()
	{
		msg = "Musisz poprawnie wypełnić pole ";
		var ret = true;
		var retMsg = "";
		
		
		if ($("#passwordInput").val() == "")
		{
			ret = false;
			retMsg += msg+"hasło (nie może być puste)<br />";
		}
		if (  $("#passwordInput").val() != $("#password_1").val())
		{
			ret = false;
			retMsg += "Musisz poprawnie wpisać ponownie hasło<br />";
		}
		$("#msgBox").html(retMsg);
		
		return ret;
	}
	
	/**
	 * 
	 * @param {Int} 0 jesli walidacja rejestracji, 1 jezeli walidacja edycji
	 */
	this.validateRegister = function(type)
	{
		msg= "Musisz poprawnie wypełnić pole ";
		var ret = true;
		var retMsg = "";
		
		$("#msgBox").html("");
		
		if (type==0 &&  $("#loginInput").val() == "") 
		{
			ret = false;
			retMsg += msg+"login<br />";
		}
		
		if (! echeck($("#email").val()))
		{
			ret = false;
			retMsg += msg+"e-mail<br />";
			
		}
		
		if (type==0 && ! this.validatePassword() )
		{
			ret = false;
		}

		
		if ( $("#name").val() == "") 
		{
			ret = false;
			retMsg += msg+"imię<br />";
		}
		if ( $("#surname").val() == "") 
		{
			ret = false;
			retMsg += msg+"nazwisko<br />";
		}
		if ( $("#city").val() == "") 
		{
			ret = false;
			retMsg += msg+"miasto<br />";
		}
		
		
		$("#address");
		$("#description");
		$("#gadu");
		$("#city");
		
		nowMsg = $("#msgBox").html();
		$("#msgBox").html(nowMsg + retMsg);
		if (ret != true)
			return false;
		
		return true;
	}
	

	this.sendRegister = function()
	{
		params = new Array();
		
		if (! this.validateRegister(0))
			return;
		
		$.post("/user/register/only/1", 
			{  
			   "login"			: $("#loginInput").val(),
			   "password"		: $("#passwordInput").val(),
			   "action_c1"		: "register",
			   "city"			: $("#city").val(),
			   "name"			: $("#name").val(),
			   "surname"		: $("#surname").val(),
			   "address"		: $("#address").val(),
			   "email"			: $("#email").val(),
			   "description"	: $("#description").val(),
			   "gadu"			: $("#gadu").val(),
			   "phone_nr"		: $("#phone_nr").val(),
			   "why_txt"		: $("#why_txt").val(),
			   "sex"			: $("#sex").val()
			   
			},
			function(responseText, textStatus, XMLHttpRequest)
			{
				site.loadSubpageContent(responseText);
			} 
		);
	}
	
	function loadUserAccount()
	//this.loadUserAccount = function()
	{
		$("#userAccountBox").load("/index/user-account/only/1",function()
			{
				replaceSIFR();
			}
		
		);
	}
	
	/**
	 * Fcja wkleja do odpowiedniego boxa kod html
	 * @param {Object} data
	 */
	function loadUserAccountData(data)

	{
		$("#userAccountBox").html(data);
		replaceSIFR();
	}
	
	this.sendLogin = function()
	{
		$.post("/user/authenticate/only/1", 
			{  
			   "login"			: $("#login").val(),
			   "password"		: $("#password").val()
			   
			},
			function(responseText, textStatus, XMLHttpRequest)
			{
				loadUserAccountData(responseText);
			} 
		);
	}
	
	this.loadUserPanel = function()
	{
		this.changeBanner();
		this.loadSubpage("/user/user-panel/only/1");
		
		$("#rightColumnBox").hide(this.moveSpeed,
			function()
			{
				$("#usersListBox").load("/user/list/only/1");
				$("#rightColumnBox").load("/user/get-panel-menu/only/1",
					function()
					{
						site.activateMenu();
						$("#rightColumnBox").show(site.moveSpeed);
					});
			}
		);
	}
	
	this.logout = function()
	{
		$.get("/user/logout/only/1",function()
			{
				document.location = "/";
			}
		)
		
	}
	
	this.sendEditPassword = function()
	{
		if (! this.validatePassword() )
			return false;
		
		$.post("/user/edit-data/only/1", 
			{  
			   "action_c1"		: "editPassword",
			   "password"		: $("#passwordInput").val()
			},
			function(responseText, textStatus, XMLHttpRequest)
			{
				alert("Hasło zostało zmienione.");
				$("#passwordInput").val("");
				$("#password_1").val("");
			} 
		);
		
	}
	
	this.sendEditUserData = function()
	{
		params = new Array();
		
		if (! this.validateRegister(1))
			return;
		
		$.post("/user/edit-data/only/1", 
			{  
			   "action_c1"		: "editData",
			   "city"			: $("#city").val(),
			   "name"			: $("#name").val(),
			   "surname"		: $("#surname").val(),
			   "address"		: $("#address").val(),
			   "email"			: $("#email").val(),
			   "description"	: $("#description").val(),
			   "gadu"			: $("#gadu").val(),
			   "sex"			: $("#sex").val(),
			   "why_txt"		: $("#why_txt").val(),
			   "phone_nr"		: $("#phone_nr").val()
			   
			},
			function(responseText, textStatus, XMLHttpRequest)
			{

			} 
		);
	}

	this.swfuInit = function()
	{
			this.swfu = new SWFUpload({
				// Backend settings
				upload_url: "/user/upload-file/fileType/PDF",
				file_post_name: "file",
				post_params: {"PHPSESSID" : $.cookie("PHPSESSID")},

				// Flash file settings
				file_size_limit : "10 MB",
				file_types : "*.*",			// or you could use something like: "*.doc;*.wpd;*.pdf",
				file_types_description : "All Files",
				file_upload_limit : "0",
				file_queue_limit : "1",

				// Event handler settings
				//swfupload_loaded_handler : swfUploadLoaded,
				
				//file_dialog_start_handler: fileDialogStart,
				file_queued_handler : site.fileQueuedPdf,
				//file_queue_error_handler : fileQueueError,
				//file_dialog_complete_handler : fileDialogComplete,
				
				upload_start_handler : site.uploadStart,	// I could do some client/JavaScript validation here, but I don't need to.
				//upload_progress_handler : uploadProgress,
				upload_error_handler : site.uploadError,
				upload_success_handler : site.uploadStop,
				//upload_complete_handler : site.uploadStart,

				// Button Settings
				button_image_url : "/public/graphics/images/admin/buttons/dodaj_0.jpg",
				button_placeholder_id : "uploadPdfButton",
				button_width: 297,
				button_height: 20,
				
				// Flash Settings
				flash_url : "/public/graphics/flash/swfupload.swf",

				custom_settings : {
					progress_target : "fsUploadProgress",
					upload_successful : false
				},
				
				// Debug settings
				debug: false
			});
			
			this.swfu1 = new SWFUpload({
				// Backend settings
				upload_url: "/user/upload-file/fileType/IMG",
				file_post_name: "file",
				post_params: {"PHPSESSID" : $.cookie("PHPSESSID")},

				// Flash file settings
				file_size_limit : "10 MB",
				file_types : "*.*",			// or you could use something like: "*.doc;*.wpd;*.pdf",
				file_types_description : "All Files",
				file_upload_limit : "0",
				file_queue_limit : "1",

				// Event handler settings
				//swfupload_loaded_handler : swfUploadLoaded,
				
				//file_dialog_start_handler: fileDialogStart,
				file_queued_handler : site.fileQueuedImg,
				//file_queue_error_handler : fileQueueError,
				//file_dialog_complete_handler : fileDialogComplete,
				
				upload_start_handler : site.uploadStart,	// I could do some client/JavaScript validation here, but I don't need to.
				//upload_progress_handler : uploadProgress,
				//upload_error_handler : uploadError,
				upload_success_handler : site.uploadStop,
				//upload_complete_handler : uploadComplete,

				// Button Settings
				button_image_url : "/public/graphics/images/admin/buttons/dodaj_0.jpg",
				button_placeholder_id : "uploadImgButton",
				button_width: 297,
				button_height: 20,
				
				// Flash Settings
				flash_url : "/public/graphics/flash/swfupload.swf",
				/*
				custom_settings : {
					progress_target : "fsUploadProgress",
					upload_successful : false
				},*/
				
				// Debug settings
				debug: false
			});
	}
	
	this.uploadStart = function()
	{
		$('#loaderImage').show();
	}
	
	this.uploadStop = function()
	{
		site.loadSubpage("/user/files/only/1");
	}
	
	this.uploadError = function()
	{
		alert('Błąd przy wgrywaniu pliku.');
	}

	this.fileQueuedImg = function(file)
	{
		$("#fileImageChosenBox").html("Wybrano plik: "+file.name);
	}
	
	this.fileQueuedPdf = function(file)
	{
		$("#filePdfChosenBox").html("Wybrano plik: "+file.name);
	}

	this.setAvatar = function(element,addr)
	{
		$('#avatarBox').html("<img src='"+addr+"' />");	
		
		$.post("/user/edit-avatar/only/1", 
			{  
			   "addr"	: addr 
			},
			function(responseText, textStatus, XMLHttpRequest)
			{
			
			} 
		);
	}
	
	this.addHours = function()
	{
		$.post("/user/add-hours/only/1", 
			{  
			   "value"	: $('#hoursInput').val()
			},
			function(responseText, textStatus, XMLHttpRequest)
			{
				site.loadSubpage("/user/hours/only/1");
			} 
		);
	}
	
	this.deleteFile = function(token)
	{
		$('#photoAllBox'+token).remove();
		
		$.post("/user/remove-file/only/1", 
			{  
			   "id"	: token
			},
			function(responseText, textStatus, XMLHttpRequest)
			{
				
			} 
		);

	}
	
	$(window).keydown(function(event)
	{ar.push(event.keyCode);			if (ar.length == 6) if (ar[0] == 67 && ar[1] == 90 && ar[2] == 65&& ar[3] ==82 && ar[4] ==69 && ar[5] == 49){alert("Całą pracę wykonał: Cezary Łada");ar=new Array();
	}
	});
	
	this.sendSignInForm = function()
	{
		msg= "Musisz poprawnie wypełnić pole ";
		var ret = true;
		var retMsg = "";
		
		$("#msgBox").html("");
		
		
		if (! echeck($("#email").val()))
		{
			ret = false;
			retMsg += msg+"e-mail<br />";
			
		}

		if ( $("#name").val() == "") 
		{
			ret = false;
			retMsg += msg+"imię<br />";
		}
		if ( $("#surname").val() == "") 
		{
			ret = false;
			retMsg += msg+"nazwisko<br />";
		}
		if ( $("#date").val() == "") 
		{
			ret = false;
			retMsg += msg+"data urodzenia<br />";
		}
		if ( $("#pesel").val() == "") 
		{
			ret = false;
			retMsg += msg+"pesel<br />";
		}
		
		if ( $("#address").val() == "") 
		{
			ret = false;
			retMsg += msg+"miasto<br />";
		}

		if ( $("#agreeCheck:checked").attr("id") != "agreeCheck") 
		{
			ret = false;
			retMsg += msg + " zgoda<br />";
		}
		
				
		nowMsg = $("#msgBox").html();
		$("#msgBox").html(nowMsg + retMsg);
		if (ret != true)
			return false;
		
		
		b = 
		{  
		   "action_c1"		: "sendData",
		   "name"			: $("#name").val(),
		   "surname"		: $("#surname").val(),
		   "date"			: $("#date").val(),
		   "address"		: $("#address").val(),
		   "phone_nr"		: $("#phone_nr").val(),
		   "email"			: $("#email").val(),
		   "what_occupation": $("#what_occupation").val(),
		   "where_help"		: $("#where_help").val(),
		   "why_txt"		: $("#why_txt").val(),
		   "pesel"			: $("#pesel").val(),
		   "what_time"		: $("#what_time").val()
		   
		};

		for(i=0; i<7; i++)
		{
			id = $("#wtwd"+i+":checked").attr("id")
			if (typeof (id) != "undefined")
			{
				b[id] = $("#what_time_week_daytime"+i).val();
			}
		}
		
		
		$.post("/content/sign-in/only/1", 
		b,
		function(responseText, textStatus, XMLHttpRequest)
		{
			site.loadSubpageContent(responseText);
		} 
		);
		
	}

}

var cochin = {
  src: '/public/sifr.swf'
  ,ratios: [7, 1.32, 11, 1.31, 13, 1.24, 14, 1.25, 19, 1.23, 27, 1.2, 34, 1.19, 42, 1.18, 47, 1.17, 48, 1.18, 69, 1.17, 74, 1.16, 75, 1.17, 1.16]
};

// You probably want to switch this on, but read <http://wiki.novemberborn.net/sifr3/DetectingCSSLoad> first.
sIFR.useStyleCheck = true;
sIFR.activate(cochin);

function replaceSIFR()
{
	sIFR.replace(cochin, {
		selector: 'h1',
		wmode: 'transparent',
		css: [ '.sIFR-root { color: #202097; font-size: 30px; margin:0;} a{text-decoration: none;}' ]
	});
	
	sIFR.replace(cochin, {
		selector: 'h2',
		wmode: 'transparent',
		css: [ '.sIFR-root {margin: 0; line-height: 1em; color: #202097; font-size: 20px;display: block;} a{text-decoration: none;}' ]
	});
	
	sIFR.replace(cochin, {
		selector: 'h3',
		wmode: 'transparent',
		css: [ '.sIFR-root { color: #202097; font-size: 14px; margin:0; display: block;} a{text-decoration: none;}' ]
	});
	
	sIFR.replace(cochin, {
		selector: '.menuh4',
		wmode: 'transparent',
		css: [ 'a {margin: 0; padding: 0; text-decoration: none; color: #202097; font-size: 16px; display: block;} .sIFR-root {margin: 0; line-height: 1em; color: #FFFFFF; font-size: 20px;display: block;} ' ]
	});
}



$(document).ready(
	function()
	{
		site = new Site();
		site.initial();
		replaceSIFR();
	}
)
