var inter;
var viewC;
var myInt;
var nCatview=0;
var yCatView;
var catView;
function inizialize(inVal,inCat){

	if(document.all)
		var f=document.all[inVal];
	else
		var f=document.getElementById(inVal);
	catView=inCat;
	f.style.display="block";
	f.style.left=0+'px';
	yCatView=inVal;
	checkScroll();
}

function apriPopUp(inValue,inTipo,inCod){
  switch(inValue){
  case 'contatti':
		if(inTipo!='')
  		str='?tipo='+inTipo+'&l='+inCod;
		else
			str='';
  	var r=window.open('contatti.php'+str,'contatti','width=550,height=300');
  	r.focus();
	break;
	case 'credits':
  	if(inTipo!='')
  		str='?tipo='+inTipo+'&l='+inCod;
		else
			str='';
    var r=window.open('credits.php'+str,'credits','width=400,height=200');
    r.focus();
	break;
	case 'info':
    var r=window.open('info.php#'+inTipo,'credits','width=400,height=250,scrollbars=yes');
    r.focus();
	break;
	case 'privacy':
		if(inTipo!='')
  		str='?tipo='+inTipo+'&l='+inCod;
		else
			str='';
    var r=window.open('privacy.php'+str,'privacy','width=500,height=350,scrollbars=yes');
    r.focus();
	break;
	case 'richiedi':
  	var r=window.open('richiedi.php','richiedi','width=400,height=350,scrollbars=no');
    r.focus();
	break;
  }
	return false;
}

function ordina(inValue,inValue1){
	var p=window.open('ordina_foto.php?f='+inValue+'&fid='+inValue1,'ordina','width=550,height=450,scrollbars');
	p.focus();
  return false;
}

function vota(inValue,inValue1,inValue2){
	var p=window.open('vota_foto.php?f='+inValue+'&tipo='+inValue1+'&fid='+inValue2,'vota','width=550,height=450,scrollbars');
	p.focus();
  return false;
}

function download(inValue,inValue1,inValue2){
	var p=window.open('download_foto.php?f='+inValue+'&eid='+inValue1+'&fid='+inValue2,'download','width=550,height=450,scrollbars');
	p.focus();
  return false;
}

function downloadFB(inValue,inValue1,inValue2){
	var p=window.open('download_fb.php?f='+inValue+'&eid='+inValue1+'&fid='+inValue2,'download','width=550,height=450,scrollbars');
	p.focus();
  return false;
}

function apriComing(inHome,inId){
  var p=window.open('coming_soon.php?tipo='+inHome+'&id='+inId,'coming','width=450,height=300,scrollbars');
	p.focus();
  return false;
}

function ascoltaAudio(inAudio){
  var p=window.open('ascolta_audio.php?file='+inAudio,'audio','width=200,height=80');
	p.focus();
  return false;
}

function guardaVideo(inVideo,inTipo){
  var p=window.open('video.php?file='+inVideo+'&ag='+inTipo,'video','width=400,height=400');
	p.focus();
  return false;
}

 function viewS(inValue){
  	resetCat(inValue);
   	//nascondo tutte le categorie
	//nascondo tutte le categorie
	var f=document.getElementById("contLCat");
	for(var e=0;e<f.childNodes.length;e++){
		tmpID=f.childNodes[e].id;
		if(tmpID)
			document.getElementById(tmpID).style.display="none";
	}
	//visualizzo la categoria interessate
	document.getElementById(inValue).style.display="block";

	//nascondo tutte le sotto categorie
	var a=document.getElementById("contListaCat");
	for(var e=0;e<a.childNodes.length;e++){
		tmpID=a.childNodes[e].id;
		if(tmpID)
			document.getElementById(tmpID).style.display="none";
	}
	yCatView=inValue;
	checkScroll();
	var tmpC=document.getElementById("L"+inValue);
	tmpC.className="sfYV";
}
function viewSU(inValue){
	clearTimeout(myInt);
	//nascondo tutte le sotto categorie
	var a=document.getElementById("contListaCat");
	for(var e=0;e<a.childNodes.length;e++){
		tmpID=a.childNodes[e].id;
		if(tmpID)
			document.getElementById(tmpID).style.display="none";
	}
	if(document.getElementById(inValue))
		document.getElementById(inValue).style.display="block";

	var posSC=document.getElementById("contListaCat");

	var a=getElementPosition("LC_"+inValue);

	var b=getPageCoords(document.getElementById("LC_"+inValue));


	tmpPOS=getTopPosition(document.getElementById("LC_"+inValue));
	tmpX=getLeftPosition(document.getElementById("LC_"+inValue));

	/*alert(a.left+" -> "+a.top);
	alert(b.x+" -> "+b.y);
	alert(tmpX+" -> "+tmpPOS);*/

	/*posSC.style.left=(tmpX-document.getElementById("LC_"+inValue).offsetWidth)+'px';
	posSC.style.top=(tmpPOS+27)+'px';*/
	posSC.style.left=(b.x)+'px';
	posSC.style.top=(b.y+27)+'px';
	return;

	sniffer = new CJL_BrowserSniffer();
	if( sniffer.isWin() ){
		//windows
		if( sniffer.isSafari() ){
			posSC.style.top=(tmpPOS.top+28)+'px';
			posSC.style.left=(tmpPOS.left-79)+'px';
		}
		else if( sniffer.isGecko() ){
			posSC.style.top=(tmpPOS.top+28)+'px';
			posSC.style.left=(tmpPOS.left-79)+'px';
		}
		else if( sniffer.isOpera() ){
			posSC.style.top=(tmpPOS.top+28)+'px';
			posSC.style.left=(tmpPOS.left-68)+'px';
		}
		else if( sniffer.isIE() ){
			posSC.style.top=(tmpPOS.top+28)+'px';
			posSC.style.left=(tmpPOS.left-75)+'px';
		}
		else{
			posSC.style.top=(tmpPOS.top+28)+'px';
			posSC.style.left=(tmpPOS.left-79)+'px';
		}
	}
	else{
		//mac
		if( sniffer.isSafari() ){
			posSC.style.top=(tmpPOS.top+28)+'px';
			posSC.style.left=(tmpPOS.left-79)+'px';
		}
		else if( sniffer.isGecko() ){
			posSC.style.top=(tmpPOS.top+28)+'px';
			posSC.style.left=(tmpPOS.left-329)+'px';
		}
		else if( sniffer.isOpera() ){
			posSC.style.top=(tmpPOS.top+28)+'px';
			posSC.style.left=(tmpPOS.left-79)+'px';
		}
		else if( sniffer.isIE() ){
			posSC.style.top=(tmpPOS.top+28)+'px';
			posSC.style.left=(tmpPOS.left-75)+'px';
		}
		else{
			posSC.style.top=(tmpPOS.top+28)+'px';
			posSC.style.left=(tmpPOS.left-79)+'px';
		}
	}

}

function hiS(){
	myInt=setTimeout("hideSub()",1000);
}

function hideSub(){
	//nascondo tutte le sotto categorie
	var a=document.getElementById("contListaCat");
	for(var e=0;e<a.childNodes.length;e++){
		tmpID=a.childNodes[e].id;
		if(tmpID)
			document.getElementById(tmpID).style.display="none";
	}
	clearTimeout(myInt);
}

function openCD(inType,inNum){
	var p=window.open('add_cd.php?type='+inType+'&cid='+inNum,'CC','width=500,height=450,scrollbars');
	p.focus();
  return false;
}

function openGallery(inType,inNum,inCod,inU,inC,inE){
	var p=window.open('agenziagallery.php?type='+inType+'&cid='+inNum+'&cod='+inCod+'&u='+inU+'&c='+inC+'&e='+inE,'CC','width=500,height=450,scrollbars');
	p.focus();
  return false;
}

function openSlide(inType,inNum,inCod,inU,inC,inE){
	var p=window.open('agenziaslide.php?type='+inType+'&cid='+inNum+'&cod='+inCod+'&u='+inU+'&c='+inC+'&e='+inE,'CC','width=500,height=450,scrollbars');
	p.focus();
  return false;
}

function opFG(op,num){
	if(document.all)
		var f=document.all["contFoto"+num];
	else
		var f=document.getElementById("contFoto"+num);
	if(op=='o')
		f.style.display="block";
	else if(op=='c')
		f.style.display="none";

	if(document.all){
  		var d=parent.document.all.contRight;
  		var d1=document.all.contGallery_1;
  		d.style.height=d1.offsetHeight+"px";
  	}
  	else{
  		var d=document.getElementById("contRight");
  		var d1=document.getElementById("contGallery_1");
  		d.style.height=d1.offsetHeight+"px";
  	}

	return false;
}

function setImgP(){
	if(document.all){
		var myDiv=document.all.big_img;
		var myFrame=document.all.contRight;
		var myPre=document.all.img_preload;
	}
	else{
		var myDiv=document.getElementById("big_img");
		var myFrame=document.getElementById("contRight");
		var myPre=document.getElementById("img_preload");
	}


    if(document.all){
    	var x=myFrame.offsetLeft-myPre.width-210;
    	//var y=document.body.scrollTop+myFrame.offsetTop
    	var y=document.body.scrollTop+222;
    }
    else{
    	var x=myFrame.offsetLeft-myPre.width-210;
		var y=myFrame.offsetTop+top.scrollY-50;
	}

    myDiv.style.width=myPre.width+90+"px";
	myDiv.style.height=myPre.height+90+"px";
	/*myDiv.style.width=10+"px";
	myDiv.style.height=10+"px";*/
	myDiv.style.border="";

    myDiv.style.left=x+"px";
    myDiv.style.top=y+"px";

    myDiv.style.display="block";


}

function setImgB(){
	if(document.all){
		var myDiv=document.all.big_img;
		var myFrame=document.all.contRight;
		var myImg=document.all.imgBig;
	}
	else{
		var myDiv=document.getElementById("big_img");
		var myFrame=document.getElementById("contRight");
		var myImg=document.getElementById("imgBig");
	}

	myImg.style.display="block";
    if(document.all){
    	var x=myFrame.offsetLeft-myImg.width;
		var y=document.body.scrollTop+222;
	}
    else{
    	var x=myFrame.offsetLeft-myImg.width;
    	var y=myFrame.offsetTop+top.scrollY;
    }

	myDiv.style.width=myImg.width+"px";
	myDiv.style.height=myImg.height+"px";
	myDiv.style.border="5px solid #FFFFFF";

    myDiv.style.left=x+"px";
    myDiv.style.top=y+"px";

    myDiv.style.display="block";



}

function view_big_img(inImg){
	if(document.all){
		var myDiv=document.all.big_img;
		var myFrame=document.all.clFoto;
		var myPreD=document.all.div_preload;
	}
	else{
		var myDiv=document.getElementById("big_img");
		var myFrame=document.getElementById("clFoto");
		var myPreD=document.getElementById("div_preload");
	}

	myPreD.style.display="block";
	myDiv.style.display="block";

	var myImg=document.createElement("img");
	myImg.onload=function(){
		if(document.all)
			var myPreD=document.all.div_preload;
		else
			var myPreD=document.getElementById("div_preload");
		myPreD.style.display="none";
		setImgB();
	}
	myImg.setAttribute("id","imgBig");
	myImg.src="resizeImage.php?file="+inImg+"&tipo=view_big";
	myImg.style.display="none";

	myDiv.appendChild(myImg);

	setImgP();

	return false;
}

function hide_big_img(){
	if(document.all){
		var myDiv=document.all.big_img;
		var myPreD=document.all.div_preload;
		var myImg=document.all.imgBig;
	}
	else{
		var myDiv=document.getElementById("big_img");
		var myPreD=document.getElementById("div_preload");
		var myImg=document.getElementById("imgBig");
	}

	myImg.onload="";
	myDiv.removeChild(myImg);
	myDiv.style.display="none";
	return false;
}

function getElementPosition(elemID) {
	var offsetTrail = document.getElementById(elemID);
	var offsetLeft = 0;
	var offsetTop = 0;

	while (offsetTrail) {
	//alert(offsetTrail.offsetLeft);
	//alert(offsetTrail+" --> "+offsetTrail.offsetParent+"  ->  "+offsetTrail.offsetLeft);
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}

	if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") {
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}

	return {left:offsetLeft, top:offsetTop};
}

function getPageCoords (element) { // thanks to 'Martin Honnen' for this idea
	var myElement, coords = {x: 0, y: 0};
    // prendo lo scroll del body
    myElement = element; // salvo l'elemento: mi riserve dopo
    while (element) {
        coords.x -= element.scrollLeft;
        coords.y -= element.scrollTop;
        element = element.offsetParent;
    }
    element = myElement; // l'avevo salvato
    // ora prendo l'offset dell'elemento
    while (element) {
        coords.x += element.offsetLeft;
        coords.y += element.offsetTop;
        element = element.offsetParent;
    }
    // ritorno le coordinate dell'angolo in alto a sx
    // dell'elemento considerate rispetto all'angolo
    // in alto a sinistra della finestra visibile
    return coords;
}

function getTopPosition(obj) {
    var topValue = 0;
    while (obj) {
        topValue += obj.offsetTop;
        var objParent = obj.parentNode;
        if (obj.offsetParent) {
            while (objParent != obj.offsetParent) {
                if (objParent.scrollTop)
                    topValue -= objParent.scrollTop;
                objParent = objParent.parentNode;
            }
        }
        obj = obj.offsetParent;
    }
    return topValue;
}

function getLeftPosition(obj) {
    var leftValue = 0;
    while (obj) {
        leftValue += obj.offsetLeft;
        var objParent = obj.parentNode;
        if (obj.offsetParent) {
            while (objParent != obj.offsetParent) {
                if (objParent.scrollLeft)
                    leftValue -= objParent.scrollLeft;
                objParent = objParent.parentNode;
            }
        }
        obj = obj.offsetParent;
    }
    return leftValue;
}

function prevCat(){
	hideSub();
	enableLeft();
	var lCat=document.getElementById(yCatView);

	tmpN=0;
	for(var e=0;e<lCat.rows[0].cells.length;e++){
		tmpN+=lCat.rows[0].cells[e].offsetWidth;
	}

	num=lCat.rows[0].cells[nCatview].offsetWidth;

	if(parseInt(lCat.style.left.replace("px",""))+parseInt(lCat.rows[0].offsetWidth)<=700 && !isNaN(parseInt(lCat.style.left))){
		blockRight();
		return;
	}
	lCat.style.left=(lCat.style.left.replace("px","")-num)+'px';
	nCatview=nCatview+1;
}

function nextCat(){
	hideSub();
	enableRight();

	var lCat=document.getElementById(yCatView);
	if(nCatview==0){
		blockLeft();
		return;
	}

	num=lCat.rows[0].cells[nCatview].offsetWidth;

	lCat.style.left=(parseInt(lCat.style.left.replace("px",""))+parseInt(num))+'px';
	nCatview=nCatview-1;
}

function resetCat(inVal){
	nCatview=1;
	var lCat=document.getElementById(inVal);
	lCat.style.left=0+'px';
	return false;
}

function blockLeft(){
	var lCat=document.getElementById(yCatView);
	lCat.style.left=0+'px';
	var btnL=document.getElementById("btLEFT");
	//btnL.onclick=null;
	//btnL.style.cursor="default";
	btnL.style.display="none";
}

function enableLeft(){
	var btnL=document.getElementById("btLEFT");
	//btnL.onclick=nextCat();
	//btnL.style.cursor="hand";
	btnL.style.display="block";
}

function blockRight(){
	var btnR=document.getElementById("btRIGHT");
	//btnR.onclick=null;
	//btnR.style.cursor="default";
	btnR.style.display="none";
}

function enableRight(){
	var btnR=document.getElementById("btRIGHT");
	//btnR.onclick=prevCat();
	//btnR.style.cursor="hand";
	btnR.style.display="block";
}

function checkScroll(){
	var lCat=document.getElementById(yCatView);

	tmpN=0;
	numC=0;
	fatto=false;
	for(var e=0;e<lCat.rows[0].cells.length;e++){
		if(catView!=lCat.rows[0].cells[e].id && !fatto && catView){
			numC+=lCat.rows[0].cells[e].offsetWidth;
			if(parseInt(lCat.style.left.replace("px",""))+parseInt(numC)>=700 && !isNaN(parseInt(lCat.style.left))){
				blockRight();
				fatto=true;
			}
			nCatview=nCatview+1;
		}
		else
			fatto=true;
		tmpN+=lCat.rows[0].cells[e].offsetWidth;
	}

	posL=parseInt(lCat.style.left.replace("px",""));
	wid=parseInt(lCat.rows[0].offsetWidth);

	if((posL+wid)>=700 || isNaN((posL+wid))){
		enableRight();
		if(numC>0)
			enableLeft();
		else
			blockLeft();

		//posiziona la categoria selezionata
		lCat.style.left=(parseInt(lCat.style.left.replace("px",""))-parseInt(numC))+'px';
	}
	else{
		blockRight();
		blockLeft();
	}
}

function CJL_BrowserSniffer(){
   var ua = navigator.userAgent;
   //alert(ua);

   this.isOpera = function(){
      return /Opera/.test(ua);
   }

   this.isSafari = function(){
      return /Safari/.test(ua);
   }

   this.isGecko = function(){
      return navigator.product == "Gecko" &&
	     ! ( this.isOpera() || this.isSafari() );
   }

   this.isIE = function(){
      return /MSIE/.test(ua);
   }

   this.isWin = function(){
      return /Windows/.test(ua);
   }

   this.isMac = function(){
      return /Mac/.test(ua);
   }
}
