//(C)Alex Barnard 2011
//Loads External Menu Items
//Loads External Data 
jQuery.noConflict();
jQuery(document).ready(function() {
	var __aspClass
		,__proID=2293
		,__showID=2295
		,www="/_asp/"
//		,www="/darkhorse-design.co.uk/bct/_asp/"	
function _initaliseHTML($id,$url){
	var id=(typeof($id)!="string"?('#'+$id.id):$id)
	initaliseContainer(id)
	//BUTTONS
	jQuery($id).find("a.button_link").click(function(){
		var butTxt=this.innerHTML
		if(/\bevaluation\b/i.test(butTxt)){
			var oP=jQuery(this).parent()
			var oF=jQuery($id).find('form')
			oF.find('input[name="Evaluation"]')[0].value=oP.find('input[name="db100"]')[0].value
			oF.submit()
		}else if(/hide/i.test(butTxt)){
			//hide
			var o=jQuery(this).parents(".toggle_frame")
			jQuery('html').animate({scrollTop:o.offset().top},'slow')
			o.hide('slow',function(){
				o.find('.toggle').removeClass("active")
				//o.find('.toggle').addClass("checked")
				o.find('.toggle_content').hide()
				o.find('input').attr('checked',false)
			});
		}else if(/view/i.test(butTxt)){
			//view
			jQuery(".toggle_frame").each(function(){
				jQuery(this).find('input').attr('checked',true)
				jQuery(this).show('slow')
			})	
		}else if(/close/i.test(butTxt)){
			//close
			jQuery(this).parents(".toggle_frame").find('h4.toggle').each(function(){
				jQuery(this).click(); 
				//jQuery(this).addClass("checked")
			})	
		}else if(/\bsearch\b/i.test(butTxt)){
			jQuery($id).find('input[name="ok"]').attr('value','')
			jQuery($id).find('form').submit()
		}else
			return true
			
		return false
	})
	
	jQuery($id).find("h4.toggle").click(function(){
		jQuery(this).addClass("checked")
	})	
	jQuery($id).find('button[type=button]').click(function(){
		if(/search/i.test(this.value)){
			//Search Again
			jQuery(this.form).addClass("ignore")
			jQuery($id).find('input[name=ok]').attr('value','')
			jQuery(this.form).submit()
		}else if(/view/i.test(this.value)){
			//View ALl
			jQuery(".toggle_frame").each(function(){
				jQuery(this).find('input').attr('checked',true)
				jQuery(this).show('slow')
			})	
		}
	})
	//FORMS
	jQuery($id).find('form').submit(function(){return _aspForm(this)})
	jQuery($id).find('input[type=text]').each(function(){
		var self=jQuery(this)
		self.keypress(function($e){
			var k=$e.keyCode||$e.which||$e.charCode,c=String.fromCharCode(k)
			//if(k==8||k==9||k==0||$e.ctrlKey||$e.altKey||$e.metaKey)return true
			if(/\b(currency|percent)\b/.test(this.className)){
				if(/[\d.]/.test(c))
					if($e.deleteKey)//check if decimal point added
						return (this.value.indexOf(".")<1)
					else
						return (k==46&&!/\./i.test(this.value))||k!=46
			}else 
				return true
			return false			
		})
		if(self.hasClass('requiredField')){
			self.change(function($e){
				jQuery(this).toggleClass('inputError',this.value=='')
			})
			self.change()
		}	
		if(/pc[\w-]+/.test(this.className)){
			self.change(function($e){
				var cls=this.className.match(/pc[\w-]+/),t=0,oF=jQuery(this.form)
				oF.find('.'+cls[0]).each(function(){t+=this.value*1})
				this.form[cls].value=t
				oF.find('.'+cls+', input[name='+cls+']').toggleClass('inputError',t!=100)
			})
			self.change()
		}
	})
}
function _aspForm($o){
	var hasError = false;
	
	jQuery($o).find('.error').remove();
	jQuery($o).find('.inputError').removeClass('inputError');
	if(!jQuery($o).hasClass('ignore')){
		jQuery($o).find('.percent-total').each(function(){
			if(this.value*1!=100){
				hasError = true;
				jQuery(this.form).find('.'+this.name+', #'+this.id).addClass('inputError');
			}	
		})
		jQuery($o).find('.requiredField').each(function() {
			var self=jQuery(this)
			var val=self.val()
			if(jQuery.trim(self.val()) == '') {
				var labelText = jQuery(this).prev('label').text();
				//jQuery(this).parent().append('<span class="error">You forgot to enter your '+labelText+'.</span>');
				self.addClass('inputError');
				hasError = true;
			} else if(self.hasClass('email')) {
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				if(!emailReg.test(jQuery.trim(val))) {
					var labelText = self.prev('label').text();
					//jQuery(this).parent().append('<span class="error">You entered an invalid '+labelText+'.</span>');
					hasError = true;
					self.addClass('inputError');
				}
			} else if(self.hasClass('phone')) {
				if(!/^(([\d\(\) ]+)|([\d\(\) ]+ ext \d+))$/.test(val)){
					hasError = true;
					self.addClass('inputError');
				}	
			} else if(self.hasClass('postcode')) {
				if(!/^([a-z]{1,2}\d{1,2}[a-z]?)[ ](\d\d?[a-z]{2})$/i.test(val)){
					hasError = true;
					self.addClass('inputError');
				}	
			}
		});
	}	
	if(hasError){
		jQuery($o).append('<p class="error" style="color:#cc0000">The form contains errors. Please check the highlighted boxes.</p>');
		return false
	}
	_loadDataTo(__aspClass,jQuery($o).serialize(),www+$o.action.match(/\w+.\w+\?*\w*$/))

	return false
}
function _loadDataTo($o,$d,$url){
	var o=document.createElement("DIV")
	o.className="cover"
	jQuery(o).fadeTo(0)
//alert("$o="+$o)	
	if(jQuery($o).children().length)
		jQuery($o).children().first().before(o)
	else
		jQuery($o).append(o)
	$url=$url.replace(/\/$/,'')
//alert("URL="+$url)
	jQuery($o).find('.cover').fadeTo('slow',0.66,function(){
//	jQuery('#form_bar').fadeOut(500)
	jQuery.ajax({
		type:'POST'
		,url: $url
		,data: $d
		,datatype:'json'
		,cache: false 
		,success: function(data){
			//highlight Search Text
			var s=window.location.search.match(/search\=(\S+)/)
			if(s){
				s=s[1]
				var oRE,a=["(\\w{4})(able|fully|ment|ness)$","(\\w{4})(ful|ies|ing|ion|ise|ism|ize|ous)$","(\\w{3})(s|al|ed|er|ly|le|ly)$"]
				for(var i=0;i<a.length;i++){
					oRE=new RegExp(a[i],"gi")
					s=s.replace(oRE,function(a,b,c,d){return b})
				}
				oRE=new RegExp('('+s+'(\\w+|\\b))',"i")
				data=data.replace(/(\>)([^<]*)(\<)/g,function(a,b,c,d){return b+c.replace(oRE,'<span class="highlight">\$1</span>')+d})
			}
			//load new data into aspblock
			jQuery($o).fadeOut('slow',function(){
				//load into temp DIV to ensure correct HTML formatting.
				o=document.createElement('div')
				o.innerHTML=data
				jQuery($o).html(o.innerHTML);

//				jQuery('.background').fadeTo(0,0.66)
				//update promoter menu if current section name has word "promoters"
				if(/promoters/.test($o))_addPromoterMenu()
				_initaliseHTML($o)
				//set page title
				document.title='B.C.T. :: '+jQuery('h1').text()
				o=$url.match(/\?([^ ]*)$/)
				//open current show
				if(o){
					o=o[1].split('/')
					o=o[o.length-1].replace(/_/g,' ')
					o=jQuery($o).find('h4:contains('+o+')').click()
				}	
				jQuery($o).fadeIn('slow',function(){jQuery($o).attr('style',jQuery($o).attr('style').replace(/filter:\s+;?/i,''))})
			})
		}
	});
	})
	return false
}

function _addPromoterMenu(){
	if(jQuery('#menu-item-'+__proID).children().length>1)return
	jQuery.ajax({
		url: www+"update_menu_promoters.asp" 
		,cache: false  
		,success: function(data){
			if(data.length>1){
				jQuery('#menu-item-'+__proID).append(data)
				jqueryslidemenu.buildmenu("main_navigation")
			}
		}
	});
}

function _addShowMenu(){
	jQuery.ajax({
		url: www+"update_menu_shows.asp" 
		,cache: false  
		,success: function(data){
			var a=document.URL.match(/\.uk\/(\w+)\/?(\S+)?/)
			if(a==null)a=['','']
//a[2]=window.location.search
//a[1]='promoters'
//a[2]='?search'
//a[2]='?bookings'
//a[1]='shows'
			__aspClass='.aspblock'+a[1]
			var scrollTop=0;qs=a[2]?a[2]:''
			//add new items to wordpress menu item 
			jQuery('#menu-item-'+__showID).after(data)
			//initalise Search Form
			jQuery('#form_show_search').submit(function(){if(this.search.value==this.search.defaultValue){this.search.focus();return false}return true})
			//update menu.
			jqueryslidemenu.buildmenu("main_navigation")
			//check if aspclass on page
			if(jQuery(__aspClass).length){
				//ADD PRINT CSS to page.
				scrollTop=1;
				var oSS=document.createElement('link');
				oSS.type = 'text/css';
				oSS.rel = 'stylesheet';
				oSS.href = www+'../_css/aspblock_print.css';
				oSS.media = 'print';
				oSS.title = 'dynamicLoadedSheet';
				document.getElementsByTagName("head")[0].appendChild(oSS);
				//insert external content
				_loadDataTo(__aspClass,'',www+a[1]+'.asp'+qs)
			}
			//load widgets
			var w='aspwidget',oW=jQuery('.'+w)
			oW.each(function($i){
				var f=this.className.match(/(\w+)\-(\d+)/)
				if(f.length){
					var id=w+f[1]+parseInt(Math.random()*Math.pow(10,10))
					this.id=id
					_loadDataTo('#'+id,'',www+'widget_'+f[1]+'.asp?'+f[2])
				}	
			})
			if(scrollTop)jQuery("html").animate({scrollTop:150},'slow')
		}
	});
}
	//------------------------
	_addShowMenu()
})

