// www.radio-plus.hr
// main javascript file
// property of studioat.hr


/////////////// korisne fcije

//execution time object
function exectime(){
this.date1 = new Date(); 
this.milliseconds1 = this.date1.getTime();
this.get = function() {
	var date2 = new Date(); 
	var milliseconds2 = date2.getTime(); 
	var difference = milliseconds2 - this.milliseconds1; 
	alert(difference + ' ms');
}
}

// returns handle of elements parent div
// ex. parentdiv(this).id -> returns parent divs id
// 		 parentdiv(this).className -> returns parent divs id
function parentdiv(handle){
if(handle.parentNode.nodeName=='BODY') return false;
else if(handle.parentNode.nodeName=='DIV') return handle.parentNode;
else return parentdiv(handle.parentNode);
}

function isinnode(handle, ajdi){
if(handle.parentNode.nodeName=='BODY') return false;
else if(handle.parentNode.id==ajdi) return true;
else return isinnode(handle.parentNode, ajdi);
}

function in_array(val, aray){
    var i;
    for (i=0; i < aray.length; i++) {
        if (aray[i] == val) return true;    // ===
    }
    return false;
};


///////////////////////////////////////////////////


// eksternalizacija linkova za van
function outLnk() {
	if(!document.links) return;
	for (i = 0; i < document.links.length; i++) {
		var lnk = document.links[i];
		if (lnk.href.match(/^(http|https|ftp)/) && (lnk.href.indexOf(location.hostname) == -1))
			lnk.setAttribute('target', '_blank'); 	
		
		if( in_array(lnk.id, new Array('ico_i','ico_k')) ) lnk.setAttribute('target', '_blank'); 	
	}
}


// reset kuki
function DeleteCookieOnClick(ajdi) {
	var handle = document.getElementById(ajdi);
	if(!handle) return;
	var links = handle.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) {
		if(!links[i].onclick)
		links[i].onclick = function(){ 
					//alert("reset kukija"); 
					deleteCookie("cat_for_show");
					return true; 
					}
		}
}

function cookieLnk() {
//var lol = new exectime();
DeleteCookieOnClick('st3');
DeleteCookieOnClick('st4');
DeleteCookieOnClick('vezane');
//lol.get();
}



// otvaranje popupa
function popme(adr, sir, vis){
	var novi = window.open(adr, 'poped', 'width='+sir+',height='+vis+',scrollbars=yes');
	if (novi.focus) { novi.focus(); }
}


// f-ije za srch formu
function chekSrch() {
	termin = document.getElementById('srchp').value;
	if(termin=='Unesite traženi pojam...' || termin == '') { alert('Niste upisali pojam za pretraživanje!'); return false; } 
	else if(termin.length < 3) { alert('Pojam mora biti barem 3 simbola!'); return false; }
	else { document.getElementById('srchf').action="http://www.radio-plus.hr/search/"+termin; return true; }
}
function onSrchFcs() {if(document.getElementById('srchp').value=='Unesite traženi pojam...'){document.getElementById('srchp').value=''};}
function onSrchBlr() {if(document.getElementById('srchp').value==''){document.getElementById('srchp').value='Unesite traženi pojam...'};}


// sort galerije
function gallSort() {
var forma = document.getElementById('gallsort');
forma.action="http://www.radio-plus.hr/galerija/"+forma.prikaz.options[forma.prikaz.selectedIndex].value;
return true;
}


// wtecajna f-ije
function calculate(ulazni){
	if(ulazni == 'in1'){
		var ulazni = document.wtecajna_calc.in1;
		var izlazni = document.wtecajna_calc.in2;
		var fakt_ul = document.wtecajna_calc.sel1; 
		var fakt_iz = document.wtecajna_calc.sel2;
	} else {
		var ulazni = document.wtecajna_calc.in2;
		var izlazni = document.wtecajna_calc.in1;
		var fakt_ul = document.wtecajna_calc.sel2; 
		var fakt_iz = document.wtecajna_calc.sel1;
	}
	if(isNaN(ulazni.value)) izlazni.value = ":]";
	else {
	var rez = fakt_ul.options[fakt_ul.selectedIndex].value / fakt_iz.options[fakt_iz.selectedIndex].value * ulazni.value;
	izlazni.value = rez.toString().substr(0,15);
	}
}

// gornji sat f-ije
function atomclock_init(){
  var http = null;
  if(window.XMLHttpRequest)
    http = new XMLHttpRequest();
  else if (window.ActiveXObject)
    http = new ActiveXObject("Microsoft.XMLHTTP");

  http.onreadystatechange = function()
  {
    if(http.readyState == 4){ 
    server_time = http.responseText;
    var vrStart = new Date();
		vrRaz = server_time - vrStart.getTime();
		atomclock();
		setInterval("atomclock()", 500);
		}
  };
  http.open('GET', 'http://www.radio-plus.hr/content/ajax/time.php', true);
  http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  http.send(null);
}

function atomclock(){
	var vrUser = new Date();
	var vrijeme = vrUser.getTime() + vrRaz;
	var vrOut = new Date(vrijeme);
	var oH = (vrOut.getHours() < 10) ? ("0" + vrOut.getHours()) : vrOut.getHours();
	var oM = (vrOut.getMinutes() < 10) ? ("0" + vrOut.getMinutes()) : vrOut.getMinutes();
	var oS = (vrOut.getSeconds() < 10) ? ("0" + vrOut.getSeconds()) : vrOut.getSeconds();
	document.getElementById('vrtopdate').innerHTML = oH + ":" + oM + ":" + oS;
}


//highlight za search termine

function doHighlight(bodyText, searchTerm) 
{
    highlightStartTag = "<font style='background-color:yellow;'>";
    highlightEndTag   = "</font>";
  
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {

      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
      }
    }
  }
  
  return newText;
}

function highlightSearchTerms(searchText, treatAsPhrase)
{
  if (treatAsPhrase) searchArray = [searchText];
  else searchArray = searchText.split(" ");
  
  if (!document.getElementById('st2') || typeof(document.getElementById('st2').innerHTML) == "undefined") return false;

  var bodyText = document.getElementById('st2').innerHTML;
  
  for (var i = 0; i < searchArray.length; i++) 
  	bodyText = doHighlight(bodyText, searchArray[i]);

  document.getElementById('st2').innerHTML = bodyText;
  return true;
}



/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {

if(!expires) expires = 1; //1 dan
var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expires * 24 * 3600 * 1000));
ExpireDate.toGMTString();

    document.cookie= name + "=" + escape(value) +
        "; expires=" + ExpireDate +
        "; path=" + ((path) ?  path : "/") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(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));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            "; path=" + ((path) ? path : "/") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}





