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

$(document).ready(function() {

//walidacja pól formularzy
	if ($(".validate").length) {
		$(".validate").validate({
			errorElement: "em"
		});
		$(".validate").submit(function(e){
			if (!$(e.currentTarget).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");
					});
				});
				message({msg: 'Należy poprawnie wypełnić pola obowiązkowe!', button: true});
				return false;
			}
		});
	}

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

//flash player
	if (typeof flowplayer != "undefined"){
		$("a[href*=.flv]").flowplayer("../swf/flowplayer/flowplayer-3.2.5.swf");
//		setTimeout(function(){$("a[href*=.flv] object").append('<param name="wmode" value="opaque" />');}, 1500);
	}
	
//kalendarzyk do wyboru daty
	if ($(".datepicker").length) {
		$(".datepicker").datepicker({
			dateFormat: 'yy-mm-dd',
			firstDay: 1,
			changeMonth: true,
			changeYear: true,
			constrainInput: true
		});
	}
});

