/*
These functions are required because safari 1.3 is broken - once users upgrade much better methods could be employed
Orignally most of the functions below where carried out via an elegant CSS Class structure. Once users stop using old
Safari's then switch back to this CSS method as it would be far easier to maintain. FYI - IE6 also struggles when inherited classed are
use with input fields.
*/
function setTabToActive(newTab){
	/*if(newTab.id == 'creative_link'){
		newTab.style.backgroundImage = "url(/_img/pressassociation/activeCreativeTab.jpg)";
	}else{*/
		newTab.style.backgroundImage = "url(/_img/pressassociation/activeTab.jpg)";
	//}
	newTab.style.backgroundColor = "#646464"
	newTab.childNodes[0].style.color = "#ffffff";
	newTab.style.top="0px";
	newTab.style.borderBottomWidth="0px";
	newTab.style.zIndex = "5";

}
function setTabToInActive(actTab){
	actTab.style.backgroundImage = "url(/_img/pressassociation/inactiveTab.jpg)";
	actTab.style.backgroundColor = "#ffffff"
	actTab.childNodes[0].style.color = "#666666";
	actTab.style.top="0";
	actTab.style.borderBottomWidth="0px";	
	actTab.style.zIndex = "1";
}

function tabMouseOut(tab){
	if(tab.id != activeTab+"_link"){
		setTabToInActive(tab)
	}	
}

function tabMouseOver(tab){
	if(tab.id != activeTab+"_link"){

		tab.style.backgroundImage = "url(/_img/pressassociation/inactiveTabHover.jpg)";		
		tab.style.backgroundColor = "#ffffff"
		tab.childNodes[0].style.color = "#b02025";
		tab.style.top="0";
		tab.style.borderBottomWidth="0px";	
		tab.style.zIndex = "1";
	}
}


var _activeForm;
function changeTab(newId){
	document.frmHistory.hdnLastPage.value = newId
	rtnVal = true;
	
	showWait(newId)

	/*if(tabControl.tab[0]['creative'][0].lastClicked != ""){
		if(newId == activeTab){
			//YTD - when on the creative tab, having run a search then return to home page the slide show does not restart.
			//setTimeout("window.frames['pageFrame_creative'].startStopSS('on')",4000);
		}else{	
			if(newId=='creative'){
				try{
					window.frames["pageFrame_creative"].startStopSS('on');		
				}catch(e){}
					
			}else{
				if(tabControl.tab[0]['creative'][0].location != "notSet"){
					try{	
						window.frames["pageFrame_creative"].startStopSS('off');
					}catch(e){};
				}
			}
		}
	}*/
	
	
	if(arguments[2]){

		tabControl.tab[0][newId][0].lastClicked = new Date();
		tabControl.tab[0][newId][0].location = arguments[1].href;
		
		if(activeTab != newId){
			//change the actual tab style
			setTabToInActive(document.getElementById(activeTab+"_link"));
			setTabToActive(document.getElementById(newId+"_link"));	
				
			if(document.getElementById(_activeForm) != tabControl.tab[0][newId][0].formToDisplay){
				if (document.getElementById(_activeForm)){
					document.getElementById(_activeForm).style.display = 'none';
				}
			}
			
			document.getElementById(tabControl.tab[0][newId][0].formToDisplay).style.display = 'block';
			_activeForm = tabControl.tab[0][newId][0].formToDisplay;		
		}else{
			
		}
		
		switchFrames(activeTab, newId)
		
		activeTab = newId;
		
		if(br[0]=="msie"){
			setTimeout("resizePanes()",200);
		}else{
			resizePanes();
		}
	}else{

		if(activeTab != newId){
		
			//change the actual tab style
			setTabToInActive(document.getElementById(activeTab+"_link"));
			setTabToActive(document.getElementById(newId+"_link"));		
			
			//decide which form to display
			var formToDisplay;
				
			dateTimeNow = new Date();	
			
			rtnVal = true;
			newLocation = ""
			newLocation2 = ""
			if (arguments[1]){
				newLocation = arguments[1].href;	
			}
		
			
			if(tabControl.tab[0][newId][0].refreshInterval < 0){
				rtnVal = false;	
			}else if (tabControl.tab[0][newId][0].refreshInterval == 0){
				rtnVal = true;
			}else if (tabControl.tab[0][newId][0].refreshInterval > 0){

				debugMsg("alternative tab clicked")
				//rewrite this bit - chrome problem within here.
				jsConLocation = tabControl.tab[0][newId][0].location
				frameSrc = document.getElementById('pageFrame_'+newId).src;

				if (jsConLocation != "notSet"){
					refreshRequired = false;
					if(tabControl.tab[0][newId][0].lastClicked != ""){
						timeDiff = dateTimeNow.getTime() - tabControl.tab[0][newId][0].lastClicked.getTime();
						timeDiff = timeDiff/1000
						if (timeDiff > tabControl.tab[0][newId][0].refreshInterval){	
							//refresh required based on refreshInterval
							refreshRequired = true;
						}
					}
					debugMsg("newId: (refresh) " + refreshRequired + ":" + newLocation +"=="+ tabControl.tab[0][newId][0].location);
										
					if (newLocation == frameSrc){
						//is the tab currently set to the default defined by the href on the tab anchor
						if (!refreshRequired){
							rtnVal = false;
						}else{
							//its been clicked with the refreshInterval period and showing default page
						}
					}else{
						//the tab/iframe is not showing the default page.
						debugMsg("changeTab: Not default page")
						if (refreshRequired){

							newLocation2 = frameSrc; 
							rtnVal = false;
						}else{
							rtnVal = false;
						}

					}
				}else{
					//there is a value for location in json
					debugMsg("tabControl.tab[0][newId][0].location='notSet'");
				}

			}
			
			tabControl.tab[0][newId][0].lastClicked = dateTimeNow;
	
			if(document.getElementById(_activeForm) != tabControl.tab[0][newId][0].formToDisplay){
				if (document.getElementById(_activeForm)){
					document.getElementById(_activeForm).style.display = 'none';
				}
			}
			
			document.getElementById(tabControl.tab[0][newId][0].formToDisplay).style.display = 'block';
			_activeForm = tabControl.tab[0][newId][0].formToDisplay;
			
			switchFrames(activeTab, newId)
			
			if(rtnVal == false && newLocation2 != ""){
				debugMsg("about to refresh page")
				var f = document.getElementById('pageFrame_'+newId);
				f.contentWindow.location.reload(true);
			}
			
			if(arguments[1]){
				debugMsg(arguments[1].href)
				tabControl.tab[0][newId][0].location = arguments[1].href; //eval("pageFrame_"+newId+".location")	;
			}
			
			
			if(tabControl.tab[0][newId][0].location == "about: blank"){
				debugMsg("ERROR: location on iframe not working")
			}	
				
			activeTab = newId;
		if(br[0]=="msie"){
			setTimeout("resizePanes()",200);
		}else{
			resizePanes();
		}
			
		}else{
			debugMsg("activeForm equals newId");
			
			if(tabControl.tab[0][newId][0].refreshInterval >= 0){
				if(tabControl.tab[0][newId][0].location == "notSet"){
					rtnVal = false;
					tabControl.tab[0][newId][0].location = window.frames["pageFrame_"+activeTab].location;
				}else{
					rtnVal = true;
					tabControl.tab[0][newId][0].location = document.getElementById(activeTab+"_link").href;
				}
			}else{
				if((newId=='creative')||(newId=='collections')){
					rtnVal = true;
				}else{
					rtnVal = false;
				}
			}
			
			tabControl.tab[0][newId][0].lastClicked = new Date();			
			
			//document.getElementById('pageFrame_'+activeTab).src;	
			if(document.getElementById(_activeForm)){
				document.getElementById(_activeForm).style.display = 'none';
			};
			document.getElementById(tabControl.tab[0][newId][0].formToDisplay).style.display = 'block';
			_activeForm = tabControl.tab[0][newId][0].formToDisplay;
			

			
			switchFrames('', newId);
			if(br[0]=="msie"){
				setTimeout("resizePanes()",200);
			}else{
				resizePanes();
			}
		}
	}
	debugMsg("changeTab returned: " + rtnVal);
	if (rtnVal == false){
 		hideWait(newId);
	}
	
	if(_activeForm == 'searchFormDiv'){
		
		setCurrentOrderByText();
	}	
	
	//This section has been added for safari and chrome
	if(rtnVal == true){
	
		//document.getElementById("pageFrame_"+activeTab).src=tabControl.tab[0][newId][0].location;
		debugMsg("json location: pageFrame_"+activeTab +" : " +tabControl.tab[0][newId][0].location);
		window.frames["pageFrame_"+activeTab].location = tabControl.tab[0][newId][0].location;
		
	}
	
	if(br[0]=="msie"){
		if(br[3]<=6){
			setTimeout("resizePanes()",200);
		}
	}
	
	
	if(newId=='results'){
		return true;	
	}else{
		return false;
	}

}


