/* $Id: script.js,v 1.30 2010-10-28 11:16:00 rbernaczek Exp $ */

var UNIQUE_CHARS = "abcdefghijklmnoprstuwqxyzABCDEFGHIJKLMNOPRSTUWQXYZ1234567890";
var FLASH_PAGE_ID = prepareUniqueString(20);
var FLASH_PAGE_ID_STRING = "uid=" + FLASH_PAGE_ID;

function prepareUniqueString(length){
	var sb = "";
	for (i=0; i<length; i++)
		sb += UNIQUE_CHARS.charAt(Math.floor(Math.random() * UNIQUE_CHARS.length));
	return sb;
}

/* ----------------------------------------------------------------------
	SET FLASH CONATAINER HEIGHT
---------------------------------------------------------------------- */
var MINIMUM_FLASH_HEIGHT = 650;

function setFlashContainerHeight(){
	var wh = $(window).height();
	var fh = $("#footer").height();

	if (wh > MINIMUM_FLASH_HEIGHT + fh){
		$("#flash_container").height(wh - fh);
	}
}


/* ----------------------------------------------------------------------
	FLASH LAYERS
---------------------------------------------------------------------- */
function openInfoPanel(path, width, height){
	closeInfoPanel();

	var wh = $(window).height();
	var ww = $(window).width();

	createMask();

	// creating gallery frame
	$("body").append("<div id='infoPanel'><div id='infoPanelFla'>...</div></div>");
	var info_panel = $("#infoPanel");
	info_panel.height(height);
	info_panel.width(width);
	info_panel.css("top", ($(window).scrollTop() + (wh - height) / 2));
	info_panel.css("left", (ww - width) / 2);

	var so = new SWFObject(path, "infoPanelFla", width, height, "9");
	so.addParam("AllowScriptAccess", "sameDomain");
	so.addParam("wmode", "transparent");
	so.write("infoPanelFla");
}

function closeInfoPanel(){
	$("#infoPanel").remove();
	closeMask();
}

function openCreator(){
	openInfoPanel("/swf/designer.swf", 735, 520);
}

function openUpdateProfil(){
	openInfoPanel("/swf/designer.swf?mod=1", 735, 520);
}

function openPassReminder(){
	openInfoPanel("/swf/password_reminder.swf", 290, 193);
}

function closeCreator(){
	closeInfoPanel();
}

function openHTML(path){
	//alert(path);
	window.open(path, 'okienko', 'toolbar=no, location=no, status=no, scrollbars=yes, resizable=yes, width=500, height=400, top=200,left=300');
	return false;
}

/* ----------------------------------------------------------------------
	PALETA EMOTIKON
---------------------------------------------------------------------- */
function emoticonPaletteInit(){
	$('textarea[name=content]')
		.css('width','595px')
		.before($('#emoticons_tmp_container').html());
	$('ul.emoticons img').click(function(e) {
		$('textarea[name=content]').replaceSelection($(this).attr('alt'), true);
	});
}


/* ----------------------------------------------------------------------
	WYSOKOSC CONTENTU
---------------------------------------------------------------------- */
var MINIMUM_CONTENT_HEIGHT = 400;
var GRASS_HEIGHT = 300;
var CONTENT_MARGIN_TOP = 180;
var RIGHT_WAVE_HEIGHT = 300;
var RIGHT_WAVE_MARGIN = 150;

var LEFT_WAVE_HEIGHT = 370;
var LEFT_WAVE_TOP_MARGIN = 200;

function setContentHeight(){
	var c = $("#content");
	var h = c.height();

	if (h < MINIMUM_CONTENT_HEIGHT) {

		var cf = $("#content_footer");
		if (cf[0] == undefined) {
			h = MINIMUM_CONTENT_HEIGHT;
			c.height(h);
		} else {
			if (cf.parent()[0].id == "container"){
				h = MINIMUM_CONTENT_HEIGHT;
				c.height(h);
			} else {
				cf.css("margin-top", MINIMUM_CONTENT_HEIGHT - h);
			}
		}
	}

	// Prawa strona
	var t = 0;
	var q = 0;
	while (t + RIGHT_WAVE_HEIGHT + RIGHT_WAVE_MARGIN <= h){
		addRightWave(t, q);
		q ++;
		t += RIGHT_WAVE_HEIGHT + RIGHT_WAVE_MARGIN;
	}

	// Lewa strona
	t = 0;
	q = 0;
	while (t + LEFT_WAVE_HEIGHT + LEFT_WAVE_TOP_MARGIN <= h){
		addLeftWave(t + LEFT_WAVE_TOP_MARGIN, q);
		q ++;
		t += LEFT_WAVE_HEIGHT + LEFT_WAVE_TOP_MARGIN;
	}
}


function addRightWave(top, no){
	if (no % 2 == 1)
		addWave("waveR_2", top);
	else
		addWave("waveR_1", top);
}

function addLeftWave(top, no){
	if (no % 2 == 1)
		addWave("waveL_2", top);
	else
		addWave("waveL_1", top);
}

function addWave(className, top){
	top += CONTENT_MARGIN_TOP;
	$("#container").append("<div class='" + className + "' style='top: " + top + "px'>&#160;</div>");
}


/* ----------------------------------------------------------------------
	IE
---------------------------------------------------------------------- */
// Ukrycie pustych <fieldset/>
$(document).ready(function(e){
	$("fieldset:empty").hide();
});


/* ----------------------------------------------------------------------
	GALLERY PREVIEW
---------------------------------------------------------------------- */
$(document).ready(setGalleryActive);

function setGalleryActive(){
//	$("a.gal_image").click( function(e) {
//		createMask();
//		showGalleryPreview(e.target);
//		return false;
//	});

	$("a.gal_image").lightbox({
		fileLoadingImage : '/images/lightbox/loading.gif',
		fileBottomNavCloseImage : '/images/lightbox/close.gif',
		overlayOpacity : 0.4,
		displayTitle: false,
		disableNavbarLinks: true,
		inprogress : true,
		loopImages: true,
		strings : {
			prevLinkTitle: 'Poprzedni obraz',
			nextLinkTitle: 'Następny obraz',
			closeTitle: 'Zamknij',
			image: 'Obraz ',
			of: ' z '
		},
		fitToScreen: true
	});
}

function showGalleryPreview(target){
	var h5 =  $(target.parentNode).next();
	var a = $(target.parentNode);
	var wh = $(window).height();
	var ww = $(window).width();
	var nextTarget = $(target.parentNode.parentNode).next();
	var prevTarget = $(target.parentNode.parentNode).prev();

	//debug2(nextTarget);
	//return false;
	//alert(a[0].pathname);

	// creating gallery frame
	$("body").append("<div id='gall_preview_frame'></div>");
	var gall_preview_frame = $("#gall_preview_frame");
	var fh = gall_preview_frame.height();
	var fw = gall_preview_frame.width();
	gall_preview_frame.css("top", ($(window).scrollTop() + (wh - fh) / 2));
	gall_preview_frame.css("left", (ww - fw) / 2);

	// creating preview space
	gall_preview_frame.append("<div id='gall_preview'></div>");
	var gall_preview = $("#gall_preview");
	gall_preview.height(fh - 28);
	var ph = gall_preview.height();
	var pw = gall_preview.width();

	// appending close button element
	gall_preview_frame.append("<div id='close_preview' class='db_button'><a href='#1' title='Zamknij'>Zamknij</a></div>");

	// appending flash preloader
	swfSize = 100;
	gall_preview.append("<div id='gall_preloader'><div id='gall_preloader_fla'>\u0141adowanie zdj\u0119cia...</div></div>");
	var gall_preloader = $("#gall_preloader");
	gall_preloader.css("top", ((ph - swfSize) / 2));
	gall_preloader.css("left", ((pw - swfSize) / 2));

	//var parObj = {};
	//parObj.wmode = "transparent";
	//parObj.allowScriptAccess = "sameDomain";
	//swfobject.embedSWF("/swf/battery_loader.swf", "gall_preloader_fla", swfSize, swfSize, "9.0.0", "/swf/expressInstall.swf", null, parObj);

	// appending image
	gall_preview.append("<div id='gall_img'><img src='/converter?heightMax=" + (ph - 100) + "&widthMax="+ (pw - 20) +"&amp;amp;center-crop=1&amp;amp;img=" + a[0].pathname + "' alt='" + h5.html() + "'/></div>");
	var gall_img = $("#gall_img");
	$("#gall_img img").load( function(e) {
		var t = $(e.target);
		gall_img.css("top", ((ph - t.height()) / 2 - 20));
		gall_img.css("left", ((pw - t.width()) / 2));
		gall_img.hide();
		gall_img.fadeIn("fast");
	});

	// append image description
	gall_preview.append("<h5>" + h5.html() + "</h5>");

	$("#close_preview").click( function(e) {
		$("#gall_preview_frame").remove();
		closeMask();
	});

	// append navigator
	if (prevTarget.length > 0){
		gall_preview.append("<div class='bd_prev db_button'><a href='#1' id='gallPrevBut'>Poprzednie zdj�cie</a></div>");
		$("#gallPrevBut").click( function(e) {
			changeImage(prevTarget);
			return false;
		});
	}
	if (nextTarget.length > 0){
		gall_preview.append("<div class='bd_next db_button'><a href='#1' id='gallNextBut'>Nazt�pne zdj�cie</a></div>");
		$("#gallNextBut").click( function(e) {
			changeImage(nextTarget);
		});
	}
}

function changeImage(target){
	if (target.length < 1)
		return;
	$("#gall_preview_frame").remove();

	showGalleryPreview($(target).find("img")[0]);
}

function createMask(){
	// creating mask layer
	$("body").append("<div id='mask'></div>");
	var mask = $("#mask");
	mask.height($(document).height());
}

function closeMask(){
	$("#mask").fadeOut("fast", function(e){
		$(this).remove();
	});
}


/* ----------------------------------------------------------------------
	COOKIE
---------------------------------------------------------------------- */
function getCookie(name)
{
	var arg	= name + '=';
	var alen = arg.length;
	var	clen =document.cookie.length;
	var i = 0;
	while (i<clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i,j)==arg) return getCookieVal(j);
		i = document.cookie.indexOf(" ",i) + 1;
		if (i==0) break;
	};
	return null;
}


function setCookieValue(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr==-1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset,endstr));
}

function delCookie(name)
{
	if (getCookie(name))
		document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function debug2(object)
{
	var t = "";
		for(var i in object)
		{
				t+="<strong>" +i+ "</strong> -> "+object[i]+"<br>";
		}
		var d = document.createElement("div");
		var newAttr = document.createAttribute("class");
		newAttr.nodeValue = "debug2";
		d.setAttributeNode(newAttr);
		d.innerHTML = t;
		document.body.getElementsByTagName("div")[0].appendChild(d);
}

function checkSearch(form){
	//form = document.formsearch;
	if (trim(form["search-clause"].value).length < 3)
	{
		alert('Prosz\u0119 wpisa\u0107 przynajmniej 3 znaki');
		return false;
	}
	else
		return true;
}

function trim(lancuch){
	while (lancuch.charAt(0) == " ")
		lancuch = lancuch.substring(1,lancuch.length);
	while (lancuch.charAt(lancuch.length-1) == " ")
		lancuch = lancuch.substring(0,lancuch.length-2);
	return lancuch;
}

function addEvent(obj, evType, fn)
{
	if(obj.addEventListener)
	{
		obj.addEventListener(evType, fn, false);
		return true;
	}
	if (obj.attachEvent)
	{
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	}
	return false;
}

