$(document).ready(function(){
	$('#nav li').hover(function(){
		$(this).addClass('hovered');
	},function(){
		$(this).removeClass('hovered');
	});
	$('#cityPicker').click(function(){
		$(this).toggleClass('hovered');
		$(this).find('a').blur();
	});
	$('.map .head a.mapa').click(function(e){
		e.preventDefault();
		$('.map .head a').removeClass('active');
		$(this).addClass('active');
		$('.map ul').hide();
		$('.map img').show();
	});
	$('.map .head a.lista').click(function(e){
		e.preventDefault();
		$('.map .head a').removeClass('active');
		$(this).addClass('active');
		$('.map img').hide();
		$('.map ul').show();
	});
});


var login_menu_bckgr = new Image(173,201)
login_menu_bckgr.src = "layout/IMG/navH.gif"
var main_bckgr = new Image(732,1179)
main_bckgr.src = "layout/IMG/bg4.jpg"


function gebID(id) {
	var el;
	if(el = document.getElementById(id))
		return el;
	else
		return false;
}

function drukuj_kupon(hash) {
	off_prt = window.open('print.php?hash='+hash,'off_prt','width=910,height=480,top=100,left=40,resizable=no,scrollbars=yes,menubar=no,location=no,status=no,toolbar=no')
	off_prt.opener = this.window;
//	off_prt.document.write('<html><head><title>Drukuj kupon: ' + hash + '</title></head><body style="padding:0px; margin:0px;"><img src="layout/IMG/wzor_kuponu.jpg" /></body></html>');
	off_prt.focus();
}

function set_colums_size() {

	var c0div, c1div, temp, main;

	if(document.getElementById('c0') && document.getElementById('c1')) {

		c0div = document.getElementById('c0');
		c1div = document.getElementById('c1');

	} else if(document.getElementById('main') && document.getElementById('c1')) {

		main = document.getElementById('main');
		c1div = document.getElementById('c1');

		if(main.firstChild.nodeName.toLowerCase() == '#text')
			temp = main.firstChild.nextSibling;
		else
			temp = main.firstChild;

		if(temp.firstChild.nodeName.toLowerCase() == '#text')
			c0div = temp.firstChild.nextSibling;
		else
			c0div = temp.firstChild;
	}
	
	if(c0div.nodeName.toLowerCase()=='div' && c1div.nodeName.toLowerCase()=='div') {
		if(c0div.clientHeight > c1div.clientHeight)
			c1div.style.height = c0div.clientHeight+'px';
		else {
			c0div.style.height = c1div.clientHeight+'px';
			if(document.getElementById('r1') && c0div) {
			
				document.getElementById('r1').style.height = parseInt(c0div.clientHeight - 63)+'px';
			}
		}
	}
	

}
//------------------------------------------------------------------------
function docWidth() {
	if (typeof(window.innerWidth)=='number') {
		return window.innerWidth;
	} else if(document.documentElement && document.documentElement.clientWidth) {
		return document.documentElement.clientWidth;
	} else if(document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	}
}
//------------------------------------------------------------------------
function docHeight() {
	if (typeof(window.innerHeight)=='number')
		return window.innerHeight;
	else if(document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else if(document.body && document.body.clientHeight)
		return document.body.clientHeight;
}
//------------------------------------------------------------------------
function get_transparent_background() {
		var wys  = docHeight();
		var szer = docWidth();
		var adv_alert_bgnd = document.createElement('div');
		adv_alert_bgnd.className = 'adv_alert_tlo';
		adv_alert_bgnd.style.width = szer+'px';
		adv_alert_bgnd.style.height = wys+'px';
		return adv_alert_bgnd;
}
//------------------------------------------------------------------------
function get_alert_frame(width, height, address) {
		if(width==undefined) var width = 300;
		if(height==undefined) var height = 100;
		var wys  = docHeight();
		var szer = docWidth();
		var marginleft = (szer - width) / 2;
		var margintop = (wys - height) / 2 - 20;
		var adv_alert_frame = document.createElement('iframe');
		adv_alert_frame.className = 'adv_alert';
		adv_alert_frame.style.width = width+'px';
		adv_alert_frame.style.height = height+'px';
		adv_alert_frame.scrolling = "no";
		adv_alert_frame.style.marginLeft = marginleft+'px';
		adv_alert_frame.style.marginTop = margintop+'px';
		adv_alert_frame.src = address;
		return adv_alert_frame;
}

function get_alert_div(width, height) {
		if(width==undefined) var width = 300;
		if(height==undefined) var height = 100;
		var wys  = docHeight();
		var szer = docWidth();
		var marginleft = (szer - width) / 2;
		var margintop = (wys - height) / 2 - 20;
		var adv_alert_frame = document.createElement('div');
		adv_alert_frame.className = 'adv_alert';
		adv_alert_frame.style.width = width+'px';
		adv_alert_frame.style.height = height+'px';
		adv_alert_frame.style.marginLeft = marginleft+'px';
		adv_alert_frame.style.marginTop = margintop+'px';
		return adv_alert_frame;
}

function adv_alert(id) {
	tlo = get_transparent_background();
	zawartosc = get_alert_frame(740, 480, 'justbought.php?id=' + id);
	document.body.appendChild(tlo);
	document.body.appendChild(zawartosc);
	this.window.jsbg = tlo;
	this.window.jsfr = zawartosc;
}

function DOM_element_remove(element, childs) {
	if(element) {
		while (element.hasChildNodes()) {
			DOM_element_remove(element.firstChild, true);
			element.removeChild(element.firstChild);
		}
		if(childs == undefined) element.parentNode.removeChild(element);
	}
}

function big_map(coords, zoom) {
	if(zoom == undefined) zoom = 15;
	var tlo = get_transparent_background();
	var zawartosc = get_alert_div(740, 480);
	zawartosc.innerHTML = '<div id="fabulousmap" style="position:absolute; width:740px; height:480px;"></div>';
	document.body.appendChild(tlo);
	document.body.appendChild(zawartosc);
	tlo.zawartosc = zawartosc;
	tlo.onclick = function() {DOM_element_remove(this.zawartosc); DOM_element_remove(this)}

	var map = new GMap2(document.getElementById("fabulousmap"));
	map.setCenter(new GLatLng(0,0),0);
	var bounds = new GLatLngBounds();
	for(var i=0; i<coords.length; i++) {
		var temp = coords[i].split(',');
		var crds = new GLatLng(temp[0],temp[1]);
		var m1 = new GMarker(crds);
		map.addOverlay(m1);
		bounds.extend(crds);
	}
	map.setUIToDefault();
	if(coords.length == 1)
		map.setZoom(zoom);
	else
		map.setZoom(map.getBoundsZoomLevel(bounds));
	map.setCenter(bounds.getCenter());
	
}

String.prototype.code = function() {
	return this.slice(this.indexOf('{') + 1, this.lastIndexOf('}'));
}

function adv_prompt(ostrzezenie, ok_url) {
		var wys  = docHeight();
		var szer = docWidth();
		var marginleft = (szer - 452) / 2;
		var margintop = (wys - 140) / 2 - 20;

		var adv_alert_bgnd = document.createElement('div');
		adv_alert_bgnd.className = 'adv_alert_tlo';
		adv_alert_bgnd.style.width = szer+'px';
		adv_alert_bgnd.style.height = wys+'px';

		var adv_alert_frame = document.createElement('div');
		adv_alert_frame.className = 'adv_alert';
		adv_alert_frame.style.width = '452px';
		adv_alert_frame.style.height = '140px';
		adv_alert_frame.style.marginLeft = marginleft+'px';
		adv_alert_frame.style.marginTop = margintop+'px';

		var tresc_alertu = document.createElement('p');
		tresc_alertu.setAttribute('id', 'tresc_alertu');
		tresc_alertu.style.margin = '10px';
		tresc_alertu.style.marginTop = '13px';
		tresc_alertu.style.marginBottom = '7px';
		tresc_alertu.style.overflow = 'hidden';
		tresc_alertu.style.height = '80px';
		tresc_alertu.innerHTML = ostrzezenie;

		var ok_butt = document.createElement('button');
		ok_butt.className = 'btn';
		ok_butt.adv_alert_bgnd = adv_alert_bgnd;
		ok_butt.adv_alert_frame = adv_alert_frame;
		ok_butt.appendChild(document.createTextNode('OK'));

		var no_butt = document.createElement('button');
		no_butt.className = 'btn';
		no_butt.style.marginLeft = '5px';
		no_butt.appendChild(document.createTextNode('Anuluj'));
		no_butt.onclick = function() {
			DOM_element_remove(adv_alert_bgnd);
			DOM_element_remove(adv_alert_frame);
		};

		adv_alert_frame.appendChild(tresc_alertu);
		adv_alert_frame.appendChild(ok_butt);
		adv_alert_frame.appendChild(no_butt);

		var temp_func = function() {DOM_element_remove(this.adv_alert_bgnd); DOM_element_remove(this.adv_alert_frame);};
		ok_butt.setAttribute('onclick', temp_func.toString().code() +' window.location.href=\'' + ok_url + '\';');


		document.body.appendChild(adv_alert_bgnd);
		document.body.appendChild(adv_alert_frame);
		no_butt.focus();
}

function nice_alert(content_id, show_close_button, bckgr) {
	if(gebID(content_id) && gebID(content_id) != undefined) {
		var wys  = docHeight();
		var szer = docWidth();
		var marginleft = (szer - 719) / 2;
		var margintop = (wys - 394) / 2 - 20;

		var adv_alert_bgnd = document.createElement('div');
		adv_alert_bgnd.className = 'adv_alert_tlo';
		adv_alert_bgnd.setAttribute('id', 'adv_alert_bgnd');
		adv_alert_bgnd.style.width = szer+'px';
		adv_alert_bgnd.style.height = wys+'px';

		var closebutton = document.createElement('a');
		closebutton.className = 'closebutton';
		closebutton.setAttribute('onclick', 'DOM_element_remove(this.parentNode); DOM_element_remove(gebID(\'adv_alert_bgnd\'));');
		
		var adv_alert_frame = document.createElement('div');
		adv_alert_frame.className = 'landLayer';
		adv_alert_frame.style.marginLeft = marginleft+'px';
		adv_alert_frame.style.marginTop = margintop+'px';

		if(bckgr != undefined)
			adv_alert_frame.style.backgroundImage = 'url(layout/IMG/'+bckgr+')';
		if(gebID(content_id).getAttribute('bckgr') && gebID(content_id).getAttribute('bckgr') != undefined && gebID(content_id).getAttribute('bckgr') != '')
			adv_alert_frame.style.backgroundImage = 'url(layout/IMG/'+ gebID(content_id).getAttribute('bckgr') +')';

		if(show_close_button)
			adv_alert_frame.appendChild(closebutton);

		adv_alert_frame.innerHTML += gebID(content_id).innerHTML;

		document.body.appendChild(adv_alert_bgnd);
		document.body.appendChild(adv_alert_frame);
	}
}


var red_bar_timeout = null;
function red_bar() {
	var bar = document.getElementById('chistmas_red_bar');
	if(bar.style.height == '')
		bar.style.height = '1px';
	var cur_height = parseInt(bar.style.height);
	cur_height++;
	bar.style.height = cur_height + 'px';
	if(cur_height < 34) {
		red_bar_timeout = setTimeout("red_bar()",(cur_height * 3));
	}
}
