

function showDivLoader(idDiv, idLoader){
	try{
	  Position.includeScrollOffsets = false;
	  
	  var oLoader = $(idLoader);
	  var oBox = $(idDiv);

	  var whBox = oBox.getDimensions();
	  var whLoader = oLoader.getDimensions();
	  var xyBox = Position.cumulativeOffset(oBox);
			
	  var xoLoader = xyBox[0] + (whBox.width/2) - (whLoader.width/2);
	  var yoLoader = xyBox[1] + (whBox.height/2) - (whLoader.height/2)-100;
	
	  oLoader.setStyle({ top: yoLoader + 'px', left: xoLoader + 'px'});
	  oLoader.show();
	  
	  new Effect.Fade(oBox, {to: 0.7, duration: 0});
	}
	catch(e){
		showDbgAlert(e);
	}
}

function hideDivLoader(idDiv, idLoader){
	try{
	  var oLoader = $(idLoader);
	  var oBox = $(idDiv);
	  oLoader.hide();
	  new Effect.Fade(oBox, {to: 1, duration: 0.1});
	}
	catch(e){
		showDbgAlert(e);
	}
}

		

function getObject(name) {
	if (document.getElementById) {
    	obj=document.getElementById(name);
   	} else if (document.layers) {
     	obj=document.layers[name];
	} else if (document.all) {
	   obj=document.all[name];
	} else {
	   obj=false;
	}
	return obj;
}

function bookmarkSite(title, url){
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "")
}


function showObject(item){
	item.style.display = "block";
}

function hideObject(item){
	item.style.display = "none";
}

function showDivV(item){
	getObject(item).style.visibility = "visible";
}

function hideDivV(item){
	getObject(item).style.visibility = "hidden";
}


function showDivRowInTable(name){
	var divObj = getObject(name);
	showRow = (navigator.appName.indexOf("Internet Explorer") != -1) ? "block" : "table-row";
	divObj.style.display = showRow;
}
function showDiv(name){
	var divObj = getObject(name);
	divObj.style.display = "block";
}

function hideDiv(name){
	var divObj = getObject(name);
	divObj.style.display = "none";
}

function changeColorOver(elem, clr, brclr){
	elem.style.backgroundColor = brclr;
	elem.style.color = clr;
}

function changeColorOut(elem, clr, brclr){
	elem.style.backgroundColor = brclr;
	elem.style.color = clr;
} 


function searchVideo(){
	var search = getObject('search'); 
	var formsearch = getObject('formsearch'); 
	formsearch.submit();
}

function searchVideoNavTab(){
	var search = getObject('searchmain'); 
	var formsearch = getObject('formsearchmain');
	formsearch.submit();
}


function submitEnter(myfield,e){ 
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13) {searchVideo(); return false;}
	else return true;
}	

DataItems = function(name) { 
  this.name = name; 
  this.items = new Array(); 
} 

DataItems.prototype.addItem = function(id, item) { 
  this.items[this.items.length] = new Array(id, item);
}

DataItems.prototype.removeItem = function(id) {
  this.items.splice(id,1);
}



String.prototype.addClass = function(theClass) {
	if (this != "")
		if (!this.classExists(theClass)) {return this + " " + theClass;}
	else return theClass;
	return this;
}

String.prototype.classExists = function(theClass) {
	var regString = "(^| )" + theClass + "\W*";
	var regExpression = new RegExp(regString);
	if (regExpression.test(this)) return true;
	return false;
}

String.prototype.removeClass = function(theClass){
	var regString = "(^| )" + theClass + "\W*";
	var regExpression = new RegExp(regString);
	return this.replace(regExpression, "");
}

function LTrim(str) {
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(0)) != -1) {
  // We have a string with leading blank(s)...
  var j=0, i = s.length;
  // Iterate from the far left of string until we
  // don't have any more whitespace...
  while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
	  j++;
	  s = s.substring(j, i);
  }
  return s;
}

function RTrim(str) {
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    // We have a string with trailing blank(s)...
    var i = s.length - 1;       // Get length of string
    // Iterate from the far right of string until we
    // don't have any more whitespace...
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      	i--;
		s = s.substring(0, i+1);
  	}
	return s;
}


function Trim(str) {
  return RTrim(LTrim(str));
}



	
function validate_email(email){
  var value = Trim(email);
  
  if (value == '')
			return 'Adres e-mail jest wymagany!';
//		return 'Podaj Twój adres email!';

  var regex = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
  
  if (value != '' && !regex.test(value))
				return 'Podany adres e-mail jest nieprawidłowy!';
//  		return 'Twój email jest nieprawidłowy!';
  
  return false;
  
 }
	
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function evalJson(data){
	return eval("(" + data + ")");
}

function dbgArr(arr){
	for(i in arr)
		alert(i+'->'+arr[i]);
}

function insertAfter(el, newEl){
		var el;
		if(el.nextSibling) 
			el.parentNode.insertBefore(newEl, el.nextSibling);
		else 
			el.parentNode.appendChild(newEl);
	}


function openwindow(url, width, height){
	window.status='Edycja obiektu';
	var okno = window.open(url,"DisplayWindow","scrollbars=yes,toolbar=no,directories=no,menubar=no,resizable=no,width=" + width + ", height="+height);
	okno.moveTo((screen.availWidth/2)-(width/2),(screen.availHeight/2)-(height/2));
	okno.focus();}

function PostRequest(url, post, fref_name, divBox, divLoader){
		new Ajax.Request(url, 
		{
 			method: 'post',
			encoding: 'UTF-8',
			parameters: post ,

			'onCreate' : function(req){
				try{
					if(divBox == -1){
						$(divLoader).show();
					}
					else
						showDivLoader(divBox, divLoader);
						//$(divLoader).show();
						//$(divLoader).innerHTML = '<p style="text-align:center;vertical-align:middle;margin:0px;padding:5px;"><img src="/img/loading.gif" style="border:0;margin-right:5px;" />Loading ... </p>';		
					}catch(e){
					}		
			},
			
			'onComplete' : function(req){
				try{
					if(divBox == -1){
						$(divLoader).hide();
					}
					else{

						setTimeout(function(){hideDivLoader(divBox, divLoader)},400);
					}	
						var url = req.getResponseHeader('location');
						if(url) location.href = url;

					}
					catch(e){}
     		},
	
			'onSuccess' : function(req){
				try {
						if(fref_name.substr(0,1)=='_'){
							eval('onMySuccess'+ fref_name + '(req)');
						}
						else{
							eval(fref_name+'(req)');
						}
				}
				catch (e){ 
					alert(e.name + ": " + e.message);
				}
				
				return true;
			}
		});
}


function GetRequest(url, fref_name, divBox, divLoader,fref_args){
		new Ajax.Request(url, 
		{
 			method: 'get',
			encoding: 'UTF-8',
			'onCreate' : function(req){
				try{
					if(divBox == -1){
						$(divLoader).show();
						$(divLoader).scrollTo();
					}
					else
						showDivLoader(divBox, divLoader);
						//$(divLoader).show();
						//$(divLoader).innerHTML = '<p style="vertical-align:middle;margin:0;padding:0;"><img src="/img/loading.gif" style="border:0;margin-right:5px;" />Loading ... </p>';		
					}catch(e){}		
			},
			
			'onComplete' : function(req){
				try{
					var url = req.getResponseHeader('location');
					if(url)
						location.href = url;
					}catch(e){}
     		},
	
			'onSuccess' : function(req){
				try {
					if(divBox == -1){
						$(divLoader).hide();
					}
					else{
						setTimeout(function(){hideDivLoader(divBox,divLoader)},500);
					}

					if(fref_name.substr(0,1)=='_'){
						fref_name = 'onMySuccess'+fref_name;
					}
					eval(fref_name+'(req,fref_args)');
					
				} catch (e) {  }
				return true;
			}
		});

		return;
}


function ec_alert(txt){
	alert(txt);
}

function ec_confirm(txt, callbackFunction){
	return confirm(txt);
}


function wheelevent(e){
	if(!e)e=window.event;if(e.preventDefault)e.preventDefault();e.returnValue=false;
}

// Navigation generator 
// ****************************************************************	
function genNavigation(script_name){
	return Try.these(
		function(){
			var nav = Navigation.get( {"iNumPage" : oXML['iNumPage'] });
			nav.iPageCount = oXML['iPageCount'];
			nav.iAllCount = oXML['iAllRecords'];
			nav.sExtraParam = '';
			nav.iJsFunction = 1;
			nav.sScriptName = script_name;
			nav.makeHTML();
			sExtraNav = '';
			if(oXML['iAllRecords'] > 0){
				$("divNavigation").show();	
				$("divNavigation").innerHTML = nav.sHtml;
				return true;
			}
			else{
				$("divNavigation").hide();
				return false;
			}
		});
}

function dbg_alert(e, source){
	//alert('s:'+source+' |> '+e.name +' : '+e.message +' : '+e);
}

function showDbgAlert(e){
	//alert(e.name +' : '+e.message +' : '+e);
}


/* -----------------------------------*/
/* --->>> onDOMReady Extension <<<----*/
/* -----------------------------------*/
// Adapted from DOM Ready extension by Dan Webb
// http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
// which was based on work by Matthias Miller, Dean Edwards and John Resig
//
// Usage:
//
// Event.onReady(callbackFunction);
Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (Event._timer)  clearInterval(Event._timer);
    
    Event._readyCallbacks.each(function(f) { f() });
    Event._readyCallbacks = null;
    
  },
  onReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady;
      
      if (domReady.done) return f();
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            var dummy = location.protocol == "https:" ?  "https://javascript:void(0)" : "javascript:void(0)";
            document.write("<script id=__ie_onload defer src='" + dummy + "'><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") { domReady(); }
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});



function openWindowList(url, width, height){
	window.status='';
	var okno = window.open(url,"DisplayWindow","scrollbars=yes,toolbar=yes,directories=no,menubar=no,resizable=yes,width=" + width + ", height="+height);
	okno.moveTo((screen.availWidth/2)-(width/2),(screen.availHeight/2)-(height/2));
	okno.focus();
}
function openPlayerWindow(url, width, height){
	window.status=location.protocol;
  var okno = window.open("/player/?url="+url);
  okno.moveTo((screen.availWidth/2)-(width/2),(screen.availHeight/2)-(height/2));
  okno.focus();
}

function _submitEnter(e, func){ 
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13) { 
		try{
			eval(func);			
		}
		catch(e){}
		posAccepted = 1;
		return false;
	}
	else return true;
}

function prepareInputsForHints() {
		  var inputs = document.getElementsByTagName("input");
		  for (var i=0; i<inputs.length; i++){
		    inputs[i].onfocus = function () {
					try{

			      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";

					}catch(e){}
		    }
		    inputs[i].onblur = function () {
					try{

			      this.parentNode.getElementsByTagName("span")[0].style.display = "none";

					}catch(e){}
		    }
		  }
		  
			var selects = document.getElementsByTagName("select");
		  for (var k=0; k<selects.length; k++){
		    selects[k].onfocus = function () {
					try{
		      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
					}catch(e){}
		    }
		    selects[k].onblur = function () {
					try{
		      this.parentNode.getElementsByTagName("span")[0].style.display = "none";
					}catch(e){}
		    }
		  }
		  var textareas = document.getElementsByTagName("textarea");
		  for (var m=0; m<textareas.length; m++){
		    textareas[m].onfocus = function () {
					try{
		      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
					}catch(e){}
		    }
		    textareas[m].onblur = function () {
					try{
		      this.parentNode.getElementsByTagName("span")[0].style.display = "none";
					}catch(e){}
		    }
		 	}
}

function roundNumber(num, dec) {
		var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
		return result;
}

function setCookie(name,value,days) {
   if (days) {
     var date = new Date();
     date.setTime(date.getTime()+(days*24*60*60*1000));
     var expires = ";expires="+date.toGMTString();
   } else {
     expires = "";
   }
   document.cookie = name+"="+value+expires+";path=/";
  }
 
function readCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}



function getFilesPatgCfg(data){
	if(data['file_type']=='video'){
		return object_mfile_url;
	}
	else{
		return object_img_url;
	}
}


function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}


function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

