/* ======================================================================

RGH JS Control

=======================================================================*/

/*---[	Globals	]---*/
/* var globalRoot = 'http://extranet.avian.co.uk/RGH/' */
var globalRoot = 'http://www.royalgardenhotel.co.uk/App_Themes/RoyalGarden'
var traceOut = false

/*---[	HeadShots	]---*/
var headshots = [
		'/graphics/headshots/headshot_alan_robinson.png', 
		'/graphics/headshots/headshot_carolan_brown.png', 
		'/graphics/headshots/headshot_graham_bamford.png', 
		'/graphics/headshots/headshot_steve_munkley.png'
	]
/* ======================================================================



=======================================================================*/
window.addEvent('domready', function(){

	
	/*
	Sitewide JS
	------------------------------------*/
	if(!exists('.DesignMode') && !exists('.EditMode')){

		trace('domReady')
/*---[	Availability Slider	]---*/
		if(exists('#avail .toggle')){ setupAvailSlider() }
		
/*---[	Search Area	]---*/
		if(exists('#searchBox')){ setupSearchBox() }
		if(exists('#searchArea')){ headhunter() }
		
/*---[	Buttons at page top	]---*/
		if(exists('#callback')){ setupCallBackFeature() }
		if(exists('#tweetlink')){ setuptweetlink() }
		
/*---[	TagThing Elements + Layout	]---*/
		if(exists('#tagThing .tagThing_section')){ tagThing_loader(globalRoot+'/js/tagThing/', 'tagThing', 500, 'right', 60, true) }
		else{if( exists('#tagThing') && !exists('#tagThing img') ){ $('tagThing').setStyle('display', 'none') } }
		if(exists('#tagThing')){ $('tagThing').setStyle('z-index', 100); $('tagThing').setStyle('z-index', 200); }
		/*---[	slideShows	]---*/
		if(exists('#homepageslideshow') ){ $('tagThing').setStyle('display', 'block'); } 
		if(exists('#slideshow_pan') ){ $('tagThing').setStyle('display', 'block') } 
	    
/*---[	Flyout Menu	]---*/
	    if( exists('#lhs')){ var myMenu = new MenuMatic({id: 'lhs', orientation:'vertical' }); }
	    if(exists('#navHolder')){ bigupTheNav.delay(5, $('navHolder')); bigupTheNav.delay(1500, $('navHolder')) }
	    
/*---[	Room Feature Links	]---*/
	    if( exists('.roomFeatures li')){ setupCreatureFeatures($$('.roomFeatures li'), 3500) }


/*---[	Contact table	]---*/
	    if(exists('.contacttable')){
	    	$('pic_wrapper').setStyle('background', '#7F7C5D')
	    	$$('.exploreSub').each(function(el,i){ el.setStyle('height', $('pic_wrapper').getStyle('height')) });
	    	$$('.contacttable').each(function(el,i){ fckHeight.delay(1500, el)});
	    }
	    
/*---[	FAQ Page menu	]---*/
	    if( exists('.faq_copy') ){ sloppySlippy() }
	    
/*---[	Div Balancer	]---*/
	    if(exists('.equal')){balanceHeights.delay(500);}
	    
/*---[	Show Alt Text	]---*/
		if(exists('#enqForm_side')){ altOnTop($$('#enqForm_side img')) }
		
		if(exists('#sideOffer')){ bounceIntoNav($('sideOffer')) }
	}

	
	/*
	Only for use in CMS
	------------------------------------*/
	else{
		if(exists('#sideOffer')){ $('sideOffer').setStyle('display', 'block') }
		if(exists('#tagThing .tagThing_section')){
			$$('#tagThing .tagThing_section').each(function(el,i){
				el.setStyles({
					display: 'block',
					float: 'right',
					width: 200
				})	
			});
		}
	}
	
	
	/* Toggler
	-------------------------------------------------------------------*/
	var accordion = new Fx.Accordion($$('.action_image'),$$('.hr_section_content'), {
		opacity: 0
	});
	
	if($$('.hum_resource .ErrorLabel').length>0 ){ $('validation_block_hr').setStyle('display', 'block') } 
	
	$$('.abs').setStyle('position', 'absolute')
	
});


/* ======================================================================

MAIN FUNCTIONS

=======================================================================*/
/*
Bounce Into Nav
------------------------------------*/
function bounceIntoNav(obj){
	var clone = obj.clone().injectInside($('navHolder')).fade('hide');
	obj.dispose()
	clone.setStyles({
		display: 'block',
		position: 'absolute',
		bottom: 0
	})
	clone.ta=1
	delayFade.delay(1000, clone)
}

/*
Alt On Top
------------------------------------*/
function altOnTop(arr){
	arr.each(function(el,i){
		el.txt = el.alt
		trace(i+' :: '+el.txt)
		el.altbox = new Element('div').addClass('altBox').injectBefore(el).fade('hide')
		el.altbox.setStyle('position', 'absolute')
		el.altbox.set('html', '<p>'+el.txt+'</p>')
		el.altbox.p = el
		el.offset = (el.getSize().y-el.altbox.getSize().y)-2
		el.altbox.setStyles({
			width: el.getSize().x.toInt() - 4,
			position: 'absolute',
			background: '#5b5943',
			margin:  el.offset+'px 0 0 2px'
		})

		el.addEvents({
			'mouseenter': function(){
				this.active = true
				this.altbox.fade(.9)
			},
			'mouseleave': function(){
				this.active = false
				checkaltbox.delay(500,this)
			}
		});
		el.altbox.addEvents({
			'mouseenter': function(){
				this.active = true
			},
			'mouseleave': function(){
				this.active = false
				checkaltbox.delay(500,this.p)
			}
		});
	});
}
function checkaltbox(){
	if(this.altbox.active!=true && this.active!=true){this.altbox.fade(0)}
}

/*
Head Hunter
------------------------------------*/
function headhunter(){
	if(Browser.Engine.trident && Browser.Engine.version == 4){ 
		var target = $('searchArea').getFirst('.headShot')
		var r = $random(0, headshots.length-1)
		/* target.setStyle('background', 'url('+globalRoot+''+headshots[r]+')') */
		target.setStyle('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader (src='"+globalRoot+''+headshots[r]+"', sizingMethod='scale'");

	}
	else{
		var target = $('searchArea').getFirst('.headShot').getFirst('img')
		var r = $random(0, headshots.length-1)
		trace('seed : '+r)
		target.setProperty('src', globalRoot+''+headshots[r]);
		/* target.src=globalRoot+''+headshots[r] */
		var s1 = target.getProperty('src').toString().split('/')
		var s2 = s1[s1.length-1].split('.')
		var s3 = s2[0].split('_')
		var txt = s3[1]+' '+s3[2]
		/* target.alt = txt.capitalize() */
		target.setProperty('alt', txt.capitalize())
		target.fade('hide')
		target.setStyle('display', 'block')
		target.ta = 1
		delayFade.delay(1000, target)
	}
}


/*
Pop Link Buttons
------------------------------------*/
function setuptweetlink(){
	var obj = $('tweetlink')
	obj.setStyle('display', 'block')
	obj.mHeight = 20
	obj.htmlStr = "<h2>Follow us on Twitter</h2>"
	buttonBox(obj)
	obj.addEvents({
		'click': function(){window.location="http://twitter.com/royalgdnhotel" },
		'mouseenter': function(){ bb_hover(this) },
		'mouseleave': function(){ bb_out(this) }
	});
}

function setupCallBackFeature(){
	var obj = $('callback')
	obj.setStyle('display', 'block')
	obj.htmlStr = "<h2>Click for a Call Back</h2><p>Click the 'Call Me' button and we will call you back (free of charge)</p>"
	obj.mHeight = 70
	buttonBox(obj)
	obj.addEvents({
		'click': function(){webVoicePop('Template=55376'); },
		'mouseenter': function(){ bb_hover(this) },
		'mouseleave': function(){ bb_out(this) }
	});
}

function buttonBox(obj){
	obj.pbox = new Element('div').addClass('pBox').injectInside($('top_buttons')).setStyle('height', 0).fade('hide')
	obj.pbox.setStyles({
		right: obj.getStyle('right').toInt()-100,
		top:25
	})
	obj.pbox.set('html', obj.htmlStr)
	obj.morph = new Fx.Morph(obj.pbox, {duration: 300, transition: Fx.Transitions.Sine.easeOut, link:'cancel'});
}

function bb_hover(obj){
	obj.morph.start({'height': obj.mHeight})	
	if(Browser.Engine.trident){obj.pbox.fade(1)}
	else{obj.pbox.fade(.9)}
}
function bb_out(obj){
	obj.morph.start({'height': 0})
	obj.pbox.fade(0)
}

	

/*
misc layout fixes
------------------------------------*/
function fckHeight(){
	this.setStyle('display', 'inline')
	this.setStyle('display', 'block')
	if(exists('#map')){
		$('map').setStyle('display', 'inline')
		$('map').setStyle('display', 'block')
	}
}

function bigupTheNav(){
	trace('navHolder >>> '+this.getSize().y.toInt())
	trace('pic_wrap >>> '+$('pic_wrapper').getSize().y.toInt())
	if($('pic_wrapper').getSize().y.toInt() > 410){
		this.ty = $('pic_wrapper').getSize().y.toInt()
		this.mph = new Fx.Morph(this, {duration: 300, transition: Fx.Transitions.Sine.easeOut, link:'cancel'});
		this.mph.start({'height': this.ty})
	}
}

function sloppySlippy(){
	$$('.fauxMenu a').each(function(el,i){
		el.addEvents({
			'click': function(e){
				e.stop()
				
				findAnchor(el)
				trace($(el.anch))
				el.slippy = new Fx.SmoothScroll(window).toElement($(el.anch));
			},
			'mouseleave': function(){
				
			}
		});	
	});
}

function findAnchor(obj){
	var anc = obj.get('href')
	var anch = anc.substring(1,anc.length)
	$$('.faq_copy a').each(function(el,i){
		
		if(el.name.toString()==anch.toString()){ 
			trace('anch: '+anch+' :: '+el.name+' :: '+el) 
			obj.anch = el
		}	
	});
}



/*
Pop Links 
------------------------------------*/
function popLinks(trigs, boxes){
	boxes.each(function(el,i){
		el.fade('hide')
		el.setStyle('display', 'none')
		el.container = boxes
		el.ii = i
		setupLinkBox.delay(50, el)
		el.getParent('div').setStyle('display', 'block')	
	});
	var tCount = 0
	trigs.each(function(el,i){
		if(isEven(i)){el.i = tCount}
		else{el.i = tCount;tCount++}
		trace(':::'+el)
		el.addEvents({
			'click': function(e){
				e.stop();
				boxes[this.i].show()
			},
			'mouseleave': function(){  }
		});	
	});
	
}


function setupLinkBox(){
	whois(this)
	/*---[	setup pages	]---*/
	this.divs = this.getChildren('div')
	this.divs[1].addClass('rc_page')
	this.divs[2].addClass('rc_page')
	
	findThumbs(this)
	
	/*---[	close button	]---*/
	setupCloseButton.delay(50,this)
		
	/*---[	call to show function	]---*/
	this.show = function(){ showBox(this) }
	this.close = function(){ closeBox(this) }
}

function findThumbs(obj){
	trace('findThumbs')
	obj.thumbs = obj.divs[1].getFirst('.venuesthumbs').getChildren('img')
	obj.thumbs.each(function(el,i){
		whois(el)
		el.link = new Element('div').injectAfter(el).addClass('link')
		if(i==0){el.link.set('text', 'View Floor Plan')}
		else if(i==1){el.link.set('text', 'View Gallery')}
		el.link.p = obj
		el.link.addEvents({
			'click': function(){
				showPage(obj, i)
			},
			'mouseleave': function(){
				
			}
		});
	});
	
}

function showPage(obj, i){
	i++
	n=i+1
	obj.divs[i].fade(0)
	obj.divs[n].fade(1)
}

function setupCloseButton(){
	this.closeBut = new Element('a').injectBefore(this.getFirst('div')).addClass('closebut').set('text', 'close');
	if(!Browser.Engine.Trident){this.closeBut.fade(.5)}
	this.closeBut.set('href', '#')
	this.closeBut.setStyles({
		position: 'absolute',
		right: 5,
		top: 5
	});
	this.closeBut.p = this
	this.closeBut.addEvents({
		'click': function(e){e.stop();this.p.close.delay(50, this.p)},
		'mouseenter': function(){this.fade(1)},
		'mouseleave': function(){this.fade(.5)}
	});
}

function showBox(obj){
	trace('showBox: '+obj.ii)
	obj.setStyle('display', 'block')
	obj.container.each(function(el,i){ if(el.active = true && el.ii != obj.ii){ el.active = false; el.fade(0); hideOut.delay(500, el) }	});
	obj.active = true 
	obj.fade(1)
	obj.slippy = new Fx.SmoothScroll(window).toElement($('header_sub'));
	obj.divs[1].fade('show')
	obj.divs[2].fade('hide')
	if(exists('.breakout')){ $$('.breakout').each(function(el,i){ el.fade(0) }); }
}
function hideOut(){
	this.setStyle('display', 'none')
}
function closeBox(obj){
	trace('closeBox: '+obj.ii)
	obj.container.each(function(el,i){ el.active = false; el.fade(0); hideOut.delay(500, el) })
	if(exists('.breakout')){ $$('.breakout').each(function(el,i){ el.fade(1) }); }
}
/*
drawTo - used by tagThing
------------------------------------*/
function drawTo(jg,coords,hex){
	jg.setStroke(3);
	jg.setColor(hex);
	jg.drawLine(coords[0],coords[1],coords[2],coords[3]);
	jg.paint();
}

/*

/*
setupCreatureFeatures
------------------------------------*/
function setupCreatureFeatures(arr,delay){
	var obj = arr[arr.length-1].getParent('ul')
	obj.flash = new Element('div', { 'id': 'featureFlash'	}).injectAfter( obj).fade('hide');
	obj.flash.setStyles({
		width: 220,
		height: 295,
		position: 'absolute',
		top: 40,
		background: '#D9C296'
	})
	obj.wait = delay
	obj.arr = arr
	arr.each(function(el,i){
		el.parent = el.getParent('ul')
		if(i!=0){el.fade('hide')}
		el.tag = el.getFirst('p').getFirst('strong').get('text')
		/* trace(el.tag) */
	});
	obj.nowShowing = 0
	obj.showNext = 1
	obj.over = false
	obj.addEvents({
		'mouseenter': function(){
			this.over = true
		},
		'mouseleave': function(){
			this.over = false
			/* nextFeature.delay(this.wait, this) */
		}
	});
	nextFeature.delay(obj.wait, obj)
}

function nextFeature(){
		/* trace('nextFeature: '+this.showNext) */
	/* if(this.over==false){ */
		this.flash.fade(1)
		showNextFeature.delay(1000, this)
	/* } */
}
function showNextFeature(){
	/* trace('fade out: '+this.nowShowing) */
	this.arr[this.nowShowing].fade('hide')
	/* trace('fade in: '+this.showNext+' :: '+this.arr[this.showNext].tag) */
	this.arr[this.showNext].fade('show')
	this.nowShowing = this.showNext
	if(this.showNext<this.arr.length-1){ this.showNext++ }
	else{ this.showNext = 0 }
	this.flash.fade(0)
	nextFeature.delay(this.wait, this)
}

/*
tagThing
------------------------------------*/
function tagThing_loader(path,s,zroot,anchor,bsize,autoClose){
	if(!document.body.tagThing_options){ document.body.tagThing_options = [] }
	document.body.tagThing_options = [path,s,zroot,anchor,bsize,autoClose]
	trace('tagThing >> Options : '+document.body.tagThing_options)
	if(!document.body.tagThing_Code){document.body.tagThing_Code = new Asset.javascript(path+'tagThing.js', {id: 'tagThing_Code' })}
}

/*
Search Box
------------------------------------*/
function setupSearchBox(){
	trace('setupSearchBox: '+$('searchBox'))
	var obj
	var sub
	$$('#searchBox input').each(function(el,i){
		if(i==0){ obj = el }	
		else{ sub = el }
	});
/* 	obj = $('searchBox').getFirst('div').getFirst('input') */
	obj.def = $('searchBox').getFirst('div').getFirst('label').get('text')
	
	obj.def = 'Search'
	obj.value = obj.def
	obj.addEvents({
		'focus': function(){ if(this.value==this.def){this.value = ''} },
		'blur': function(){ if(this.value.trim()==''){this.value = this.def} }
	});
	sub.addClass('subBut')
	sub.value = 'go'
	sub.setStyle('display', 'none')
}

/*
Availability Slider
------------------------------------*/
function av_trig(){
	
	var obj = $('avail')
	obj.togg.fireEvent('mouseenter')
}
function setupAvailSlider(){
	trace('setupAvailSlider')
	var obj = $('avail')
	obj.togg = obj.getFirst('.toggle')
	obj.togg.p = obj
	obj.open = false
	obj.slideDelay = 500
	obj.slider = new Fx.Morph(obj, {duration: obj.slideDelay, transition: Fx.Transitions.Sine.easeOut, link:'cancel'});
	obj.togg.addEvents({
		'mouseenter': function(){
			/*
this.addClass('active')
			if(this.p.open==false){
				this.p.slider.start({'height': 410})
				this.p.open = true
				this.setStyle('-moz-box-shadow', '0px 2px 2px #6f6c56')
				this.setStyle('-webkit-box-shadow', '0px 2px 2px #6f6c56')
			}
			else{
	 			this.p.over = false
	 			closeSlider.delay(5, this.p)
			}
*/
		},
		'click' : function(e){
			e.stop()
			if(this.p.open==false){
				this.p.slider.start({'height': 410})
				this.p.open = true
				this.setStyle('-moz-box-shadow', '0px 2px 2px #6f6c56')
				this.setStyle('-webkit-box-shadow', '0px 2px 2px #6f6c56')
			}
			else{
	 			this.p.over = false
	 			closeSlider.delay(5, this.p)
			}
		}
	});
 	obj.addEvents({
 		'mouseenter': function(){
 			this.over = true
 			this.togg.addClass('active')
 			if(this.open==true){this.slider.start({'height': 410})}
 		},
 		'mouseleave': function(){
 			this.over = false
 			closeSlider.delay(500, this)
 		}
 	});
}
function closeSlider(){
	if(this.over == false){
		this.slider.start({'height': 30})
		switchClass.delay(this.slideDelay, this)
	}
}
function switchClass(){
	if(this.over==false){
		this.togg.removeClass('active')
		this.open = false
			this.setStyle('-moz-box-shadow', 'none')
			this.setStyle('-webkit-box-shadow', 'none')
	}
}

/* ======================================================================

Util Functions

=======================================================================*/

/*
Tracer
------------------------------------*/
function trace(msg){
	if(traceOut==true){
		if(!document.body.TracerStack){document.body.TracerStack=[]} document.body.TracerStack.push(msg)
		if(document.body.tracerEnabled){document.body.TraceOut.show(msg)}
		else if(!document.body.tracerLoad)
		{ document.body.tracerLoad = true; var TracerScript = new Asset.javascript(globalRoot+'/js/tracer/Trace.js', {id: 'Tracer' }) }
	}
}

/*
trace object properties
------------------------------------*/
function whois(obj){ trace('Whois :: '+obj+' :: type: '+$type(obj)+' :: id: '+obj.get('id')+' :: class: '+obj.className) }

/*
check for even number
------------------------------------*/
function isEven(value){
	if (value%2 == 0)
		return true;
	else
		return false;
}

/*
Check to see a given element exists
------------------------------------*/
function exists(chk){ 
	if($$(chk).length>0)
		return true;
	else
		return false; 
}

/*
Caps
------------------------------------*/
String.prototype.capitalize = function(){ return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } ); };

/*
Balance Heights
------------------------------------*/
var bhc = 0
function balanceHeights(){
	var arr = $$('.equal')
	var heights = []
	arr.each(function(el,i){ heights.push(el.getPosition().y.toInt()+el.getSize().y.toInt()) });
	arr.each(function(el,i){ 
		trace(i+' :: '+heights[i])
		if(heights.max()-heights[i]>0){ el.spacer = new Element('div').injectInside(el).setStyles({ height: heights.max()-heights[i], clear: 'both' }) }
	});
	if(bhc<1){
		bhc++
		balanceHeights(arr)
	}
}

/*
Fade To
------------------------------------*/

function delayFade(){ this.fade(this.ta) }