// dz

var jq = jQuery.noConflict();

function uid(){
    var r, i, j;
    r = '';
    
    for (j = 0; j < 32; j++) {
        i = Math.floor(Math.random() * 16).toString(16).toLowerCase();
        r = r + i;
    }
    
    return r
}

;(function($) {
    $.fn.extend({
	defaultText : function() {
	    $(this).focus(function() {
		if( $(this).val() == $(this)[0].title ) {
		    $(this).removeClass("activeDefaultText");
		    $(this).val("");
		}
	    });
		
	    $(this).blur(function() {
		if($(this).val() == "") {
		    $(this).addClass("activeDefaultText");
		    $(this).val($(this)[0].title);
		}
	    });
	    
	    $(this).blur();
	}
    });
})(jQuery);


function log(value)
{
    if(typeof console.log == 'function') {
	console.log(value);
    }
}

var bodyOpt = {
    sT:0,
    wH:0,
    up:function() {
	this.sT = this.getScrollXY();
	},
    
    onLoad:function() {
	this.sT = this.getScrollXY();
	this.wH = this.getCSize();
    },
    
    menu: function() {
	this.onLoad();
	jq('#menu_cont').css({
	    "position": "absolute",
	    "top": bodyOpt.sT+"px"
	});
    },
    
    getScrollXY: function() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	}
	return scrOfY;
    },
    
    getCSize: function() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	}
	return myHeight;
    }
}


function sendEmail()
{	
	//TODO: funkcja do ukończenia, ma zastąpić nieszczęsnego onclicka :P
	
	jq.ajax({
		type: 'GET',
		url: "/tools/newsletter.php?email="+email,
		success: function(responseText,b){
			log(responseText);
			if(!responseText) {
			} else {
				switch(responseText){
					case 'WRONG_EMAIL': {
						jq("#newsletterbg").addClass("newsletter_adresnie");
						//alert('nieprawidłowy adres')
						break;
					}
					case 'EXISTS': {
						//alert('juz jest w bazie');
						jq("#newsletterbg").addClass("newletter_istnieje");
						break;
					}
					case 'THANKS': {
						//alert('zapisano'); 
						jq("#newsletterbg").addClass("newsletter_dzieki");
						jq("#newsletterbg").find("input").hide();
						jq("#newsletterbg").find("button").hide();
						break;
					}
					}
				}
			},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			
		},
		dataType: 'text'
	});
}


function newsModuleInit()
{
    var $ = jq;
    $.ajax({
	async: true,
	cache: false,
	dataType: "json",
	url: "/news.php?ajax=1&newsList=1",
	success: function(data, textStatus, XMLHttpRequest) {
	    data = eval(data);
	    $("#newsw #newslista .newslists-wrapper").html(data.newsList);
   
	    function onAfterNews(curr, next, opts) {
		var index = opts.currSlide;
		$('.prev')[index == 0 ? 'hide' : 'show']();
		$('.next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
	    }
	    
	    function onAfterNewsImg(curr, next, opts) {
		var index = opts.currSlide;
		$('.leftarrow')[index == 0 ? 'hide' : 'show']();
		$('.rightarrow')[index == opts.slideCount - 1 ? 'hide' : 'show']();
	    }
	    
	    $("#newsw #newslista .newslists-wrapper").cycle({
		fx: "scrollHorz",
		speed: "fast",
		pager: "#pager",
		//next: ".next",
		//prev: ".prev",
		after: onAfterNews,
		speed: 300,
		timeout: 0
	    });
	    
	    $("#newsimgbox .box").cycle({
		fx: "scrollHorz",
		speed: "fast",
		next: ".rightarrow",
		prev: ".leftarrow",
		after: onAfterNewsImg,
		speed: 300,
		timeout: 0
	    });
	    
	    function loadNewsContent(href) {
		if(!href) return;
		setTimeout(function(){
		$.ajax({
		    async: true,
		    cache: false,
		    dataType: "json",
		    timeout: 5000,
		    url: href+"&ajax=1&content=1",
		    success: function(data, textStatus, XMLHttpRequest) {
			data = eval(data);
			$("#newscontent h2").html(data.title);
			$("#newscontent .box").html(data.content);
			$("#newsw #newsimgbox .box").html(data.images);
			setTimeout(function(){
			    $("#newscontent .content-wrapper").jScrollPane({
				scrollbarWidth: 24,
				showArrows: true,
				arrowSize: 23
			    });
			}, 1000);
			
			return false;
		    },
		    error: function(XMLHttpRequest, textStatus, errorThrown) {
			log(textStatus+" + "+errorThrown);
			return true;
		    }
		});
		}, 100);
	    }
	    
	    $("#newsw #newslista ul li a").each(function(e){
		$(this).bind("click", function(){
		    loadNewsContent(this.href);
		    $("#newsw #newslista ul li.activeNews").removeClass("activeNews");
		    $(this).parent().addClass("activeNews");
		    return false;
		});
	    });
	    
	    loadNewsContent($("#newsw #newslista ul:first-child li:first-child a").attr("href"));
	    $("#newsw #newslista ul:first-child li:first-child").addClass("activeNews");
	    $("#newscontent .content-wrapper").jScrollPane({
			scrollbarWidth: 24,
			showArrows: true,
			arrowSize: 23
	    });
	    return false;

	},
	error: function(XMLHttpRequest, textStatus, errorThrown) {
	    return true;
	}
    });
}


function loadModule(id) {
    var $ = jq;
    
    switch(id) {
	case 'fotki': {
	    $("#fotkiw a.blank").attr("target", "_blank");
	    
	    $("#newsletterform input").defaultText();
	    if($("#fotki #loadElDiv").length > 0) {
		$("#fotki #loadElDiv").remove();
	    }
	    
	    $("#fotkiw .jcarousel-skin-cropp").jcarousel({
				scroll: 4,
				buttonNextHTML: '<div id="rightarrow"><img src="/images/lc/fotki_right.jpg" alt="" /></div>',
				buttonPrevHTML: '<div id="leftarrow"><img src="/images/lc/fotki_left.jpg" alt="" /></div>'
	    });
	    
	    function setPhoto(href) {
		var img = new Image();
		
		var container = $("#fotkiw .img-view");
		var oldImg = container.find("img");
		$(img).hide();
		img.onload = function() {
		    
		    img.onload = null;
		    var posX = (container.width() - img.width )/2;
		    var posY = (container.height() - img.height )/2;
		    $(img).css({
			"position": "absolute",
			"top" : posY+"px",
			"left" : posX+"px"
		    });
		    
		    $(img).appendTo(container).fadeIn("fast", function(){
			oldImg.fadeOut("fast");
			oldImg.remove();
		    });
		    
		    log(img.width);
		}
		
		img.src = href;

	    }
	    
	    $("#fotkiw #gallist li").each(function(){
		$(this).click(function(){return false;}).bind("click", function(){
		    setPhoto($(this).find("a").attr("href"));
		    return false;
		});
	    });

	    $("a#fshare").attr("target", "_top");
	    
	    return false;
	}
	
	case 'news': {
	    newsModuleInit();
	    $("#newsw a.blank").attr("target", "_blank");
	    return false;
	}
	
	case 'esklep': {
	   /* 
	    for(i=1; i >= 3; i++) {
		var r = (Math.random()*(rssItems.length-1));
		var p = rssItems[r.toFixed()];
		rssItems.splice(r.toFixed(), 1);
		
		$("#esklepw .product"+i+" a").attr("href", p[2]);
		$("#esklepw .product"+i+" a").attr("title", p[1]);
		$("#esklepw .product"+i+" a img").remove();
		$("<img>").attr("src", p[6]).appendTo("#esklepw .product"+i+" a");
		$("#esklepw .goToShop-"+i+"\"").attr("href", p[2]);
		
	    }*/
	    $("#esklepw a.blank").attr("target", "_blank");
	    
	    var r1 = (Math.random()*(rssItems.length-1));
	    var p1 = rssItems[r1.toFixed()];
	    rssItems.splice(r1.toFixed(), 1);
	    
	    var r2 = (Math.random()*(rssItems.length-1));
	    var p2 = rssItems[r2.toFixed()];
	    rssItems.splice(r2.toFixed(), 1);
	    
	    var r3 = (Math.random()*(rssItems.length-1));
	    var p3 = rssItems[r3.toFixed()];
	    rssItems.splice(r3.toFixed(), 1);
	    
	    $("#esklepw .product1 a").attr("href", p1[2]);
	    $("#esklepw .product1 a").attr("title", p1[1]);
	    $("#esklepw .product1 a img").remove();
	    $("<img>").attr("src", p1[6]).appendTo("#esklepw .product1 a");
	    $("#esklepw .goToShop-1").attr("href", p1[2]);
	
	    $("#esklepw .product2 a").attr("href", p2[2]);
	    $("#esklepw .product2 a").attr("title", p2[1]);
	    $("#esklepw .product2 a img").remove();
	    $("<img>").attr("src", p2[6]).appendTo("#esklepw .product2 a")
	    $("#esklepw .goToShop-2").attr("href", p2[2]);
	    
	    $("#esklepw .product3 a").attr("href", p3[2]);
	    $("#esklepw .product3 a").attr("title", p3[1]);
	    $("#esklepw .product3 a img").remove();
	    $("<img>").attr("src", p3[6]).appendTo("#esklepw .product3 a");
	    $("#esklepw .goToShop-3").attr("href", p3[2]);
	    
	    
	    
	    break;   
	}
	
	case 'index': {
	    $("#indexw a.blank").attr("target", "_blank");
	    
	    var _id = uid();
	    
	    $("#player_swf").flashembed({
		src: "/swf/engine_cropp_player.swf?v=11",
		width: 523,
		height: 360,
		allowfullscreen: false,
		allowscriptaccess: false,
		wmode: "transparent",
		quality: "high",
		w3c: true,
		required: false,
		version: [10,0,0],
		menu: "false"
	    }, { id: _id });
			
		// PL FLASH
			
	    $("#menuleft_swf").flashembed({
		src: "/swf/engine_cropp_header_leftside.swf",
		width: 201,
		height: 442,
		allowfullscreen: false,
		allowscriptaccess: false,
		wmode: "transparent",
		quality: "high",
		w3c: true,
		required: true,
		menu: "false"
	    }, { id: _id });
	    
	    $("#menuright_swf").flashembed({
		src: "/swf/engine_cropp_header_rightside.swf",
		width: 183,
		height: 375,
		allowfullscreen: false,
		allowscriptaccess: false,
		wmode: "transparent",
		quality: "high",
		w3c: true,
		required: true,
		menu: "false"
	    }, { id: _id , movie1: '/swf/assets/movies/piaty.flv||/swf/assets/images/zaslepka_video_5.jpg' , movie2: '/swf/assets/movies/drugi.flv||/swf/assets/images/zaslepka_video_2.jpg' , movie3: '/swf/assets/movies/trzeci.flv||/swf/assets/images/zaslepka_video_3.jpg' , movie4: '/swf/assets/movies/czwarty.flv||/swf/assets/images/zaslepka_video_4.jpg' , first: 'movie1' });
	
		// EN FLASH
			
	    $("#menuleft_swf_en").flashembed({
		src: "/swf/engine_cropp_header_leftside_EN.swf",
		width: 201,
		height: 442,
		allowfullscreen: false,
		allowscriptaccess: false,
		wmode: "transparent",
		quality: "high",
		w3c: true,
		required: true,
		menu: "false"
	    }, { id: _id });
	    
	    $("#menuright_swf_en").flashembed({
		src: "/swf/engine_cropp_header_rightside_EN.swf",
		width: 183,
		height: 375,
		allowfullscreen: false,
		allowscriptaccess: false,
		wmode: "transparent",
		quality: "high",
		w3c: true,
		required: true,
		menu: "false"
	    }, { id: _id , movie1: '/swf/assets/movies/piaty.flv||/swf/assets/images/zaslepka_video_5.jpg' , movie2: '/swf/assets/movies/drugi.flv||/swf/assets/images/zaslepka_video_2.jpg' , movie3: '/swf/assets/movies/trzeci.flv||/swf/assets/images/zaslepka_video_3.jpg' , movie4: '/swf/assets/movies/czwarty.flv||/swf/assets/images/zaslepka_video_4.jpg' , first: 'movie1' });
	    
	    $("#header_swf").flashembed({
		src: "/swf/engine_cropp_header_top.swf",
		width: 535,
		height: 284,
		allowfullscreen: false,
		allowscriptaccess: false,
		wmode: "transparent",
		quality: "high",
		w3c: true,
		required: true,
		menu: "false"
	    }, { id: _id });
	    break;
	}
	    
	case 'croptv': {
	    $("#croptvw a.blank").attr("target", "_blank");
	    break;
	}
	
	case 'blog': {
	    $("#blogw a.blank").attr("target", "_blank");
	    
	    break;
	}
	
	case 'sklepy': {
	    $("#sklepyw a.blank").attr("target", "_blank");

	    function fillUl(ul, items, callback) {
		ul.html("");
		for(i in items) {
		    var li = jq("<li></li>");
		    var a = jq("<a></a>").attr("href", "#").attr("rel", i).html(i);
		    a.appendTo(li);
		    li.appendTo(ul);
		    if(typeof callback == 'function') {
			callback(a);
		    }
		}
	    }
	    
	    function addScrollbar(el) {
		var item = $(el);
		item.jScrollPane({
		    scrollbarWidth: 24,
		    showArrows: true,
		    arrowSize: 23
		});
	    }
	    
	    fillUl($("#panstwo .list-wrapper ul.items"), locations);
	    addScrollbar("#panstwo .list-wrapper");
	    
	    $("#panstwo .list-wrapper ul.items li a").each(function(){
		$(this).bind("click", function() {
		    var lvl1 = this;
		    $("#panstwo .list-wrapper ul.items li a").removeClass("current");
		    $(this).addClass("current"); 
		    $("#miasto").addClass("sklepy_m");
		    $("#miasto .list-wrapper ul.items").html("");
		    $("#ulica .list-wrapper ul.items").html("");
		    $("#in_mapka").html("");
		    $("#sklepyw #ulica").removeClass("sklepy_u");
		    $("#sklepyw #mapka").css("background", "url(/images/lc/sklepy_mapka_logo.jpg)");
		    fillUl($("#miasto .list-wrapper ul.items"), locations[this.rel], function(item) {
			item.bind("click", function() {
			    $("#in_mapka").html("");
			    $("#sklepyw #mapka").css("background", "url(/images/lc/sklepy_mapka_logo.jpg)");
			    $("#ulica").addClass("sklepy_u");
			    $("#ulica .list-wrapper ul.items").html("");
			    $("#miasto .list-wrapper ul.items li a.current").removeClass("current");
			    $(this).addClass("current");
			    var lvl2 = this;

			    for(i in locations[lvl1.rel][this.rel]) {
				var li = jq("<li></li>");
			
				var a = jq("<a></a>").attr("href", "#")
					.attr("id", i)
					.attr("posx", locations[lvl1.rel][this.rel][i].posx)
					.attr("posy", locations[lvl1.rel][this.rel][i].posy)
					.attr("rel", locations[lvl1.rel][this.rel][i].address).html(locations[lvl1.rel][this.rel][i].address)
					.attr("name", i);
				a.appendTo(li);
				li.appendTo("#ulica .list-wrapper ul.items");
				
				a.bind("click", function(){
				    $("#mapka").css({'background': 'url("/images/lc/sklepy_mapka.jpg")'});
				    
					/*
					var iframe = null;
				    if($("#sklepyw #mapka").find("iframe").length > 0) {
					iframe = $("#sklepyw #mapka").find("iframe");
				    } else {
					iframe = $("<iframe>").attr("width", "226px").attr("height", "202px");
					iframe.appendTo("#sklepyw #mapka");
				    }
				    iframe.fadeOut("fast");
				    iframe.attr("src", locations[lvl1.rel][lvl2.rel][this.name].map);
				    iframe.fadeIn("fast");
				    */
	
					startGoogleMap($('#' + this.id).attr('posx'), $('#' + this.id).attr('posy'));
					$("#ulica .list-wrapper ul.items li a.current").removeClass("current");
				    $(this).addClass("current");
		
				    return false;
				});
			
				//return false;
			    }
			    addScrollbar("#ulica .list-wrapper");
			    return false;
			});
		    });
		    addScrollbar("#miasto .list-wrapper");
		    return false;
		});
	    });
	    
	    break;   
	}
    }
}

function scrollPage(href)
{
    jq(window).scrollTo(href, 700);
}

function initMenu()
{
    jq("#top_menu a").each(function(){
	jq(this).bind("click", function() {
	    scrollPage(jq(this).attr("href"));
	    return false;
	});
    });
}

function pageload()
{
    if(hash) {
       //jq("#load").load(hash + ".html");
    } else {
      //jq("#load").empty();
    }  
}

jq(document).ready(function($){
    
    if($.browser.msie) {
    	if(parseInt($.browser.version) < 8) { 
    		$("#menu_cont").css({"position": "absolute", "right": "40px"}).width(527);
    			bodyOpt.menu();
    			$(window).bind("resize", function(){
    				bodyOpt.menu();
    			});
    
    			$(window).bind("scroll", function(){
    				bodyOpt.menu();
    			});
    	}
    	$('#top_menuFB li > iframe').attr('allowTransparency', true);
    }
    
    if($("#indexw").length > 0) {
	loadModule("index");
    }
    
    if($("#fotkiw").length > 0) {
	loadModule("fotki");
    }
    
    if($("#newsw").length > 0) {
	loadModule("news");
    }
    
    if($("#esklepw").length > 0) {
	loadModule("esklep");
    }
    
    if($("#croptvw").length > 0) {
	loadModule("croptv");
    }
    
    if($("#sklepyw").length > 0) {
	loadModule("sklepy");
    }
    
    initMenu();
    
    //
    
    //document.body.parentNode.parentNode.firstChild.nodeValue = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
    
});



