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);
	}

function ulubione(obj) {
   var title = document.title;
   var url = document.location;
   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;
}

function startowa() {
	
	var adres = document.location;

	if (adres.toLowerCase().indexOf('http://') != 0)
		adres = 'http://' + adres;

	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) {
		this.style.behavior='url(#default#homepage)';
		this.setHomePage(adres);
	}
	return false;
}

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;
}
		
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');
}

function includeScript(path) {
	$("head").append('<script type="text/javascript" charset="utf-8" language="JavaScript" src="'+path+'"></script>');
}

function includeStyle(path) {
	$("head").append('<link rel="stylesheet" charset="utf-8" type="text/css" href="'+path+'" />');
}

function notice(msg, type) {
	if (typeof(type) == "undefined")
		type = "notice";
	
	if (type == "error")
		$("#notice").removeClass("ui-state-highlight").addClass("ui-state-error").html("<span style='float: left; margin-right: .3em;' class='ui-icon ui-icon-alert'></span>"+msg)
	else
		$("#notice").removeClass("ui-state-error").addClass("ui-state-highlight").html("<span style='float: left; margin-right: .3em;' class='ui-icon ui-icon-info'></span>"+msg);

	$("#notice").parent().show();
}

$(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(){
			$(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: {
					autoPlay: false,
					autoBuffering: true
				}
			});
		});
		$("a.audioplayer").each(function(){
			$(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: {
					autoPlay: false,
					autoBuffering: true
				}
			});
		});
	}

//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").accordion({
			autoHeight: false
		});
	}
	
	//zakładki
	if ($(".tab_headers button").length) {
		
		$(".tab_headers button").each(function(){
			$(this).button().click(function(){
				if ($(this).parent().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).parent().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).parent().hasClass('bottom'))
						pos='bottom';
					if ($(this).parent().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).parent().hasClass("right"))
				$("#tab_right_"+$(this).attr('rel')).css("margin-top", $("#tab_right_"+$(this).attr('rel')).innerHeight()/(-2));
			else if ($(this).parent().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));
	}
});

