var voicesearch = 0;
var gender = 0;
var age = 0;
var mystyle = 0;
var subOpen = 'sub_voices';
var tabOpen = 'gender';
var castingengineon = 1;
var quickcaston = 0;
var foreignon = 0;
var sidepromoon = 0;
var subnavopen = 1;
var $page = 1;
var backlinkaction = '';
var openid = 1;

function no(){}

function bespoke(action){// Bespoke promobox actions
	if(action == 'vo2go'){
		filter('stud', 1, 'qs16')	
	}
}

function backlink(){
	var backlinkaction = getCookie('backlinkaction');
	
	if(backlinkaction=='radio'){
		ajaxTalentSearch('presenter','radio');
	}else if(backlinkaction=='television'){
		ajaxTalentSearch('presenter','television');
	}else if(backlinkaction=='newvoices'){
		newVoices(1);
	}else{
		try{ajaxTalentSearch('speaker',backlinkaction);}catch(err){}
	}
}

function tabopen(id){
	
	if(openid == id){return false;}
	
	// Close the open tab
	if(openid){
		var oldelementId = 'tab'+openid;
		var oldelm = document.getElementById(oldelementId);
		try{oldelm.className = 'tab_closed';}catch(err){}
		
		var oldcontent = 'tab_'+openid;
		var oldelm = document.getElementById(oldcontent);
		
		try{oldelm.style.display = 'none';}catch(err){}
	}
	openid = id;
	
	// Open the new tab
	var elementId = 'tab'+id;
	try{var elm = document.getElementById(elementId);
	elm.className = 'tab_open';}catch(err){}
	
	var tabcontentId = 'tab_'+id;
	var tabcontent = document.getElementById(tabcontentId);
	try{tabcontent.style.display = 'block';}catch(err){}
	
}

function changePWfield(){
	document.forms['logins'].pw.value='';
	document.getElementById('pwfield').innerHTML = '<input type="password" name="pw" id="pw" value=""/>';
	document.forms['logins'].pw.focus();
}


// Swithc on the main content panel
function contentPanelOn(){
	
	document.getElementById('headimage').style.display = 'block';
	
	// Turn off audio player
	try{document.getElementById('audioplayer').style.display = 'none';}catch(err){}
	
	// Main panel
	document.getElementById('main').style.backgroundImage = 'none';	
	document.getElementById('main').style.width = '785px';	
	
	
	// Content panel
	document.getElementById('contentpanel').style.backgroundImage = 'url(images/main_bgnd.gif)';
	document.getElementById('contentpanel').style.padding = '10px';
	document.getElementById('contentpanel').style.paddingLeft = '15px';
	document.getElementById('contentpanel').style.paddingTop = '0px';
	
	// Panel header image
	//document.getElementById('headimage').style.backgroundImage = 'block';	
	document.getElementById('headimage').src = 'images/main_header.gif';
	
	// Tabbed bars
	try{document.getElementById('tab_bar').style.display = 'none';}catch(err){}
	try{document.getElementById('tabcontent').style.display = 'none';}catch(err){}
	try{document.getElementById('tab_base').style.display = 'none';}catch(err){}

}


function resetMainPanel(){
	document.getElementById('headimage').src = 'images/main_header.gif';
	document.getElementById('contentpanel').style.backgroundImage = 'url(images/main_bgnd.gif)';
	document.getElementById('main').style.width = '785px';
	document.getElementById('main').style.backgroundImage = 'none';	
}

function subnav(index){
	
	if(index==1 && subOpen != 'sub_voices'){
		
		closeSearchResults();
		contentPanelOn();
		document.getElementById('contentinner').innerHTML = '<h1>Voices</h1><p>Click on one of the options above to start casting a voice over.</p>';
		try{document.getElementById('homepageanim').style.display = 'none';}catch(err){}
		
		resetsubnavigation();
		resetnavigation();
		resetbuttons();

		if(!subnavopen){
			$('sub_voices').appear({ duration: 1.4, from: 0, to: 1 });
		}else{
			$('sub_voices').appear({ duration: 0.3, from: 0, to: 1 });
		}
		subOpen = 'sub_voices';
		document.getElementById('b2').src = 'images/b_voices_2.gif';
		subNavOpen();
		
	}
	
	if(index==2 && subOpen != 'sub_presenters'){
		
		closeSearchResults();
		contentPanelOn();
		document.getElementById('contentinner').innerHTML = '<h1>Presenters</h1><p>Click on one of the options above to start casting a presenter.</p>';
		try{document.getElementById('homepageanim').style.display = 'none';}catch(err){}
		
		resetsubnavigation();
		resetnavigation();
		resetbuttons();
		sidePromos();
		
		if(!subnavopen){
			$('sub_presenters').appear({ duration: 1.4, from: 0, to: 1 });
		}else{
			$('sub_presenters').appear({ duration: 0.3, from: 0, to: 1 });
		}
		subOpen = 'sub_presenters';
		document.getElementById('b3').src = 'images/b_presenters_2.gif';
		subNavOpen();
	}
	
	if(index==3 && subOpen != 'sub_speakers'){
		
		closeSearchResults();
		contentPanelOn();
		document.getElementById('contentinner').innerHTML = '<h1>Speakers</h1><p>Click on one of the options above to start casting a speaker.</p>';
		try{document.getElementById('homepageanim').style.display = 'none';}catch(err){}
		
		resetsubnavigation();
		resetnavigation();
		resetbuttons();
		sidePromos();
		
		if(!subnavopen){
			$('sub_speakers').appear({ duration: 1.4, from: 0, to: 1 });
		}else{
			$('sub_speakers').appear({ duration: 0.3, from: 0, to: 1 });
		}
		subOpen = 'sub_speakers';
		document.getElementById('b4').src = 'images/b_speakers_2.gif';
		subNavOpen();
	}
	
	if(index==4 && subOpen != 'sub_about'){

		resetsubnavigation();
		resetnavigation();
		resetbuttons();
		
		if(!subnavopen){
			$('sub_speakers').appear({ duration: 1.4, from: 0, to: 1 });
		}else{
			$('sub_speakers').appear({ duration: 0.3, from: 0, to: 1 });
		}
		subOpen = 'sub_about';
		document.getElementById('b7').src = 'images/b_about_2.gif';
		subNavOpen();
	}
	
}

function resetbuttons(){
	document.getElementById('b1').src = 'images/b_home_1.gif';
	document.getElementById('b2').src = 'images/b_voices_1.gif';
	document.getElementById('b3').src = 'images/b_presenters_1.gif';
	document.getElementById('b4').src = 'images/b_speakers_1.gif';	
}

// Submit a general search
function submitsearch(formname){

	var value = document.forms[formname].terms.value;
	var action = document.forms[formname].action.value;
	
	if(value == '' || value == 'Name search...'){
		alert("Please enter all or part of the artiste's name");
		return;
	}
	
	showLoader();
	
	if(action == 'voice'){
		ajaxvoicesearch(0, 0, 0, 'name='+document.forms[formname].terms.value, 1);
	}
	if(action == 'presenter'){
		ajaxTalentSearch('presenter', 0, 1, document.forms[formname].terms.value)
	}
	if(action =='speaker'){
		ajaxTalentSearch('speaker', 0, 1, document.forms[formname].terms.value)
	}
	
}

// Submit a general search with pagination (called from the AJAX back-end)
function pagedSearch(value, action, page){
	
	showLoader();
	
	if(action == 'voice'){
		ajaxvoicesearch(0, 0, 0, 'name='+value, page);
	}
	
	if(action == 'presenter'){
		alert('presenter');
		//ajaxtalentsearch(0, 0, 0, 'name='+document.forms['searchpresenter'].terms.value, 1);
		ajaxTalentSearch('presenter', 0, 1, value, page)
	}
	
	if(action =='speaker'){
		//ajaxtalentsearch(0, 0, 0, 'name='+document.forms['searchspeaker'].terms.value, 1);
		ajaxTalentSearch('speaker', 0, 1, value, page)
	}
	
}

/* Voice tab changer */
function voicetabs(tab){
	if(tabOpen == tab){
		return false;
	}
	if(tab=='gender'){
		Effect.SlideDown('panel_gender', { duration: 0.2 });
		Effect.SlideUp('panel_age', { duration: 0.2 });
		Effect.SlideUp('panel_style', { duration: 0.2 });
		document.getElementById('voicetabimg').src = 'images/voice_tab_1.gif';
		//document.getElementById('voicesearch').style.height = '185px';
	}
	if(tab=='age'){
		Effect.SlideUp('panel_gender', { duration: 0.2 });
		Effect.SlideDown('panel_age', { duration: 0.2 });
		Effect.SlideUp('panel_style', { duration: 0.2 });
		document.getElementById('voicetabimg').src = 'images/voice_tab_2.gif';
		//document.getElementById('voicesearch').style.height = '185px';
	}
	if(tab=='style'){
		Effect.SlideUp('panel_gender', { duration: 0.2 });
		Effect.SlideUp('panel_age', { duration: 0.2 });
		Effect.SlideDown('panel_style', { duration: 0.2 });
		document.getElementById('voicetabimg').src = 'images/voice_tab_3.gif';
		//document.getElementById('voicesearch').style.height = '185px';
	}
	tabOpen = tab;
}



// Change buttons when opening a page
function openPage(page){
	if(page=='advice'){
		resetnavigation();
		//resetsubnavigation();
		document.getElementById('b5').src = 'images/b_advice_2.gif';
		//subNavClose()
	}
	if(page=='training'){
		resetnavigation();
		//resetsubnavigation();
		document.getElementById('b6').src = 'images/b_training_2.gif';
		//subNavClose()
	}
}

function resetnavigation(){
	document.getElementById('b2').src = 'images/b_voices_1.gif';
	document.getElementById('b3').src = 'images/b_presenters_1.gif';
	document.getElementById('b4').src = 'images/b_speakers_1.gif';
	document.getElementById('b5').src = 'images/b_advice_1.gif';
	document.getElementById('b6').src = 'images/b_training_1.gif';
	document.getElementById('b7').src = 'images/b_about_1.gif';
}

function resetsubnavigation(){
	if(subOpen){
		$(subOpen).fade({ duration: 0.2, from: 1, to: 0 })
		subOpen = 0;
	}
}


// Set the H1 tag content
function setTitle(title){
	document.getElementById('contentinner').innerHTML = '<h1>'+title+'</h1>';
	closeSearchResults()
}

function subNavClose(){
	if(subnavopen==0){return false;}
	subnavopen=0;
	$('nav').morph('height:36px;', { duration: 0.6 });	
}
function subNavOpen(){
	if(subnavopen==1){return false;}
	subnavopen=1;
	$('nav').morph('height:76px;', { duration: 0.4 });
}


// Toggle casting engine open/closed
function castingEngine(){
	
	contentPanelOn();
	
	if(quickcaston){
		Effect.SlideUp('quickcast', { duration: 0.4 });
		quickcaston = 0;
	}
	
	if(sidepromoon){
		Effect.SlideUp('sidepromos', { duration: 0.4 });
		sidepromoon = 0;
	}
	
	if(foreignon){
		Effect.SlideUp('foreignvoices', { duration: 0.4 });
		foreignon = 0;
	}
	
	if(!castingengineon){
		castingengineon = 1;
		
		Effect.SlideDown('castingengine', { duration: 0.4 });
		closeSearchResults();
	}
	
	document.getElementById('contentinner').innerHTML = '<h1>Casting Engine</h1><p>Select which Gender, Age and Style of voice you require in the CASTING ENGINE box on the right.</p>';
	
	voicetabs('gender');
	try{document.getElementById('homepageanim').style.display = 'none';}catch(err){}
	
}


// Toggle quickcast open/closed
function quickCast(){
	
	contentPanelOn();
	
	if(castingengineon){
		Effect.SlideUp('castingengine', { duration: 0.4 });
		castingengineon = 0;
	}
	
	if(sidepromoon){
		Effect.SlideUp('sidepromos', { duration: 0.4 });
		sidepromoon = 0;
	}
	
	if(foreignon){
		Effect.SlideUp('foreignvoices', { duration: 0.4 });
		foreignon = 0;
	}
	
	if(!quickcaston){
		quickcaston = 1;
		document.getElementById('contentinner').innerHTML = '<h1>Quick Cast</h1><p>Select a category from the panel on your right.</p>';
		Effect.SlideDown('quickcast', { duration: 0.4 });
		closeSearchResults();
	}
	
	try{document.getElementById('homepageanim').style.display = 'none';}catch(err){}
}


// Toggle foreign voices open/closed
function foreignVoices(){
	
	contentPanelOn();

	if(castingengineon){
		Effect.SlideUp('castingengine', { duration: 0.4 });
		castingengineon = 0;
	}
	
	if(quickcaston){
		Effect.SlideUp('quickcast', { duration: 0.4 });
		quickcaston = 0;
	}
	
	if(sidepromoon){
		Effect.SlideUp('sidepromos', { duration: 0.4 });
		sidepromoon = 0;
	}
	
	if(!foreignon){
		foreignon = 1;
		document.getElementById('contentinner').innerHTML = '<h1>Foreign Voices</h1><p>Select a nationality from the panel on your right.</p>';
		Effect.SlideDown('foreignvoices', { duration: 0.4 });
		closeSearchResults();
	}
	
	try{document.getElementById('homepageanim').style.display = 'none';}catch(err){}
}

// Toggle sidepromos open/closed
function sidePromos(){

	if(castingengineon){
		Effect.SlideUp('castingengine', { duration: 0.4 });
		castingengineon = 0;
	}
	
	if(quickcaston){
		Effect.SlideUp('quickcast', { duration: 0.4 });
		quickcaston = 0;
	}
	
	if(foreignon){
		Effect.SlideUp('foreignvoices', { duration: 0.4 });
		foreignon = 0;
	}
	
	if(!sidepromoon){
		sidepromoon = 1;
		Effect.SlideDown('sidepromos', { duration: 0.4 });
		closeSearchResults();
	}
	
}


function setAudioPlayerFile(file, id, reelname){	
	window.document.audiobj.SetVariable("file", file);
	window.document.audiobj.SetVariable("reelname", reelname);
}


function closeContentPanel(){
	document.getElementById('contentpanel').style.display = 'none';
	document.getElementById('main').style.backgroundImage = 'none';	
}



// DW functions
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());

}

function getCookie(c_name){

	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			var value = unescape(document.cookie.substring(c_start,c_end));
			return value;
		}
	}
	return "";
}

function deleteconfirm(del, msg){
	
	if(!msg){
		msg = 'Are you sure you want to delete this item?';
	}
	
	if (confirm(msg)){ 
		window.location=del;
	}
}

function loginconfirm(msg, del){
	if (confirm(msg)){ 
		window.location=del;
	}
}

function shortlistSaveConfirm(msg, save, clear){
	if (confirm(msg+" (Yes - Cancel = No)")){ 
		window.location=save;
	}else{
		window.location=clear;
	}
}

function launchYoutube(reel_id){
	if(!reel_id){return false;}
	showreelwindow = window.open('http://www.excellentvoice.co.uk/youtube.php?reel_id='+reel_id, 'showreelwindow',"menubar=0,resizable=0,width=500,height=400");
	showreelwindow.focus();
}

function launchYoutubeMainreel(id){
	if(!id){return false;}
	showreelwindow = window.open('http://www.excellentvoice.co.uk/youtube.php?id='+id, 'showreelwindow',"menubar=0,resizable=0,width=500,height=400");
	showreelwindow.focus();
}