/**
* 15.04.08@FC	1.0.4	FIX: try to apply MIE filter on tab's png with cached images at first time.
* 26.03.08@FC	1.0.3	FIX: release tab lock before execute open() by idItem correspondency.
* 20.03.08@FC	1.0.2	FIX: checkInnerImage method using w3gMedia AJAX interface in order to avoid loading asyncronism during rendering of IE filter fix 
* 12.03.08@FC	1.0.1	FIX: IE v7 also need png fix
* 12.03.08@FC	1.0.1	FIX: Tabs locked until DOM completely load
* 05.03.08@FC	1.0.0	First release.
*
*/
var w3gTab = function (idVal,parameter){
	 
	var id = idVal;   
	var idContent; 
	var idLabel; 
   	var animationLock;
   	var tabLock;   
	var IE;	
	var openDuration = 0.2;
	var closeDuration = 0.5;	
	var watchIdItem; 
	
	var initialized = false; 	true; //12.03.08@FC FIX tabs locked until DOM completely load
	 
	//get raw id of the instance
	this.id = function(){return id;}
	 
	/** Initialize object instance on window onload event **/
   	this.create=function(){	
   	  idContent =id+"-content"; 
	  idLabel=  id+"-label"; 
	  idImage=  id+"-image";
	  tabLock = false;	
      var fixPNG = true;		
	  if(parameter){
		  if(typeof(parameter['fixPNG'])!='undefined' && parameter['fixPNG']==false)
				fixPNG = false;
			if(parameter['start'] && parameter['start']=='open')
				this.open();
			if(parameter['start'] && parameter['start']=='close')
				this.close();
			if(parameter['start'] && parameter['start']=='toggle')
				this.toggle();
			if(parameter['effectsDuration']){
				closeDuration=parameter['effectsDuration'];
				openDuration=parameter['effectsDuration'];
			}else{
				if(parameter['openDuration'])
					openDuration=parameter['openDuration'];
				if(parameter['closeDuration'])
					closeDuration=parameter['closeDuration'];
			}
			tabLock = parameter['lock']
		}
		if(fixPNG)checkInnerImage();
		initialized = true;
		if(w3gItemAndSezione){
			var anchor = new w3gDOM.Inspector($(idContent)).searchIn("A")[0];
			iditem = anchor.href;
			if(iditem.indexOf('?')>0){
			 	iditem = iditem.substr(iditem.indexOf('?'),iditem.length);
				if(iditem.indexOf('idItem=')>0){
				  iditem = iditem.substr(iditem.indexOf('idItem='),iditem.length);
				  iditem = iditem.substr(0,iditem.indexOf('&')>0?iditem.indexOf('&'):iditem.length);
				  iditem = iditem.split('=')[1];		  
				}
			}
			if(!isNaN(Number(iditem))){
				watchIdItem =  iditem;
				//*@eg  w3gItemAndSezione="&idItem=20296&idSezione=20295&idLanguage=0&idDevice=null"		
	   			iditem = w3gItemAndSezione;
	   			if(iditem.indexOf('idItem=')>0){
				  iditem = iditem.substr(iditem.indexOf('idItem='),iditem.length);
				  iditem = iditem.substr(0,iditem.indexOf('&')>0?iditem.indexOf('&'):iditem.length);
				  iditem = iditem.split('=')[1];		  
				}
				if(watchIdItem == iditem){					
					this.unlock();
					this.open();
					this.lock();
				}
	   		}
   		}
   		path =window.location.pathname   		
		if(path.indexOf('home.do')==path.length-'home.do'.length){
			this.unlock();
			this.open();
			this.lock();
		}		
   }
	// @PRIVATE lock animation performs
   var obtainLock = function(){
     animationLock=true;
   }
   // @PRIVATE unlock animation performs
   var releaseLock = function(){
     animationLock=false;
   }
   // show the content
   this.open= function(){
      if(initialized && !tabLock && !this.isOpen() && !animationLock){
        try{
			new Effect.Appear(idContent,
          		{duration:openDuration,
          		beforeStart:obtainLock,
          		afterFinish:releaseLock});
        }catch(err){
         $(idContent).style.display='block';
        }
      }
   }   
   // hide the content
   this.close=function(){ 
      if(initialized && !tabLock && this.isOpen() && !animationLock){
        try{
	        new Effect.Fade(idContent,
	          {duration:closeDuration,
	          beforeStart:obtainLock,
	          afterFinish:releaseLock});
	    }catch(err){
         $(idContent).style.display='none';
        }
      
      }
   }
   // toggle content visibility
   this.toggle= function(){
    if(!initialized)return;
    if (!this.isOpen()) this.open();
    else this.close();    
   } 
   
   //lock\unlock the tab
   this.lock=function(){tabLock=true;}
   this.unlock=function(){tabLock=false;}
   
   //tell if opened
   this.isOpen=function(){
  	if(initialized)
    	return ($(idContent).style.display != 'none');
    else
    	null
   } 
   //@PRIVATE Check foe PNG in IE
   function checkInnerImage(){
   	 IE=false;
	 if(/MSIE/.test(navigator.userAgent)){		
			//var version = parseFloat(navigator.appVersion.split("MSIE")[1])				
			/*12.03.2007@FC FIX IE7, png fix required*/
			 if (document.body.filters){	
				IE=true;
				var images = new w3gDOM.Inspector($(idContent)).searchIn("IMG");
					for(var i = 0; i<images.length;i++){	
						var image = images[i];		
						//Fist cached image processing...				    
						if( !MIEFixPng(image) && /media\/show\?/g.test(image.src)){											    
							var idMedia = image.src.substr(image.src.indexOf('?')+(('?').length));
							var media = new w3gMedia(idMedia);
							//Repository Ajax load....
							media.load(function(){MIEFixPng(image,media)});
							//flow breaking! jump to MIEFixPng
						}else{ //brutal attemp to fix;
							MIEFixPng(image);	
						}				
					}
			}
		}
   }
   //@PRIVATE FIX PNG trasparency on IE
  	 function MIEFixPng(image,media){
  	    var h=0;
  	    var w=0;
  	    var png=false;  	  
  	    var src = image.src;
  	    if(!media){//brutal attemp to fix;    
  	    	png=true; //forcing to png process
  	    	var img = new Image();
			//swap attemp
			img.src=src;	
			if(img.height>0 && img.width>0){
			  h = img.height;
			  w = img.width;			  	
			 }  	    
		}else{//AJAX repository info call			
			if(media.isImage() && /png/g.test(media.getContentType())){
				png= true;
				if(media.getImageWidth()!=0 && media.getImageHeight()!=0){
					w =media.getImageWidth();
					h =media.getImageHeight();
				}
			}
		}		
		// filter fix DXImageTransform.Microsoft.AlphaImageLoader
		if(png==true && h>0 && w>0){
			image.outerHTML = '<div id="'+idImage+'" style="cursor:pointer;width:'+w+'px;height:'+h+'px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+src+',sizingMethod=\'scale\');"></div>';				 
		}else
			return false;
		return true;
	}
	//put the instance in tabs collection
	w3gTab.Collection[idVal]=this;	
	//prepare the initialization for document onload event	
	onloadAddFunction(function(){w3gTab.Collection[id].create();});				
}
//STATIC@ Collect all the instance of w3gTab
w3gTab.Collection = new Hash();

//STATIC METHODS@-----------------------------------------------------------------------
/*check if external js object are loaded, otherwise if dontInclude Parameter 
 equal false or isn't present, include required *.js file using w3g.js function 
 or myIncludeJs internal function if non aviable */
w3gTab.CheckDependency = function(dontInclude){
	//internal include function
	function myIncludeJs(filename)
	 {
	 	var scriptElt = document.createElement('script');
	 	scriptElt.type = 'text/javascript';
		scriptElt.src = "/"+window.w3gContex+"/"+filename;
		//append on HEAD section
	 	document.getElementsByTagName('head')[0].appendChild(scriptElt); 
	 }	 
	if(typeof(Scriptaculous)=='undefined'){
		if(!dontInclude)
		 	try{
		 		w3gIncludeJs("js/scriptaculous/scriptaculous.js");
		 	}catch(err){
		 		myIncludeJs("js/scriptaculous/scriptaculous.js")
		 	}
		else
			throw 'w3gTab object not successfully loaded, dependency obj [Scriptaculous] not found';
	}
	if(typeof(w3gDOM)=='undefined'){
		if(!dontInclude)
		 	try{
		 		w3gIncludeJs("js/obj/w3gDOM.js");
		 	}catch(err){
		 		myIncludeJs("js/obj/w3gDOM.js")
		 	}
		else
			throw 'w3gTab object not successfully loaded, dependency obj [w3gDOM] not found';
	}
}
//Close al the instance of w3gTab
w3gTab.CloseAll = function(){
  var keyset = w3gTab.Collection.keys();
  for(var i=0 ; i<keyset.length;i++){
	  w3gTab.Collection[keyset[i]].close();
	}
}
//Toggle visibility all the instance of w3gTab
w3gTab.ToggleAll = function(){
  var keyset = w3gTab.Collection.keys();
  for(var i=0 ; i<keyset.length;i++){
	  w3gTab.Collection[keyset[i]].toggle();
	}
}
//Open all the instance of w3gTab
w3gTab.OpenAll = function(){
  var keyset = w3gTab.Collection.keys();
  for(var i=0 ; i<keyset.length;i++){
	  w3gTab.Collection[keyset[i]].open();
	}
}
//Lock all the instance of w3gTab
w3gTab.LockAll = function(){
  var keyset = w3gTab.Collection.keys();
  for(var i=0 ; i<keyset.length;i++){
	  w3gTab.Collection[keyset[i]].lock();
	}
}
//unLock all the instance of w3gTab
w3gTab.UnlockAll = function(){
  var keyset = w3gTab.Collection.keys();
  for(var i=0 ; i<keyset.length;i++){
	  w3gTab.Collection[keyset[i]].unlock();
	}
}
//Version of w3gTabs.js
w3gTab.Version= function(){
	var release ='1';
	var major='0';
	var minor='4';		
	this.release=release;this.major=major;this.minor=minor;	
	function fullVersion(){	return release+'.'+major+'.'+minor; }
	return fullVersion();
}
// check if this obj is aviable
w3gTab.CheckDependency();
