var div_name = "coverad"
var url = "http://www.coveradmedia.com/GetCoverAd.php";
//var jqueryUrl = "http://www.coveradmedia.com/";

var div_height = 50;

var timeout = 1200;
var closetimer = 0;
var rnr_sebesseg_show = 400;
var rnr_sebesseg_hide = 400;

var coverad_jq;
var newAD = 0;

savePageMeta();

if (typeof jQuery == 'undefined'){ // ellenorzi, hogy az oldal hasznal-e jquery-t
  document.write('<script type="text/javascript" src="http://www.coveradmedia.com/jquery.js"></script>');
}


document.write('<link rel="stylesheet" type="text/css" href="http://www.coveradmedia.com/ad.css">');
//for (i=0;i<document.images.length;i++){
//alert(document.images.length);
for (i=0;i<300;i++){
       document.write('<div class="boxcaption" onmouseover="mcancelclosetime();" onmouseout="mclosetime('+i+')" id="'+div_name+i+'" style="position:absolute;width:0px; height:0px;top:0px;left:0px;color:#fff;display:none;border-style:solid;border-bottom-width:2px;border-top-width:0px;border-left-width:0px;border-right-width:0px;border-color:#000;z-index:1000;"></div>');  
}
document.write('<div id=coverad_temp style="top:0;left:0;position:absolute;width:50px;height:50px;color:#ffffff;display:none;"></div>');

function coverad_init2(){
alert('coverad');
}

function coverad_init(){
//  coverad_jq = jQuery.noConflict();
  
  coverad_jq = jQuery;
   
  for (i=0;i<document.images.length;i++){
       document.images[i].id = "pic"+i;
       document.images[i].par = document.images[i].id;
       if (document.getElementById(document.images[i].par).width>210){
         if (document.getElementById(document.images[i].par).height>119){
           document.getElementById("pic"+i).onmouseover= new Function('adshow(this.par,'+i+');');
	   document.getElementById("pic"+i).onmouseout= new Function('mclosetime('+i+');');  
	 }
       }
  }
}



function adshow(pic_element,num){
  
  for (i=0;i<document.images.length;i++){

   if (i!=num) {
            coverad_jq("#coverad"+i).stop().animate({height:'0px'},400,function(){
	       coverad_jq(this).attr('innerHTML','');
	       //$(this).arrr('display','none');
	     });
     }  
       
  }
  
  var bordTop = coverad_jq('#'+pic_element).css('border-top-width');
 //  alert(pic_element);
  if (bordTop=='medium'){
    bordTop = "0px";
  }
  
  x = bordTop.split('px');
  z = x[0];
  bordTop = z;
  
  var bordLeft = coverad_jq('#'+pic_element).css('border-left-width');
  if (bordLeft=='medium'){
    bordLeft = "0px";
  }
  x = bordLeft.split('px');
  z = x[0];
  bordLeft = z;
//  alert(bordLeft);
  
  var imgPadding = coverad_jq('#'+pic_element).css('paddingTop');
//  if (imgPadding=='medium'){
//    bordLeft = "0px";
//  }
  x = imgPadding.split('px');
  z = x[0];
  imgPadding = z;
  
  
  dleft = findPosLeft(document.getElementById(pic_element));
  dtop = findPosTop(document.getElementById(pic_element));

//  alert('1:'+num);
   
   var imgpos = coverad_jq("#pic"+num).offset();
//   var imgpos = coverad_jq("#pic"+num).position();


      //   alert(imgPadding);
    document.getElementById(div_name+num).style.top = parseInt(bordTop)+imgpos.top+"px";
    document.getElementById(div_name+num).style.left =parseInt(bordLeft)+imgpos.left+"px";
  

  mcancelclosetime();

  document.getElementById(div_name+num).style.width = (document.getElementById(pic_element).width)+"px";
  
//  alert(coverad_jq('#'+pic_element).css('border-left-width'));
  
//  alert(document.getElementById(pic_element).style.paddingTop);
//  document.getElementById(div_name+num).style.width= coverad_jq("#"+pic_element).width();
//  alert(document.getElementById(div_name+num).style.width);

  getPicStatus(document.getElementById(pic_element).src,num);
//  alert(document.getElementById('coverad_temp').innerHTML);
//    alert(document.getElementById(pic_element).src);

//    document.getElementById('coverad'+num).innerHTML='Loading...';
//    xss_ajax(url+'?'+timeSt()+'&num='+num+'&kep_width='+document.getElementById(pic_element).width+'&kep_height='+document.getElementById(pic_element).height+'&kep_src='+document.getElementById(pic_element).src+'&kep_alt='+document.getElementById(pic_element).alt);
//    document.getElementById('coverad'+num).style.display='inline';
//    $("#coverad"+num).stop().animate({height:div_height+'px'},{queue:false,duration:rnr_sebesseg_show});
}


function callback(div_name,data) {    
//   alert(data);
   cucc = document.getElementById(div_name);
   //alert(cucc);
   document.getElementById(div_name).innerHTML = data; 
 
}


function callback_pic_status(data,num) {
  
   document.getElementById('coverad_temp').innerHTML = data;
   var pic_element = document.images[num];
//   alert(data);
//   alert(pic_element);
//alert(document.images[num]);
    if (data !='1'){
      if (document.getElementById('coverad'+num).innerHTML==""){
        document.getElementById('coverad'+num).innerHTML='Loading...';
//        alert(pic_element.alt);
//alert(newAD);
        xss_ajax(url+'?'+timeSt()+'&num='+num+'&kep_width='+pic_element.width+'&kep_height='+pic_element.height+'&kep_src='+pic_element.src+'&kep_alt='+pic_element.alt);
        document.getElementById('coverad'+num).style.display='inline';
      }
      coverad_jq("#coverad"+num).stop().animate({height:div_height+'px'},{queue:false,duration:rnr_sebesseg_show});
    }
}

function getImgBorder(str){

var z = 0;
  t = str.split(' ');
  for (i=0;i<t.length;i++){
    if (t[i].indexOf('px')!=-1){
      x = t[i].split('px');
      z = x[0];
    }
  }
  return z;
}



function metaKeywords() { 
  var key="";
  metaCollection = document.getElementsByTagName('meta'); 
  for (i=0;i<metaCollection.length;i++) { 
     nameAttribute = metaCollection[i].name.search(/keywords/);
     if (nameAttribute!= -1) { 
       key = metaCollection[i].content; 
     } 
  }
  return key;
}

function metaDescription() { 
  var key="";
  metaCollection = document.getElementsByTagName('meta'); 
  for (i=0;i<metaCollection.length;i++) { 
     nameAttribute = metaCollection[i].name.search(/description/);
     if (nameAttribute!= -1) { 
       key = metaCollection[i].content; 
     } 
  }
  return key;
}

function metaContent() { 
  var key="";
  metaCollection = document.getElementsByTagName('meta'); 
  for (i=0;i<metaCollection.length;i++) { 
     nameAttribute = metaCollection[i].name.search(/content/);
     if (nameAttribute!= -1) { 
       key = metaCollection[i].content; 
     } 
  }
  return key;
}


function adhide(num){
    coverad_jq("#coverad"+num).stop().animate({height:'0px'},400,function(){
      document.getElementById('coverad'+num).style.display='none';
      document.getElementById('coverad'+num).innerHTML='';
    });

}

function xss_ajax(url) {
     var script_id = null;
     var script = document.createElement('script');
     script.setAttribute('type', 'text/javascript');
     script.setAttribute('src', url);
     script.setAttribute('id', 'script_id');
     script_id = document.getElementById('script_id');
     if(script_id){
       document.getElementsByTagName('head')[0].removeChild(script_id);       
     }
     document.getElementsByTagName('head')[0].appendChild(script);
}

function findPosLeft(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
  	  do {
	    curleft += obj.offsetLeft;
	  } while (obj = obj.offsetParent);
        }
	return curleft;
}

function findPosTop(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
  	  do {
	    curtop += obj.offsetTop;
	  } while (obj = obj.offsetParent);
        }
	return curtop;
}


function writeDIV(div_name){
  document.write('<div onmouseover="mcancelclosetime();" onmouseout="mclosetime()" id="'+div_name+'" style="position:absolute;width:300px; height:'+div_height+'px; display:none; top:0px; left:0px;"></div>');
}


function timeSt(){
  return parseInt(new Date().getTime().toString().substring(1,10));
}


function mclosetime(num){
  closetimer = window.setTimeout(function(){
//    $("#coverad"+num).hide(1);
//    document.getElementById('coverad'+num).style.display='none';
    //$("#coverad"+num).stop().animate({height:'0px'},{queue:false,duration:rnr_sebesseg_hide});
  adhide(num);       
  }, timeout);
}

function mcancelclosetime(){
  if(closetimer){
     window.clearTimeout(closetimer);
     closetimer = null;
  }
}

function getPicStatus(src,num){
  var script_id = null;
  var picurl = "http://www.coveradmedia.com/inc/tartalom_getpicstatus.php?"+timeSt()+"&kep_src="+src+"&num="+num;
     var script = document.createElement('script');
     script.setAttribute('type', 'text/javascript');
     script.setAttribute('src', picurl);
     script.setAttribute('id', 'pic_script_id');
     script_id = document.getElementById('pic_script_id');
     if(script_id){
       document.getElementsByTagName('head')[0].removeChild(script_id);       
     }
     document.getElementsByTagName('head')[0].appendChild(script);
}

function savePageMeta(){
  var loc = document.location.href;
  var loc2 = loc.replace('&','ANDANDAND');
  loc = loc2;

  tit = document.title;
  key =  metaKeywords();
  des = metaDescription();
  con = metaContent();

  var script_id = null;
  var saveurl = "http://www.coveradmedia.com/inc/tartalom_savemeta.inc.php?location="+loc+"&title="+tit+"&keywords="+key+"&description="+des+"&content="+con;

  var script = document.createElement('script');
  script.setAttribute('type', 'text/javascript');
  script.setAttribute('src', saveurl);
  script.setAttribute('id', 'save_script_id');
  script_id = document.getElementById('save_script_id');
  if(script_id){
    document.getElementsByTagName('head')[0].removeChild(script_id);       
  }
  document.getElementsByTagName('head')[0].appendChild(script);
}
