var d = new Date();
var videoURL = 'md16.dqteam.com';
 
function loadHtmlBlock(targetId, blockUrl) 
{		
	$.get(blockUrl + '?' + d.getTime(), function(data) 
	{	
		$('#' + targetId).html(data);
	});
}

function loadHtmlBlockConfig(targetId, blockUrl, id) 
{
	$.get(blockUrl + 'index_16.html' + '?' + d.getTime(), function(data) 
	{
		$('#' + targetId).html(data);
	});
}

function SelectTopMenu (idDiv)
{	
	var obj = $().htmlMenu();		
	setTimeout(function() {obj.SelMenu(idDiv);}, 500);	
}

function loadHtmlCount (idDiv, idNews, src)
{
	$.get('/counts/'+src+'.js?id='+d.getTime(), '', 
	   function (data) 
	   {       
		 var NewsCounts = eval(data);       
		 FindNewsCount(idDiv,NewsCounts,idNews);       
		});   
}

function FindNewsCount(idDiv,NewsCounts,idNews)
{
	for (var i=0; i<NewsCounts.length; i++)
	{
   	  if (NewsCounts[i].i == idNews)
   	  {
   		$('#' + idDiv).html(NewsCounts[i].c);
   		break;
   	  }   		  
	}
}

$(window).resize(function() 
{		
	ResizePromo("#CategoryPromo");	
});

function ResizePromo(id)
{
	var obj = $(id);	
	var pos = $("#HtmlMenuTop").position();
	
	var availableWidth = getWindowWidth();
	var perBanner = parseInt((availableWidth - mainTdWidth) / 2);
	var px = parseInt(perBanner + 3);
	
	var cssObj = {
		      'position'  : 'absolute',
		      'padding'   : '5px',
		      'top'       : pos.top + 70 +'px',
		      'float'     :'right',
		      'right'     : px + 'px'
		    };
	
	obj.css(cssObj);
	
	return obj;
}

function InserCategoryPromo(video)
{	
	var obj = ResizePromo("#CategoryPromo");
	
	obj.html('<div id="PromoContent"></div>');
	
	var flashvars = {
         videoUrl  : "http://"+ videoURL +"/gallery_video/"+ video +".mp4",		  
	     imgUrl    : "http://"+ videoURL +"/gallery_video/thumbsJTE2_"+ video +".jpg",
	     newsUrl   : "",		     
	     bannersUrl: "",
	     bannersXml: "",		  
	     nextUpUrl : "",   
	     titlePage : ""	                 
	};
	var params = {
		menu: "false",
		scale: "noScale",
		allowFullscreen: "true",
		allowScriptAccess: "always",
		bgcolor: "#FFFFFF",
		wmode: "transparent" 
	};
	var attributes = { id:"DQVideoPlayerPromo" };
	if (swfobject.hasFlashPlayerVersion("10.0.0")) 
	{
		swfobject.embedSWF("/swf/DQJTVPlayer.swf", "PromoContent", "300", "176", "10.0.0", "/swf/expressInstall.swf", flashvars, params, attributes);
	}	
	
}



function GetGoogleStatsCounter()
{
  var d = new Date();
  $("#googlestats").load("/analytics/google_analytics.html?"+d.getTime());   
}


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);
}

//***** Banners *************
var mainTdWidth = 980;

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)
{  
  if (document.getElementById(div_id))
  {
    $.get('/banners/'+file+'.html?id='+d.getTime(),'', 
     function (data) 
     { 
         $('#' + div_id).html(data);    
         if (div_id == 'banners_4') ResizePromo("#CategoryPromo");
     }
    );  
  }  
}

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);
    });  
}

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;
    
 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);  
           
           if (pos == 7 || pos == 8)
           {
        	   var availableWidth = getWindowWidth();
        	   var perBanner = parseInt((availableWidth - mainTdWidth - 10) / 2);
        	   var px = parseInt(perBanner - 120 - 15);
        	   
        	   var cssObj = {
					      'overflow' : 'hidden',
					      'width'    : '120px',
					      'height'   : '600px',
					      'position' : 'fixed',
					      'top'      : '10px'
					    }; 
        	   $('#banners_'+Banners[i].possition).css(cssObj);
        	           	   
        	   if (pos == 7)
        		   $('#banners_'+Banners[i].possition).css({'float':'left','left': px + 'px'});
        	   
        	   if (pos == 8)
        		   $('#banners_'+Banners[i].possition).css({'float':'right','right': px + 'px'});
        	   
        	   if (Banners[i].scroll == 1)
        		 $('#banners_'+Banners[i].possition).css({'position':'fixed'});
        	                    
           }        
           
           i = 0;
         }     
   }   
 } 
 
} 

function GetBanners (pos, category)
{
   //alert(category);
   var jsfile = 'banners';   
   if (category && category > 0) jsfile = category;   
   $.get('/banners/'+jsfile+'.js?id='+d.getTime(), '', 
     function (data) 
     {       
       eval(data);       
       FindBanners(Banners,pos);       
     }
    ); 
}
//****end* Banners *************

//**** Comments ********
function SetCommentsSource(divID,fbXid)
{
   var div_if = document.getElementById(divID);
   if (div_if)
   {
      div_if.innerHTML = '';      
      div_if.innerHTML = '<div id="fb-root"></div> <fb:comments xid="'+fbXid+'"  numposts="10" width="500" ></fb:comments>';
                                                        
	  window.fbAsyncInit = function() 
      {
         FB.init({appId: '194899680540892', status: true, cookie: true, xfbml: true});
      };
     
      (function() {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
       }());
     
      /*       
       <script src="http://connect.facebook.net/en_US/all.js#appId=194899680540892&amp;xfbml=1"></script>
       <fb:comments href="example.com" num_posts="2" width="500"></fb:comments>
       */
       
   }   
}

//***** end Comments ********
