var RollIt = {
    timeout : null,
    showPopup : function(e){
        clearTimeout(this.timeout);
        if($(e).style.display == 'none'){
				$$('div.submenu').each(function(el){el.hide().setStyle({height:'0px'});});
			   $(e).show();
			   var theHeight= $(e).down('table').getHeight() + 'px'
			   new Effect.Morph(e,{style:{height:theHeight},duration:0.2})
        }
    },
    hidePopup : function(e){
        if($(e).style.display == 'none'){
            clearTimeout(this.timeout);
        }else{
            this.timeout = setTimeout(function(){$(e).hide();},200);
        }
    }
};

function initMenubars(){
	$$('#dropdownmenu ul li>a').each(function(el){
								   el.observe('mouseover',function(event){RollIt.showPopup(Event.element(event).previous('.submenu'));});
								   el.observe('click',function(event){$$('div.submenu').each(function(el){el.hide();});});
								   el.observe('mouseout',function(event){RollIt.hidePopup(Event.element(event).previous('.submenu'));});
								   });
	$$('#dropdownmenu div.submenu').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 swapImage(event) {
	var thumb = Event.element(event);
	var thumbcontainer = $(thumb).up('.thumb')
	$$('#captions li').each(function(captions){captions.setStyle({color:''})});
	$$('.thumb').each(function(thumb){thumb.setStyle({borderColor:'#555'})});
	var caption = ("caption"+$(thumbcontainer).identify().substr(5,1))
	if ($(caption)!=undefined){
		$(caption).setStyle({color:'#FEB929'})
	}
	var thephoto = $(thumb).up('.thumb').identify().gsub('thumb','photoBig')
	thumbcontainer.setStyle({borderColor:'#999'});
	$($('photobox')._current).hide();
	$(thephoto).show()
	$('photobox')._current=thephoto
}

document.observe("dom:loaded", function() {
	initMenubars();

	if ($('sizechart') != undefined) {
		hs.graphicsDir = '/highslide/graphics/';
		hs.showCredits = false;
		hs.outlineType = null;
		hs.wrapperClassName = 'popupwrapper';
		hs.dimmingOpacity = 0.65;
		hs.width = 558;
		hs.align = 'center';
		hs.captionEval = 'this.a.title';
		hs.allowSizeReduction = false;
		hs.preserveContent = false;
		hs.registerOverlay({
			html: '<div class="closebutton" onclick="return hs.close(this)" title="Close"></div>',
			position: 'top right',
			useOnHtml: true,
			fade: 2
		});
	} // END if ($('sizechart') != undefined)

	if ($('photobox') != undefined) {
		// Initialise thumbnails
		
		$$('.thumb').each(function(thumbnail){
								   if ($(thumbnail).down('img') != undefined) {
									   var photoSrc = $(thumbnail).down('img').readAttribute('src').gsub('_thumb','')
									   var photoID = $(thumbnail).identify().gsub('thumb','photoBig')
									   $('photobox').insert({bottom: new Element('img',{src:photoSrc,id:photoID,style:'display:none;position:absolute;top:0px;left:0px;'})})
								   }
								   });
		if ($('photoBig1') != undefined) {
			$$('#photobox img')[0].hide()
			$('photoBig1').show()
			$('photobox')._current='photoBig1'
		}
		$$('.thumb img').each(function(thumbnail){thumbnail.observe('mouseover',swapImage)});
		if ($('thumb1').down('img') != undefined) {
			$('thumb1').setStyle({borderColor:'#999'})
		}
		if ($('photobox').down('img') === undefined) {
			$('photobox').hide();
		}
		if ($$('.thumb img')[1] === undefined) {
			$$('.thumb').each(function(thumbnail){thumbnail.hide()});
		}
	} // END if ($('photobox') != undefined)
	
	if ($('btnRange') != undefined) {
		$('btnRange').observe('click',function(){
											   Event.stop(event);
											   new Effect.ScrollTo('sizechart')
											   })
	}
});

function trackclick(var1,var2){
	var _gaq = _gaq || [];
	_gaq.push(['_trackEvent', var1, var2]);
}

// ##########################################################################################################
// AXENT.JS
if (Object.isUndefined(Axent)) { var Axent = { } }
Axent.SelfLabeledInput = Class.create({
	initialize: function() {
		var labelSelector = arguments[0] || 'label';
		$$(labelSelector).findAll(function(l) {
										   return (l.readAttribute('for') !== null)}).each(function(l){
		    l.hide();
			$(l.readAttribute('for'))._value = l.innerHTML;
			if ($(l.readAttribute('for')).value.empty()) {
                $(l.readAttribute('for')).value = $(l.readAttribute('for'))._value
            }
			$(l.readAttribute('for')).observe('blur',function(e){if(Event.element(e).value == '') Event.element(e).value = Event.element(e)._value;});
			$(l.readAttribute('for')).observe('focus',function(e){if(Event.element(e).value == Event.element(e)._value) Event.element(e).value = '';});
		});
		var formField = arguments[1];
		$(formField).observe('submit',function(){
		$$(labelSelector).findAll(function(l) {return (l.readAttribute('for') !== null)}).each(function(l){
			$(l.readAttribute('for')).observe('blur',function(e){if(Event.element(e).value == '') Event.element(e).value = Event.element(e)._value;});
			if($(l.readAttribute('for')).value==$(l.readAttribute('for'))._value){$(l.readAttribute('for')).value=''}
		});
												})
		
	}
});
// ##########################################################################################################


function showSignup(){
	if(typeof(hideSignupTimer)!=="undefined"){clearTimeout(hideSignupTimer);}
	if(typeof(SignupEffect)!=="undefined"){SignupEffect.cancel();}
	SignupEffect = new Effect.Morph('subForm',{style:{height:'40px'},duration:0.3});
}
function hideSignup(){
	if ($('otrwj-otrwj').hasFocus||$('btnSignupSubmit').hasFocus||$('signupbox').mouseover){return;}
	if(typeof(SignupEffect)!=="undefined"){SignupEffect.cancel();}
	SignupEffect = new Effect.Morph('subForm',{style:{height:'0px'},duration:0.3});
}
function initSignup(){
	$('signupbox').observe('mouseover',function(){this.mouseover=true;showSignup();});
	$('signupbox').observe('mouseout',function(){this.mouseover=false;hideSignupTimer=setTimeout(hideSignup,250);});
	$('otrwj-otrwj').observe('focus',function(){this.hasFocus=true;});
	$('otrwj-otrwj').observe('blur',function(){this.hasFocus=false;hideSignupTimer=setTimeout(hideSignup,100);});
	$('btnSignupSubmit').observe('focus',function(){this.hasFocus=true;});
	$('btnSignupSubmit').observe('blur',function(){this.hasFocus=false;hideSignupTimer=setTimeout(hideSignup,100);});
}


var enquirySelfLabeledInput = new Axent.SelfLabeledInput('#subForm label.inline','subForm');
initSignup()
