function message(data, location, button) {
	if (typeof(data) == "object") {
		msg = data.msg;
		location = data.location;
		button = data.button;
	} else msg = data;

	var div = $("<div>"+msg+"</div>").dialog({
		show: "blind",
		hide: "explode",
		modal: true
	});

	if (button) {
		div.dialog({
			buttons: {
				Ok: function() {
					if (location)
						window.location = location;
					else
						div.dialog( "close" );
				}
			},
			minWidth: 400
		});
	} else {
		setTimeout(function(){
			if (location)
				window.location = location;
			else
				div.dialog( "close" );
		}, 2000);
	}
}

function myConfirm(data, location) {
	if (typeof(data) == "object") {
		msg = data.msg;
		location = data.location;
	} else
		msg = data;

	var div = $("<div>"+msg+"</div>").dialog({
		show: "blind",
		hide: "explode",
		modal: true,
		buttons: {
			"Tak" : function() {
				window.location = location;
			},
			"Nie" : function() {
				$( this ).dialog( "close" );
			}
		},
		minWidth: 400
	});

	return false;
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ) {
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ) {
      if ( aQueryString[iParam].indexOf(strParamName + "=") > -1 ) {
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}

function setCookie(name, value, expires) {
	  var cookieStr = escape(name) +"=";
	  if (typeof value != "undefined") {
	    cookieStr += escape(value);
	  }
	  if (!expires) {
	    expires = new Date();
	    expires.setTime(expires.getTime()+365*24*60*60*1000);
	  }
	  cookieStr += "; expires="+ expires.toGMTString() +";";
	  document.cookie = cookieStr;
	}

function getCookie(name){
	  var str = '; '+ document.cookie +';';
	  var index = str.indexOf('; '+ escape(name) +'=');
	  if (index != -1) {
	    index += name.length+3;
	    var value = str.slice(index, str.indexOf(';', index));
	    return unescape(value);
	  }
	  return '';
	}

function deleteCookie(name){
	  var past = new Date();
	  past.setTime(0); // 1970-01-01
	  setCookie(name, null, past);
	}

//Ulubione
function ulubione(obj) {
	var title = document.title;
	var url = document.location.href;
	if(window.sidebar) {
		window.sidebar.addPanel(title, url, "");
		obj.href = "#";
		return false;
	}
	else if(window.external) {
		window.external.AddFavorite(url, title);
		obj.href = "#";
		return false;
	}
	else if(window.opera && window.print) {
		obj.rel = 'sidebar';
		obj.title = title;
		obj.href = url;
		return true;
	}
	return false;
}

//Startowa
function startowa(obj) {
	var url = document.location.href;
	if(url.toLowerCase().indexOf('http://') != 0) {
		url = 'http://' + url;
	}
	var przegladarka = navigator.userAgent;
	var ie_ver = 0;
	if(przegladarka.indexOf("MSIE") != -1 && przegladarka.indexOf(") ") == -1) {
		ie_ver = parseInt(przegladarka.substring(przegladarka.indexOf("MSIE")+5));
	}
	if (ie_ver >= 4) {
		obj.style.behavior='url(#default#homepage)';
		obj.setHomePage(url);
	}
	return false;
}

/**
 * funkcja formatująca podpis w fancyboxie w widoku galerii
 */
function formatTitle(opis, currentArray, currentIndex, currentOpts) {
	title = $(currentArray[currentIndex]).children('img').attr('title');

	titleBox = '<div></div>';
	if ((title && title.length) || (opis && opis.length))
		titleBox = '<div id="fancyboxTitle" class="ui-corner-all">'
			+ (title && title.length ? '<b>' + title + '</b>' : '')
			+ (title && title.length && opis && opis.length ? '<br />' : '')
			+ (opis && opis.length ? opis : '')
			+ '</div>';

    return titleBox;
}

/**
 * funkcja ukrywająca wszystkie zakładki w widoku
 */
function hideAllTabs() {
	$("#menu_tabs_top .tab_content").slideUp("fast");
	$("#menu_tabs_right .tab_content").animate({right: -500}, 'fast');
	$("#menu_tabs_bottom .tab_content").slideUp("fast");
	$("#menu_tabs_left .tab_content").animate({left: -500}, 'fast');
}

/**
 * funkcja dołączająca plik js
 *
 * @param path string scieżka do pliku
 */
var scripts = new Array();
function includeScript(path) {
	if (in_array(path, scripts))
		return false;
	scripts[scripts.length] = path;
	$("head script:last").after('<script type="text/javascript" charset="utf-8" language="JavaScript" src="'+path+'"></script>');
	return true;
}

/**
 * funkcja dołączająca plik css
 *
 * @param path string scieżka do pliku
 */
function includeStyle(path) {
	if (!$("head").find('link[href="'+path+'"]').length)
		$("head link:last").after('<link rel="stylesheet" charset="utf-8" type="text/css" href="'+path+'" />');
}

/**
 * funkcja identyczna jak w PHP
 *
 * @param needle mixed szukany element
 * @param haystack array tablica do przeszukania
 * @return bool czy znaleziono
 */
function in_array(needle , haystack){
	for (var i = 0; i < haystack.length; i++) {
		if (haystack[i] == needle)
			return true;
	}
	return false;
}

/**
 * funkcja przesuwająca linki w środku wskazanego diva
 *
 * @param target string id diva
 * @param speed int ilość milisekund na przewinięcie (domyślnie 1500)
 * @param stop int ilość milisekund do następnego przewinięcia (domyślnie połowa speed)
 */
function slideLogos(target, speed, stop){
	if (typeof(speed) != "number")
		speed = 1500;
	if (typeof(stop) != "number")
		stop = Math.round(speed/2);
	a = $("#"+target+" div a:first");
	a.animate({
		"margin-left": -1*a.outerWidth(true)
	}, speed, 'easeInOutCirc', function(){
		$("#"+target+" div").append(a);
		a.css("margin-left", 0);
		setTimeout(function(){slideLogos(target, speed, stop);}, stop);
	});
}

$(document).ready(function() {

//walidacja pól formularzy
	$(".validate").each(function(){
		$(this).validate({
			errorElement: "em"
		});
		$(this).submit(function(){
			if (!$(this).valid()) {
				$(".check em.error").each(function(){
					$(this).parent().addClass("error");
					$(this).parent().children("input").change(function(){
						$(this).parent().removeClass("error");
						$(this).parent().children("em").removeClass("error");
					});
				});
				notice('Należy poprawnie wypełnić pola obowiązkowe!', "error");
				return false;
			} else
				return true;
		});
	});

//fancybox
	if ($("a.fancybox").length) $("a.fancybox").fancybox({
		transitionIn	:	'easing',
		transitionOut	:	'easing',
		speedIn			:	600,
		speedOut		:	200,
		overlayShow		:	true,
		titleFormat		:	formatTitle
	});

//youtube player
	if ($(".youtubeplayer").length){
		$(".youtubeplayer").each(function(){
			$(this).height($(this).parent().width()*3/4);
		});
	}

//flash player
	if (typeof flowplayer != "undefined"){
		$("a.videoplayer").each(function(){
			switch(parseInt($(this).attr('rel'))){
				case 1:
					options = {
						autoPlay: false,
						autoBuffering: true
					};
					break;
				case 2: 
					options = {
						autoPlay: true,
						autoBuffering: true
					};
					break;
				default:
					options = {
						autoPlay: false,
						autoBuffering: false
					};
					break;
			}
			$(this).height($(this).width()*3/4);
			$(this).flowplayer("swf/flowplayer/flowplayer-3.2.5.swf", {
				plugins: {
					controls: {
						url: 'swf/flowplayer/flowplayer.controls-3.2.3.swf',
						playlist: false,
						//autoHide: 'never',
						play:true,
						volume:$(this).width()>=180,
						mute:$(this).width()>=380,
						time:$(this).width()>=360,
						stop:$(this).width()>=400,
						playlist:false,
						fullscreen:true
					}
				},
				clip: options
			});
		});
		$("a.audioplayer").each(function(){
			switch(parseInt($(this).attr('rel'))){
			case 1: 
				options = {
					autoPlay: true,
					autoBuffering: true
				};
				break;
			default:
				options = {
					autoPlay: false,
					autoBuffering: false
				};
				break;
			}
			$(this).flowplayer("swf/flowplayer/flowplayer-3.2.5.swf", {
				playlist: [
					{url: $(this).attr("href")}
				],
				plugins: {
					controls: {
						url: 'swf/flowplayer/flowplayer.controls-3.2.3.swf',
						playlist: false,
						autoHide: 'never',
						play:true,
						volume:$(this).width()>=100,
						mute:$(this).width()>=300,
						time:$(this).width()>=280,
						stop:$(this).width()>=320,
						playlist:false,
						fullscreen:false
					}
				},
				clip: options
			});
		});
	}

//kalendarzyk do wyboru daty
	if ($(".datepicker").length) {
		$(".datepicker").datepicker({
			dateFormat: 'yy-mm-dd',
			firstDay: 1,
			changeMonth: true,
			changeYear: true,
			constrainInput: true
		});
	}

	//accordion w newsach
	if ($("#news_accordion").length) {
		$("#news_accordion input[type='submit']").button();
		$("#news_accordion").accordion({
			autoHeight: false
		});
	}

	//zakładki
	if ($(".tab_headers button").length) {
		$(".tab_headers button").each(function(){
			$(this).button().click(function(){
				if ($(this).hasClass('right')){
					if ($("#tab_right_"+$(this).attr('rel')).css("right") == "-500px") {
						hideAllTabs()
						$("#tab_right_"+$(this).attr('rel')).animate({right: 0}, "slow");
					} else
						$("#tab_right_"+$(this).attr('rel')).animate({right: -500}, "slow");
				} else if ($(this).hasClass('left')) {
					if ($("#tab_left_"+$(this).attr('rel')).css("left") == "-500px") {
						hideAllTabs()
						$("#tab_left_"+$(this).attr('rel')).animate({left: 0}, "slow");
					} else
						$("#tab_left_"+$(this).attr('rel')).animate({left: -500}, "slow");
				} else {
					if ($(this).hasClass('bottom'))
						pos='bottom';
					if ($(this).hasClass('top'))
						pos='top';

					if ($("#tab_"+pos+"_"+$(this).attr('rel')).css("display") == "none") {
						hideAllTabs()
						$("#tab_"+pos+"_"+$(this).attr('rel')).slideDown("slow");
					} else
						$("#tab_"+pos+"_"+$(this).attr('rel')).slideUp("slow");
				}
			});
			if ($(this).hasClass("right"))
				$("#tab_right_"+$(this).attr('rel')).css("margin-top", $("#tab_right_"+$(this).attr('rel')).innerHeight()/(-2));
			else if ($(this).hasClass("left"))
				$("#tab_left_"+$(this).attr('rel')).css("margin-top", $("#tab_left_"+$(this).attr('rel')).innerHeight()/(-2));
		});
		if ($("#menu_tabs_top").length)
			$("#menu_tabs_top").css("margin-left", $("#menu_tabs_top").width()/(-2));
		if ($("#menu_tabs_right").length)
			$("#menu_tabs_right").css("margin-top", $("#menu_tabs_right").height()/(-2));
		if ($("#menu_tabs_bottom").length)
			$("#menu_tabs_bottom").css("margin-left", $("#menu_tabs_bottom").width()/(-2));
		if ($("#menu_tabs_left").length)
			$("#menu_tabs_left").css("margin-top", $("#menu_tabs_left").height()/(-2));
	}
});

