$(document).ready(function(){
	
	for (i=0; i<document.links.length;) {
		document.links[i].onfocus=document.links[i++].blur;
	}
	
	$(function() {
        $('.photoframe_small a').lightBox(); //galeria zdjec glownych -> aktualnosci, podstrony, artykuly
    });
	
	$(function() {
        $('.photoframe a').lightBox(); //galeria zdjec glownych -> aktualnosci, podstrony, artykuly
    });
	
	$.post($("base").attr("href")+'welcome/komunikat.html', {
	}, function(data){
		if(data){
			komunikat( data.header, data.txt , data.url, data.another_type, data.anuluj );
		}
	}, "json");	
	
    $('#slide').cycle({
		fx: 'fade', 
	    speed:  'slow',
	    timeout: 4000,
		width: 150, 
    	height: 100, 
	    random: 1
	});
    
    $(function() {
    	$('#galeria a').lightBox({
    		txtImage: 'Zdjęcie',
    		txtOf: 'z',
    		keyToClose: 'Esc',
    		fixedNavigation: true
    	});
    });
    
    $(document).keypress(function (e) {
    	if( e.keyCode == 13 && $("#kom_ok").length>0 ) {
    		var url = $("#kom_ok").attr('href');
    		window.location.href = url;
    	}
    });
	
});

function kategoria(cat){

	var sub_set = $('#sub_no'+cat).size(); //sub_no# is set
	
	if( sub_set > 0 ){	
		
		if( $('#sub_no'+cat+':visible').size()==0 ){
			$.post($("base").attr("href")+'kategoria/statistic.html', {
				id: cat
			});
			//$('.sub').slideUp('fast');
			$('#sub_no'+cat).slideDown('fast');
			$('#sub_no'+cat).parent().addClass("rozsuniete");
		}
		else{
			$('#sub_no'+cat).slideUp('fast');
			$('#sub_no'+cat).parent().removeClass("rozsuniete");
		}
   	}
   	else{
		window.location.href = $("base").attr("href")+'kategoria/index/'+cat+'.html';
   	}
}

function welcome_kat(cat){
	$.post($("base").attr("href")+'welcome/welcome_kat.html', {
		id: cat
	}, function(data){
		if(data) $('#tresc').html(data);
		else window.location.href = $("base").attr("href")+'kategoria/index/'+cat+'.html';
	} );	
}

function komunikat(comm_header, comm_txt, ok_url, another_type, anuluj) {

	//wielkosc okna
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	//polozenie na stronie
	var xPos, yPos;
	if (self.pageYOffset) {
		yPos = self.pageYOffset;
		xPos = self.pageXOffset;
	} else if (document.documentElemsent && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yPos = document.documentElement.scrollTop;
		xPos = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yPos = document.body.scrollTop;
		xPos = document.body.scrollLeft;	
	}
	

	var future_pos_top = parseInt(yPos+windowHeight/2)-200;
	future_pos_top = future_pos_top>50 ? future_pos_top : 50;
	
	var html = '<div id="info_preloader" style="display: none; z-index: 10; opacity: 0.5; filter: alpha(opacity=50); background-color: #000; position: absolute; left: 0px; top: 0px; width: '+pageWidth+'px; height: '+pageHeight+'px;"></div>';
	html += 	'<div id="info_content" style="left: '+(parseInt(xPos+windowWidth/2)-195)+'px; top: '+future_pos_top+'px;">';
	html += 		'<div id="comm_top">&nbsp;</div>';
	html +=			'<div id="comm_content">';
	html +=				'<div id="comm_header">'+comm_header+'</div>';
	html +=				'<div class="comm_txt">'+comm_txt+'</div>';
	if( anuluj ){
		html +=			'<a class="button_ok button" id="kom_ok" style="margin-left: 100px" href="'+ok_url+'">&nbsp;</a>';
		html +=			'<a class="button_anuluj button" style="margin-left: 90px"  href="javascript:hide_info()">&nbsp;</a>';
	}
	else{
		html +=			'<a class="button_ok button" id="kom_ok" href="'+ok_url+'">&nbsp;</a>';
	}
	html += 			'<div class="clear" style="height: 0;">&nbsp;</div>';
				
	
	if( another_type ){
				html += '<div class="comm_line">&nbsp;</div>';
		switch(another_type){
			/*
			case 'haslo':
				html += '<div class="comm_txt">Jeśli zapomniałeś hasła kliknij poniżej.</div>';
				html += '<a id="button_haslo button" href="javascript:haslo();">&nbsp;</a>';
				break;
				*/
			case 'rejestracja':
				html += '<div class="comm_txt">Jeśli nie posiadasz jeszcze konta <a href="klient/rejestracja.html" style="font-weight: bold;">zarejestruj się</a>.</div>';
				break;
		}
	}
				
	html +=			'</div>';
	html += 		'<div id="comm_bottom">&nbsp;</div>';
	html += 		'<div class="clear" style="height: 0;>&nbsp;</div>';
	html += 	'</div>';

	$("body").append(html);


	jQuery("#info_preloader").fadeIn(200, function(){
		jQuery("#info_content").fadeIn(200);
	});

}

function hide_info(){

	jQuery(document).ready(function(){
		jQuery("#info_content").fadeOut(200,function(){
			jQuery("#info_preloader").fadeOut(200,function(){
				jQuery("#info_preloader").empty();
				jQuery("#info_preloader").remove();
				jQuery("#info_content").empty();
				jQuery("#info_content").remove();
			});
		});
	});
}

function enter_search(wsk, e){
	if(e.keyCode == 13) {
		if( $(wsk).val() ) $(wsk).parent().submit();
	}
}

function szukaj_strona(page, szukaj, typ){
	$.post( $("base").attr("href")+'szukaj/lista.html', {
		szukaj: szukaj,
		typ: typ
	}, function(data){
		$('#content').html(data);
	} );	
}

function haslo(){
	var txt = 'Wypełnij formularz podając adres email <br />zapisany w Twoim koncie w sklepie.<br />Na podany adres email zostanie przesłane nowe, wygenerowane hasło.<br />'
	txt += '<form action="klient/haslo.html" method="post" id="pass_form" enctype="multipart/form-data">';
	txt += '<table class="comm_form"><tr><td>e-mail:</td><td>';	
	txt += '<input type="text" id="email" name="email" value="" class="yellow" style="width: 200px" /></td></tr></table></form>';

	setTimeout(function() {
		komunikat( 'Przypominanie hasła do konta:', txt, "javascript:$('#pass_form').submit();", '', true );
	}, 500);
}

function dodaj_do_koszyka(id){
	
	var comm_header = 'Dodaj do koszyka';
	var comm_txt = '<table><tr><td style="width: 100px;">ilość:</td><td><input type="text" name="towar_ilosc" class="px150" value="1"/></td></tr></table>';
	ok_url = 'javascript:koszyk_dodaj_submit('+id+');';
	
	komunikat(comm_header, comm_txt, ok_url, false, true);
}

function koszyk_dodaj_submit(id){
	
	ilosc = $('input[name="towar_ilosc"]').val();
	
	$.post($("base").attr("href")+'klient/koszyk_dodaj/'+id+'.html', {
		ilosc: ilosc
	}, function(data){
		window.location.reload();
	} );
}

function zadzwon_info(){
	
	$.post($("base").attr("href")+'welcome/zadzwon_info.html', {
	}, function(data){
		var comm_header = 'Zadzwoń';
		ok_url = 'javascript:hide_info();';
		komunikat(comm_header, data, ok_url, false, false);
	} );
}

function zmien_w_koszyku(id, ilosc){
	
	var comm_header = 'Zmień ilość towarów w koszyku';
	var comm_txt = '<table><tr><td style="width: 100px;">ilość:</td><td><input type="text" name="towar_ilosc" class="px150" value="'+ilosc+'"/></td></tr></table>';
	ok_url = 'javascript:koszyk_zmien_submit('+id+');';
	
	komunikat(comm_header, comm_txt, ok_url, false, false);
}

function koszyk_zmien_submit(id){
	
	ilosc = $('input[name="towar_ilosc"]').val();
	
	$.post($("base").attr("href")+'klient/koszyk_ilosc/'+id+'.html', {
		ilosc: ilosc
	}, function(data){
		window.location.reload();
	} );
}

function logowanie_info(){
	
	var comm_header = 'Wymagane logowanie';
	var comm_txt = 'Aby zrealizować zamówienie musisz się zalogować.';
	ok_url = 'javascript:logowanie_info_ok()';
	komunikat(comm_header, comm_txt, ok_url, false, false);
}

function logowanie_info_ok(){
	javascript:scroll(0,0); 
	javascript:hide_info();
}

function loader() {
	var pageWidth = $(document).width();
	var pageHeight = $(document).height();
	var windowHeight = $(window).height();
	
	var posTop = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		posTop = window.pageYOffset;
	} else if( document.body && document.body.scrollTop ) {
		//DOM compliant
		posTop = document.body.scrollTop;
	} else if( document.documentElement && document.documentElement.scrollTop ) {
		//IE6 standards compliant mode
		posTop = document.documentElement.scrollTop;
	}
	posTop = posTop + windowHeight/2 - 100;
	
	/*
	var str = 	'<div id="loader_info" style="display: none; z-index: 3; opacity: 0.7; filter: alpha(opacity=70); background-color: #fff; position: absolute; top: 0; left: 0; width: '+pageWidth+'px; height: '+pageHeight+'px;">';
	str += 			'<div id="loader_info_content" style="margin: auto; margin-top: '+posTop+'px;text-align: center; width: 200px; height: 200px;">';
	str += 				'<img src="media/dom/gfx/loader.gif" alt="loader" />';
	str += 				'<div style="font-size: 14px; color: #000; margin-top: 7px;" id="info_time"></div></div></div>';
	 $("body").append(str);
	 */

	$('#loader_info').css('width', pageWidth+'px');
	$('#loader_info').css('height', pageHeight+'px');
	$('#loader_info_content').css('margin-top', posTop+'px');
	
	$("#loader_info").fadeIn(200);
	
	var handle = setInterval(function() {
		if( $("#loader_info").size()!=0 ){
			var now = new Date();
			var year = 1900 + now.getYear();
			var month = 1+now.getMonth();
			month = month<10 ? '0'+month : month;
			var day = now.getDate();
			day = day<10 ? '0'+day : day;
			var hours = now.getHours();
			hours = hours<10 ? '0'+hours : hours;
			var minutes = now.getMinutes();
			minutes = minutes<10 ? '0'+minutes : minutes;
			var seconds = now.getSeconds();
			seconds = seconds<10 ? '0'+seconds : seconds;
			$("#info_time").html(year+'-'+month+'-'+day+' '+hours+':'+minutes+':'+seconds);
		}
		else{
			clearInterval(handle);
		}
	}, 1000);
	
		
}

function setFilter(wsk){
	
	var value = $(wsk).is('[type="checkbox"]') 
				? ( $(wsk).is(':checked') ? 1 : 0)
				: ( $(wsk).is('select') ? $(wsk).val() : null ); 
	
	$.post( $("base").attr("href")+'welcome/setFilter.html', {
		filter: $(wsk).attr('name'),
		value: value
	}, function(data){
		window.location.href = window.location.href;
	} );
}
