/* Cookies */
function setCookie(c_name,value,expiredays)
{
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
  if (document.cookie.length>0)
  {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
    {
      c_start=c_start + c_name.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

function checkCookie(cname)
{
  var cval = Number(getCookie(cname));
  var expiredays = 1;  
  if (cval!=null || cval!="")
  {
    cval = cval + 1;
	setCookie(cname, cval, expiredays);	
	if ( cval % 1 == 0 || cval == 1)
	{
		$("#banners_6:not(:empty)").each(function(i){
			$("#popupadsdialog").dialog("open");
        });       
	}
	
  }  
}

function delCookie(cname) 
{	
	//document.cookie = cname + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 
/*  end -- Cookies */

function loadHtmlBlock(targetId, blockUrl) 
{
	var currentTime = new Date();
	$.get(blockUrl + '?' + currentTime.getTime(), function(data) 
	{
		$('#' + targetId).html(data);
	});
}

function GetGoogleStatsCounter()
{
  var d = new Date();
  $("#googlestats").load("/analytics/google_analytics.html?"+d.getTime());   
}

$(function() 
{
/*
  $("#news_comments").dialog({
    autoOpen: false,
    resizable: false,
    position : [340,60]
  });
  
  $("#feedback").dialog({
    autoOpen: false,
    resizable: false,
    position : [340,60]
  });
  */
  var url = false;
  if ( url = getHash() )
  {
    window.location = 'http://'+window.location.hostname+'/ro/news/'+url;
  }
  
});

function getHashFromString(hash) 
{			
		     if (!hash) return false;
		     hash = hash.replace(/^.*#/, '');		     		     	    		    
		     if ( hash == '' || !isNaN(hash) ) return false;
		     return (hash);
}

function addHash (hash)
{
  var address = new Array();
  address = document.location.href.split('/');   
  
  if (!address[5])
    return document.location.hash = hash;  
   
  if (address[5] != hash)
    return document.location.hash = hash;
  
  if (address[5] == hash)
    return document.location.hash = '';
}
	        	        
function getHash() 
{
   var hash = document.location.hash;
   return getHashFromString(hash);
}

//***** Video Player **********
function SetPageTitle(pTitle)
{
  document.title = pTitle;
}

function SetNewsTitle(pTitle)
{
  $('#NewsTitle').text(pTitle); 
}

function SetNewsHash(pUrl)
{
  addHash (pUrl); 
}
//***** end ** Video Player **********

//**** Comments ********
function SetCommetsSource(pURL, idDiv)
{
   var d = new Date();  
   var div_if = document.getElementById(idDiv);
   if (div_if)
   {
       div_if.innerHTML = '';
       div_if.innerHTML = '<iframe src="" id="'+idDiv+'_iframe" name="'+idDiv+'_iframe" scrolling="auto" height="655px" frameborder="0" width="100%" marginheight="0" marginwidth="0"  ></iframe>';
       
       var el1 = document.getElementById(idDiv+'_iframe');
       if (el1) el1.src = pURL+'&id='+d.getTime();
   }
}

function SetViewsSource(pURL, idDiv)
{
   var d = new Date();  
   var div_if = document.getElementById(idDiv);
   if (div_if)
   {
       div_if.innerHTML = '';
       div_if.innerHTML = '<iframe src="" id="'+idDiv+'_iframe" name="'+idDiv+'_iframe" scrolling="no" height="16px" frameborder="0" width="100%" marginheight="0" marginwidth="0"  ></iframe>';
       
       var el1 = document.getElementById(idDiv+'_iframe');
       if (el1) el1.src = pURL+'&id='+d.getTime();
   }
                                 
}
//***** end Comments ********

//***** Banners *************
var mainTdWidth = 960;
function getScrollY() 
{
	if (window.screen)
	{
		var scroll;
		if ($.browser.msie) {
			scroll = document.documentElement.scrollTop;
		}
		else {
			scroll = window.scrollY;
		}
		return scroll;
	 }
	return 0;
}

function getWindowWidth() 
{
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

function InsetBanners (div_id, file)
{
  var d = new Date();
  if (document.getElementById(div_id))
  {
    $.get('/banners/'+file+'.html?id='+d.getTime(),'', 
     function (data) 
     { 
         $('#' + div_id).html(data); 
         if (div_id == 'banners_6') checkCookie("primefm_popup_ads_counter");
     }
    );  
  }  
}

function InsetBannersGoogle (div_id, file, pos)
{
  var d = new Date();  
  if (document.getElementById(div_id))
  {
    $.get('/banners/'+file+'.html?id='+d.getTime(),'', 
     function(loadedHtml){
    	$('#'+div_id).html(loadedHtml);
    	$('#if_banner_'+pos).attr('src', '/banners/content_'+file+'.html?id='+d.getTime());
     });   
  }  
}

function InsetBannersBody (file, color)
{ 
  var d = new Date();   
  
  $.get('/banners/'+file+'.html?id='+d.getTime(),'', 
    function(loadedHtml)
    {
      loadedHtml = loadedHtml.replace(' ', '');				
	  if (loadedHtml && loadedHtml != '') 
		$("body").css('background', 'url('+loadedHtml+') no-repeat center top'+color);
    });  
}

var scroll_baners = new Array();

function FindBanners (Banners,pos)
{

 //--- Calculam ponderea de aparitie
 var pon = 0;
 for (i=0; i<Banners.length; i++) if (Banners[i].possition == pos) pon = pon + Banners[i].priority;       
 var round = Math.random();   
 var con = 1;
 var exists = true;
    
 for (i = Banners.length-1; i>=0; i--)
 {
   if (Banners[i].possition == pos)
   {
         con = con - Banners[i].priority/pon;
         //--- Includem banerul la locul lui ----
         if (round > con) 
         { 
           InsetBanners ('banners_'+Banners[i].possition, Banners[i].id);           
           i = 0;
         }
         
        exists = false;          
   }   
 } 
 
 if (exists)
 {
   //--- verificam daca categoria data are banere daca nare punem google ---
   //InsetBanners ('banners_'+pos, 'google_'+pos);
   /*   
     if (pos == 1 || pos == 2 || pos == 3 || pos == 11)
      InsetBannersGoogle ('banners_'+pos, 'google_'+pos, pos);
     else      
      InsetBanners ('banners_'+pos, 'google_'+pos);
   */
 }     
 /* Banere din parti cu scroll */
 /*
 window.onscroll = function()
 {					    		  
   var scroll = getScrollY();	
   for (i=0; i<scroll_baners.length; i++)
   {	
    $('#banners_'+scroll_baners[i]).css('top', scroll + 20);
   }
 }
 */
 
} 

function GetBanners (pos)
{   
   var d = new Date();
   $.get('/banners/banners.js?id='+d.getTime(), '', 
     function (data) 
     {       
       eval(data);       
       FindBanners(Banners,pos);       
     }
    ); 
}
//****end* Banners *************
