// JavaScript Document

document.observe("dom:loaded", function() {
  $('google_q').observe('focus', searchbarfocus)
  $('google_q').observe('blur', searchbarblur)
  $('cse-search-box').observe('submit', function(event){searchbarvalidate(event)})
  initMenubars()
  initContactform()
  initMenuarrow()
});

var RollIt = {
    timeout : null,
    showPopup : function(e){
        clearTimeout(this.timeout);
        if($(e).style.display == 'none'){
				$$('table.mainsubmenu').each(function(el){el.hide()})
			   e.show()
        }
    },
    hidePopup : function(e){
        if($(e).style.display == 'none'){
            clearTimeout(this.timeout);
        }else{
            this.timeout = setTimeout(function(){e.hide()},200);
        }
    }    
}
function searchbarfocus(){
	if ($F('google_q').strip().toLowerCase()=="search") {
		$('google_q').value=""
	}
	$('google_q').setStyle({color: '#FFFFFF'})
}
function searchbarblur(){
	$('google_q').value=$F('google_q').strip();
	if (($F('google_q').blank())||($F('google_q').toLowerCase()=="search")) {
		$('google_q').value="Search"
	}
	$('google_q').morph({color: '#A3937B'})
}
function searchbarvalidate(e){
	$('google_q').value=$F('google_q').strip();
	if (($F('google_q').toLowerCase()=="search")||($F('google_q').blank())){
		Event.stop(e)
	}
}
function initMenubars(){
	$$('#menubar table+a').each(function(el){
								   el.observe('mouseover',function(event){RollIt.showPopup(Event.element(event).previous('.mainsubmenu'))})
								   el.observe('click',function(event){$$('div.mainsubmenu').each(function(el){el.hide()})})
								   el.observe('mouseout',function(event){RollIt.hidePopup(Event.element(event).previous('.mainsubmenu'))})
								   })
	$$('table.mainsubmenu').each(function(el){
								   el.observe('mouseover',function(event){RollIt.showPopup(this)})
								   el.observe('mouseout',function(event){RollIt.hidePopup(this)})
								   el.observe('click',function(event){RollIt.hidePopup(this)})
								   })
}
function newsletterToggle(){
	new Effect.toggle('newsletterPanel','blind',{duration: 0.5})
}
function contactformToggle(){
	if($('contactform')._open!=1){
		$('contactform')._open=1
		 if(!$('frmContact')._height){$('frmContact')._height=340}
		 if(!$('frmContact')._logox){$('frmContact')._logox=0}

	new Effect.Move('logo',{x:$('frmContact')._logox,y:-128,mode:'absolute',duration:0.7})
	new Effect.Morph('contactform',{style:'height:'+$('frmContact')._height+'px;opacity:1.0;-moz-opacity:1.0;-webkit-opacity:1.0',duration: 0.7,afterFinish:function(){$('strContactName').focus()}})
	if($('panelportal')){new Effect.Morph('panelportal',{style:'height:0px;',duration: 0.7})}
	}else{
		$('contactform')._open=0
	new Effect.Move('logo',{x:246,y:-128,mode:'absolute',duration:0.7})
	new Effect.Morph('contactform',{style:'height:0px;opacity:0;-moz-opacity:0;-webkit-opacity:0',duration: 0.7})
	if($('panelportal')){new Effect.Morph('panelportal',{style:'height:340px;',duration: 0.7})}
	valid.reset();
	}
}
function signup(){
	if (!($F('strNewsletterEmail').blank())){
	$('btnSend').hide()
	$('ajaxLoader').show()
		new Ajax.Request("/ajax/subscribe.asp",{method:'post',parameters:{strNewsletterName:$F('strNewsletterName'),strNewsletterEmail:$F('strNewsletterEmail')},
					 onSuccess: function(transport){
						 pageTracker._trackEvent('Newsletter','Signup',$F('strNewsletterEmail'))
						 var response=transport.responseText.strip() || "no response";
						 if (response=="no response"){
							 $('frmSubscribe').update('Error: could not subscribe.')
						 }else{
							 $('frmSubscribe').update(response)
						 }
					 },
						 onFailure: function(transport){
						 var response=transport.responseText.strip() || "no response";
							 $('frmSubscribe').update('<abbr title="(f) '+response.stripTags().strip()+'">Error: could not subscribe.</abbr>')
						 }
					});
	}
}
function initContactform(){
	valid = new Validation('frmContact',{immediate:true})
	$('frmContact').observe('submit',contactSend)
}
function contactSend(e){
	if (valid.validate()){
		$('btnContactSend').hide()
		$('contactAjaxLoader').show()
		var strBody=$F('strContactEnquiry')
		var strName=$F('strContactName')
		new Ajax.Request("/ajax/sendenquiry.asp",{method:'post',parameters:$('frmContact').serialize(true),
					 onSuccess: function(transport){
						 pageTracker._trackEvent('Contact Form',$F('contactpref'),$F('strContactName'))
						 var response=transport.responseText.strip() || "no response";
						 if (response=="no response"){
							 $('frmContact').update('<h4><abbr title="(f) '+response.stripTags().strip()+'">Oops.</abbr></h4>There was a problem submitting the form. Sorry about that... you might want to <a href="mailto:wiseguys@bvdigital.co.uk">email us</a> instead.')
						 }else{
							 gaq.push(['_trackEvent', 'Enquiry Form', strName]);
							 $('frmContact').update(response)
							 $('frmContact').setStyle('margin-left:0')
							 $('frmContact')._height=170
							 $('frmContact')._logox=760
							new Effect.Move('logo',{x:760,y:-82,mode:'absolute',duration:0.7})
							new Effect.Morph('contactform',{style:'height:170px;opacity:1.0;-moz-opacity:1.0;-webkit-opacity:1.0',duration: 0.7,afterFinish:function(){$('strContactName').focus()}})
						 }
					 },
						 onFailure: function(transport){
						 var response=transport.responseText.strip() || "no response";
							 $('frmContact').update('<h4><abbr title="(f) '+response.stripTags().strip()+'">Oops.</abbr></h4>There was a problem submitting the form. Sorry about that... you might want to <a href="mailto:wiseguys@bvdigital.co.uk?body='+encodeURI(strBody)+'">email us</a> instead.')
						 }
					});
	}
	if(e){
		Event.stop(e)
	}
}
function initGalleryTabs(){
	if($('gallerytabs')!=null){
		$('gallerytab1').observe('click',function(){setGalleryPane('gallerytab1','gallerypane1')})
		$('gallerytab2').observe('click',function(){setGalleryPane('gallerytab2','gallerypane2')})
		$('gallerytab3').observe('click',function(){setGalleryPane('gallerytab3','gallerypane3')})
		$('gallerytab4').observe('click',function(){setGalleryPane('gallerytab4','gallerypane4')})
	}
}
function setGalleryPane(tabID,paneID){
	$$('.gallerypane').each(function(paneObj){paneObj.hide()})
	$$('.gallerytab').each(function(paneObj){paneObj.removeClassName('activetab')})
	$(paneID).show()
	$(tabID).addClassName('activetab')
}
function initMenuarrow(){
	if($('menuarrow')!=null){
		$('menuarrow').clonePosition('sidemenu',{setLeft:true,setTop:false,setWidth:false,setHeight:false})
		$('menuarrow').clonePosition($$('#sidemenu li.active')[0],{setLeft:false,setTop:true,setWidth:false,setHeight:false,offsetTop:-6})
	}
}
function contactus(){
	new Effect.ScrollTo('header',{duration:0.5})
	if($('contactform')._open!=1){
		contactformToggle.delay(0.5)
	}else{
		valid.reset();
		window.setTimeout(function(){$('strContactName').focus()},500);
	}

}
function initPanels(){
	$('panelcontainer')._arrPanels=['bettavend','armorgard','firsthr','friarsgate','stannah','ves','titchfield','coffeebeancompany','empath-e','electrofreeze']
	$('panelcontainer')._panelID=Math.floor(Math.random()*$('panelcontainer')._arrPanels.length)
	$('panelcontent').setStyle({backgroundImage: 'url(\'/images/panels/'+$('panelcontainer')._arrPanels[$('panelcontainer')._panelID]+'/bg_panel.jpg\')'});
	$('panelbutton').observe('click',rotatePanels)
	imageArray = new Array();
	for (i=0; i<$('panelcontainer')._arrPanels.length; i++){
		imageArray[i]=new Image();
		imageArray[i].src='/images/panels/'+$('panelcontainer')._arrPanels[i]+'/bg_panel.jpg';
	}
	new PeriodicalExecuter(rotatePanels,15)
}
function rotatePanels(){
	$('panelbutton').setStyle({opacity:0.4}).stopObserving('click')
	$('panelcontainer')._panelID=$('panelcontainer')._panelID+1;
	if($('panelcontainer')._panelID==$('panelcontainer')._arrPanels.length){$('panelcontainer')._panelID=0};
	new Effect.Opacity('panelcontent',{from:1,to:0,duration:0.3})
	Element.setStyle.delay(0.4,'panelcontent',{backgroundImage: 'url(\'/images/panels/'+$('panelcontainer')._arrPanels[$('panelcontainer')._panelID]+'/bg_panel.jpg\')'});
	new Effect.Opacity('panelcontent',{from:0,to:1,duration:0.4,delay:0.5})
	new Effect.Opacity('panelbutton',{from:0.4,to:1,duration:0.2,delay:0.7})
	Element.observe.delay(0.8,'panelbutton','click',rotatePanels)
}
