var clickedFromMenu = false;
var mainTdWidth = 986;

function sel_menu (parent_id, current_id)
{ 
 /* --- deselectam toate meniurile --- */
 var nr_node = document.getElementById(parent_id).childNodes.length;
 
 for (i=0; i<nr_node; i++)
 {
   if (document.getElementById(parent_id).childNodes[i].id)
   {
    var child_id = document.getElementById(parent_id).childNodes[i].id;
    if (child_id.substring(0,4) == 'sub-')
    {       
     document.getElementById(child_id).className =" sub_menu";
     document.getElementById('subimg-'+child_id.substring(4,child_id.length)).innerHTML ='<img alt="" src="/img/site/s1.gif"/>';
     document.getElementById('subspan-'+child_id.substring(4,child_id.length)).className =" sub_menu"; 
    }    
   }
 }
 
 /* --- il selectam pe cel cerut   --- */
 document.getElementById('sub-'+current_id).className =" sub_menu_sel";
 document.getElementById('subimg-'+current_id).innerHTML ='';
 document.getElementById('subspan-'+current_id).className =" sub_menu_sel";
} 

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;
}

//***** Banners *************
function InsetBanners (div_id, file)
{
  var d = new Date();
  if (document.getElementById(div_id))
  {
    $.get('/banners/'+file+'.html?id='+d.getTime(),'', 
     function (data) 
     {         
        if (div_id == 'banners_9' || div_id == 'banners_10')
          $('.' + div_id).html(data);                 
        else   
         $('#' + div_id).html(data);        
     }
    );  
  }  
}

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)
{
  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 fixed center top');
    });  
}

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) 
         { 
          if (pos == 6)
           InsetBannersBody (Banners[i].id);
          else
          {                     
            InsetBanners ('banners_'+Banners[i].possition, Banners[i].id);
            if (pos == 7 || pos == 8)
            {             
              var availableWidth = getWindowWidth(); 
              var perBanner = parseInt((availableWidth - mainTdWidth - 10) / 2);
              $('#banners_'+Banners[i].possition+'_container').css('width', perBanner); //.css('overflow', 'hidden');                           
              if (Banners[i].scroll == 1) { scroll_baners.push(pos); }                            
            }            
           }
           
          i = 0;
         }
         
        exists = false;          
   }   
 } 
 
 if (exists)
 {
   //--- verificam daca categoria data are banere daca nare punem google ---   
     if (pos == 1 || pos == 2 || pos == 3)
      InsetBannersGoogle ('banners_'+pos, 'google_'+pos, pos);
     else      
      InsetBanners ('banners_'+pos, 'google_'+pos);
 }     

 window.onscroll = function()
 {					    		  
   var scroll = getScrollY();	
   for (i=0; i<scroll_baners.length; i++)
   {	
    $('#banners_'+scroll_baners[i]).css('top', scroll + 20);
   }
 }
} 
//****end* Banners *************

function GetVideoByCategory(lang_code,map,thumbs_id, category_id, menuClick ) /* -- ID categorie sau subcategorie */
{ 
  if (menuClick != undefined) clickedFromMenu = menuClick;
  
  var d = new Date();
  /* --- Banners ------------ */  
  if (map == 'categories')
  {       
     $.get('/banners/'+category_id+'.js?id='+d.getTime(), '', 
     function (data) 
     {       
       eval(data);
       FindBanners(Banners,1);       
       FindBanners(Banners,2);       
       FindBanners(Banners,3);
       FindBanners(Banners,4);
       FindBanners(Banners,5);
       FindBanners(Banners,6);
       FindBanners(Banners,7);
       FindBanners(Banners,8);
       FindBanners(Banners,9);
       FindBanners(Banners,10);
     }
    ); 
  }  
  /* --- end - banners ------ */
  $.get('/'+lang_code+'/'+map+'/'+category_id+'/index.html?id='+d.getTime(),'', 
    function (data) 
    {
      $('#' + thumbs_id).html(data);
    }
  );  
}

function GetVideoByPage(lang_code,map,thumbs_id, category_id) /* -- ID categorie sau subcategorie */
{ 
  var d = new Date();
  /* --- Banners ------------ */      
     $.get('/banners/'+category_id+'.js?id='+d.getTime(), '', 
     function (data) 
     {
       eval(data);       
       FindBanners(Banners,1);       
       FindBanners(Banners,2);       
       FindBanners(Banners,3);
     }
    );  
  /* --- end - banners ------ */
  
  $.get('/'+lang_code+'/'+map+'/index.html?id='+d.getTime(),'', 
    function (data) 
    {
      $('#' + thumbs_id).html(data);
    }
  );  
}

function GetPageView(div_id, url, news_id) /* -- view video */
{ 
  var d = new Date();  
  $.get('/counts/'+news_id+'.html?id='+d.getTime(), '', 
   function (data) 
   {   
     document.getElementById(div_id).innerHTML= data;
   }
  );
}

function Loader (div_id)
{
 if (document.getElementById(div_id))
 {
   document.getElementById(div_id).innerHTML = '<div align="center"><img src="/img/site/loader.gif" alt="" /></div>';
 }
}

function getContentFromIframe(iFrameName)  
{  
 
}  

function OpenVideo ()
{
  var ancor = document.location.hash;  
  var video = ancor.substring(1,ancor.length);
  video = video.split("&");
  
  if (video[0] > 0)
  {
    var video_file = video[0]; 
    var fknews = video[2];
    var exml = video[3];     
    if (exml != '' && exml != null && exml != undefined)
	{
	   exml = 'http://' + gSiteURL + '/ro/events_xml/'+exml+'.xml';	   
	}
	
	 var pubURL = ""; 
     if (video[7] > 0) pubURL = "/banners/"+video[7]+".xml";
  
    
    SetVideo(
		fknews,
		'http://' + gImageURL + '/gallery_video/thumbs2_' + video_file + '.jpg', 
		'http://' + gVideoURL + '/gallery_video/' + video_file + '.mp4', 
		'00:00',
		exml,
		'',
		pubURL); //--- video in plaer
    if (video[1] > 0 && fknews > 0) //--- Parintii la video
    {      
      GetVideoByCategory('ro','news_block/'+video[1],'new_block', fknews);
    }   
  }
  else 
  {    
    SetVideoFV();
  }
  
  if (video[5] > 0) //--- categoria parinte din care face parte 
  {
    var fkparent = video[5]; 
    $("#newTabs").tabs().tabs('select', '#newTabs-'+fkparent);
  }
  
  if (video[6] > 0) //--- categoria din care face parte 
  {
    var fkcategory = video[6]; 
    $('#subspan-'+fkcategory).trigger('click');
  }  
}

function SelectByAnchor()
{
  var ancor = document.location.hash;  
  var video = ancor.substring(1,ancor.length);
  video = video.split("&");
  
  $("#thumbs_li_"+video[2]).trigger('highlight');
  
  if (!clickedFromMenu)
  {
    $("#thumbs_li_"+video[2]).trigger('click');
  }
  clickedFromMenu = false;
}

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]
  });
});

function SetLastCommentsSource(pURL)
{  
  var d = new Date();  
  var div_if  = document.getElementById("new_last_comment");
  if (div_if)
  {  
    div_if.innerHTML = '';    
    div_if.innerHTML = '<iframe src="" id="last_comments_iframe" name="last_comments_iframe" scrolling="auto" height="120px;" frameborder="0" width="100%" marginheight="0" marginwidth="0"  ></iframe>';    
  }
  
  var el1 = document.getElementById("last_comments_iframe");  
  if (el1)
  {   
     el1.src = pURL+'&id='+d.getTime(); 
  }
}
  
function SetCommentsSource(pURL, pTitle, pDate)
{
  var d = new Date();
  
  var div_if = document.getElementById("div_iframe");
  if (div_if)
  {
    div_if.innerHTML = '';    
    div_if.innerHTML = '<iframe src="" id="news_comments_iframe" name="news_comments_iframe" scrolling="auto" height="660px;" frameborder="0" width="100%" marginheight="0" marginwidth="0"  ></iframe>';
  }
  
  //<iframe id="news_comments_iframe" name="news_comments_iframe" scrolling="auto" height="600px;" frameborder="0" width="100%" marginheight="0" marginwidth="0"   src="http://{$forumURL}/vid2.html?id=3" ></iframe>
  var el1 = document.getElementById("news_comments_iframe");
  if (el1) el1.src = pURL+'&id='+d.getTime();
    
  var el2 = document.getElementById("news_comments_title");
  if (el2) el2.innerHTML = pTitle;
  
  var el3 = document.getElementById("news_comments_date");
  if (el3) el3.innerHTML = pDate;
}

function SetFeedbackSource(pURL)
{
  //alert(pURL);
  pURL = pURL + 'cms/?call=feedback'; //'http://md11.dqteam.com/cms/?call=feedback';
  var d = new Date();
  var el1 = document.getElementById("feedback_iframe");
  if (el1) el1.src = pURL+'&id='+d.getTime();
}

