// global language setting
var lang = readCookie("lang");
lang = lang.toLowerCase();
if (lang != 'pl') { lang = 'en' }

var texty = ["pl","en"];

texty["pl"] = {
    noIE: "<br /><strong>Przepraszamy! Twoja przeglądarka (Internet Explorer) nie współpracuje prawidłowo z tą stroną.</strong><br /><br />Jeśli chcesz zobaczyć zawartość tej strony, prosimy o aktualizację do wersji minimum 7.0, lub <a href='http://getfirefox.org'>zmienić przeglądarkę</a>.",
    loading: "ŁADOWANIE...",
    clickToClose: "kliknij na obrazek, aby zamknac okno"
}

texty["en"] = {
    noIE: "<br /><strong>Sorry! Your browser (Internet Explorer 6) doesn't work propertly with this page.</strong><br /><br />If you'd like to see this content, please consider upgrading Your browser to version 7 or higher, or use <a href='http://getfirefox.org'>alternative browser</a>.",
    loading: "LOADING...",
    clickToClose: "Click on image, to close the window"
}

// Main app
$(document).ready(function() {

    // NO IE6- !!! All IE browsers lower than 7 are CSS/XML uncompilant and unsecure! They don't support alpha channel in PNG images, so this site will not shown properly.
    // We need to block this browsers.
    if(navigator.userAgent.indexOf("MSIE 6")>=0){
	$(function(){
		
		$("<div id='noie6'>")
			.css({
				'position': 'absolute',
				'top': '0px',
				'left': '0px',
				backgroundColor: 'black',
				'opacity': '0.8',
				'width': $(window).width(),
				'height': $(window).height(),
				zIndex: 50000
			})
			.appendTo("body");

		$("<div><img src='images/no-ie6.png' alt='' style='float: left;'/><p>" + $texty[lang].noIE + "</p>")
			.css({
				backgroundColor: 'white',
				'opacity': '1',
				'top': '50%',
				'left': '50%',
				marginLeft: -210,
				marginTop: -100,
				width: 410,
				paddingRight: 10,
				height: 200,
				'position': 'absolute',
				zIndex: 50001
			})
			.appendTo("body");

		$("#wrapper").hide();
	});
    }

    // Main function
    $(function() { 

        // inicjalizacja efektów menu
	var $menu = $("#1");
	$menu.lavaLamp({ 
	    fx: "easeOutCirc", 
	    speed: 500,
	    click: function(event, menuItem) {return true}
	});

	$('#1 li:has(a span.parent)').each(function(i) {
	    var $submenu=$(this).find('div:first');
	    $(this).hover(
		function(e) {
		    $submenu.stop(true, true).delay(150).animate({height: 'show'}, {duration: 700, easing: 'easeOutBounce'});
		}, function(e) {
		    $submenu.stop(true, true).delay(150).animate({height:'hide'}, {duration: 300, easing: 'linear'});
		}
	    );
	});

	// inicjalizacja efektów strony
	var initDelay = 3000;

	$('.box').fadeIn(initDelay);
	$('.owoc').delay(initDelay).each(function(i) {
	    $(this).delay(Math.floor(Math.random()*2000+1)).animate({
		width: '60px',
		height: '60px',
		left: '-=30px',
		top: '-=30px'
	    }, {
		duration: 1000, 
		easing: 'easeOutElastic'
	    });
	});

	$('#slogan1').delay(initDelay+4000).fadeIn(5000);
	$('#glow').delay(initDelay+7000).animate({left: '-=50px', opacity: 0.7},3000).animate({left: '-=50px', opacity: 0},3000);

	// aktywacja linków
	var hash = window.location;
	var href = $('a[href]:not(.ex)').each(function() {
	    var href = $(this).attr('href');
	    if(hash==href) {
		var content = getURLVar(document.location, "content");
		var toLoad = "get_ajax.php?content=" + content + "&lang=" + lang;
		loadContent(toLoad);
	    }
	});

	$('a[href]:not(.ex)').click(function() {
	    var content = getURLVar($(this).attr('href'),"content");
	    var toLoad = "get_ajax.php?content=" + content + "&lang=" + readCookie("lang");
	    $('.box').fadeOut(4000,loadContent());
	    $('#load').remove();
	    $('<div id="load">'+texty[lang].loading+'</div>').insertBefore('.box');
	    $('#load').fadeIn(1000);
	    window.location = $(this).attr('href');
	    return false;
	});
    });
});


// Functions
function loadContent(toLoad) {
    $('.box').get(toLoad, '', showNewContent());
}

function showNewContent() {
    hideLoader();
    $('.box').fadeIn(4000);
}

function hideLoader() {
    $('#load').fadeOut(1000);
}

function displayWindow(title, url, width, height)
{
    var win = window.open("", "_blank", 'directories=no,channelmode=no,resizable=no,scrollbars=no,menubar=no,location=no,toolbar=no,status=no,fullscreen=no,titlebar=no,width=' + width +',height=' + height + "'");
    win.document.open("text/html", "replace");
    win.document.write('<html><head><title>' + title + '</title></head>');
    win.document.write('<body oncontextmenu="return false" bgcolor="#f0f0f0" topmargin="10" leftmargin="10" marginheight="10" marginwidth="10"><div align="center"><img border="0" src="' + url + '" onclick="javascript:window.close()"><p align="center"><font size="-2" color="red" face="arial">');
    win.document.write(texty[lang].clickToClose);
    win.document.write(')</font></p></div></body>');
    win.document.write('</html>');
    win.document.close();
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function createCookie(name,value,days) {
    if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
    } else {
	var expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

// this deletes the cookie when called
function deleteCookie(name) {
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = name += "=; expires=" + cookie_date.toGMTString();
}

function switchLang(newLang) {
    deleteCookie("lang");
    createCookie("lang", newLang);
    window.location.reload();
}

function getURLVar(urlstring, urlVarName) {
    //divide the URL in half at the '?'
    if (urlstring == "") { urlstring = document.location; }
    var urlHalves = String(urlstring).split('?');
    var urlVarValue = '';
    if(urlHalves[1]){
	//load all the name/value pairs into an array
	var urlVars = urlHalves[1].split('&');
	//loop over the list, and find the specified url variable
	for(i=0; i<=(urlVars.length); i++){
	    if(urlVars[i]){
		//load the name/value pair into an array
		var urlVarPair = urlVars[i].split('=');
		if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
		    //I found a variable that matches, load it's value into the return variable
		    urlVarValue = urlVarPair[1];
		}
	    }
	}
    }
    return urlVarValue;
}

function displayWindow(title, url, width, height) {
    var win = window.open("", "_blank", 'directories=no,channelmode=no,resizable=no,scrollbars=no,menubar=no,location=no,toolbar=no,status=no,fullscreen=no,titlebar=no,width=' + width +',height=' + height + "'");
    win.document.open("text/html", "replace");
    win.document.write('<html><head><title>' + title + '</title></head>');
    win.document.write('<body oncontextmenu="return false" bgcolor="#f0f0f0" topmargin="10" leftmargin="10" marginheight="10" marginwidth="10"><div align="center"><img border="0" src="' + url + '" onclick="javascript:window.close()"><p align="center"><font size="-2" color="red" face="arial">(kliknij na obrazek, aby zamknac)</font></p></div></body>');
    win.document.write('</html>');
    win.document.close();
}
