var mT, inputV = new Array (),pageNum=1, isIE6 = false, working=0, working2 = new Array (), mem = new Array (), mem2 = new Array (), uploadedFile = new Array (), missingList = new Array(), errorList = new Array();

if (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) isIE6 = true;

function messageBox(m,container,action) {
	var mB = 'messageBox';
	if (action =='close') {
		closeBox(mB,0.6,1)
	}
	else if (action =='update') {
		$(mB).update(m);
	}
	else {
		var s = m.split(' ');
		var secondsOut = Math.ceil((s.length/3)*0.6)*1000;
		if ($(mB) != null) {
			if ($(mB).visible()==true) {
				$(mB).update(m);
				if (mT != null) clearTimeout(mT);
				mT = setTimeout(function () { messageBox(null,null,'close'); }, secondsOut);
			}
		}
		else {
			if (container==null) container='body';
			else $(container).setStyle({position:'relative'});
			m='<div id="'+mB+'" style="display:none;">'+m+'</div>';
			$(container).insert(m);
			centreElement (mB,1,container);
			if (mT != null) clearTimeout(mT);
			mT='';
			$(mB).appear({ duration: 0.3, delay:0.2 });
			mT = setTimeout(function () { messageBox(null,null,'close'); }, secondsOut)
		}
	}
}


function loadingBox(onOff,container,delOL) {
	if (onOff==1) {
		if (container==null) container='mainWrapper';
		$(container).setStyle({position:'relative'});
		var m='<div id="loadingBox" style="display:none;"></div>',o;
		
		$(container).insert(m);
		if ($('pageOverlay') == null) {
			o='<div id="pageOverlay" style="display:none;"></div>';
			$(container).insert(o);
			$('pageOverlay').setStyle({height:$(container).getHeight()+'px',width:$(container).getWidth()+'px'});
		}
		if ($('pageOverlay') != null && $('pageOverlay').visible() == false) {
			$('pageOverlay').appear({ duration: 0.3, from:0, to:0.5 });
		}
		centreElement ('loadingBox',1,container);
		$('loadingBox').appear({ duration: 0.3, delay:0.2 });
	}
	else {
		if (delOL != null) closeBox('pageOverlay',0.5,1)
		closeBox('loadingBox',0.5,1)
	}
}

function pageOverlay (c) {
	var i = 'pageOverlay';
	if ($(i)!=null) $(i).remove();
	if (c==null) c='mainWrapper';
	$(c).setStyle({position:'relative'});
	$(c).insert('<div id="'+i+'" style="display:none;"></div>');
	$(i).setStyle({height:$(c).getHeight()+'px',width:$(c).getWidth()+'px'});
	$(i).appear({ duration: 0.3, from:0, to:0.5 });
}

function showBox(i,c,ov) {
	if (c==null) c='mainWrapper';
	if (ov) pageOverlay(c)
	centreElement (i, 1, c)
	$(i).appear({ duration: 0.5, delay:0.2 });
}

function centreElement (e, vert, c) {
	var a = document.viewport.getWidth(),b = document.viewport.getHeight(),theScrolls = document.viewport.getScrollOffsets();
	theScrolls = document.viewport.getScrollOffsets();
	if (c) {
		theScrolls[0]=0
		a = $(c).getWidth()
	}
	theLeft = Math.floor(((a)-($(e).getWidth()))/2) + theScrolls[0];
	theTop = Math.floor(((b)-($(e).getHeight()))/2) + theScrolls[1];
	if (theTop < 0) theTop = 0;
	if (vert==1)  {
		$(e).setStyle({ top: '0px' });
		$(e).setStyle({ top: theTop+'px' });
	}
	$(e).setStyle({ left: theLeft+'px' });
	
}

function closeBox(i,d,r) {
	$(i).fade({ duration: d, afterFinish:function () {
		if (r != null) $(i).remove();
	}});
}


function clearInputBox(i,t,o,c) {
	if (o==1) {
		if ($F(i) == t) $(i).value='';
	}
	else if (o==2) {
		$(i).stopObserving('focus');
		$(i).stopObserving('blur');
		$(i).observe('focus', function () { clearInputBox(i,t,1,c); });
		$(i).observe('blur', function () { clearInputBox(i,t,0,c); });
	}
	else {
		if ($F(i) == '') $(i).value=t;	
	}
	if (c) { $(i).setStyle({ color: c }); }
}

function isValidEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1) return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
	if (str.indexOf(at,(lat+1))!=-1) return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
	if (str.indexOf(dot,(lat+2))==-1) return false;
	if (str.indexOf(" ")!=-1) return false;
	return true;			
}

function handleEnter (event, func, v) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		event.stop()
		if (func) func (v);
		return false;
	}
	else return true;
}

function tinyMCEStuff (i,t) {
	if (t == 'focus') {
		tinyMCE.execCommand('mceAddControl', false, i);
		tinyMCE.execInstanceCommand("mceFocus",false,i);	
	}
	if (t == 'remove') {
		tinyMCE.execCommand('mceRemoveControl', false, i);	
	}
	if (t == 'get') {
		return tinyMCE.get(i).getContent();	
	}
}

function ajaxText (f) {
	var c = '', h = 'hiddenField', r, i,dd;
	
	r = f.split(',');
	r.each(function(i){
		
		var t = $(i).tagName.toLowerCase()
		
		if (t=='form') {
			
			var fi = $$('#'+f+' input[type="email"]', '#'+f+' input[type="text"]', '#'+f+' input[type="password"]', '#'+f+' textarea', '#'+f+' select').uniq().compact(),ff = new Array();
			
			fi.each(function(n){
				if (n.id.blank()==false) ff.push(n.id);
			});
			if (ff.length>0) c = c + ajaxText (ff.join());
			
		}
		else {
			var p = $(i).getOffsetParent();
			$(p).insert('<input type="hidden" id="'+p+'_'+h+'" name="'+i+'_2" />');
			if ($(i).hasClassName('tinyMCE')) {
				
				$(p+'_'+h).setValue(tinyMCE.get(i).getContent());
				
			}
			else if (t=='textarea' || (t=='input' && ($(i).type=='text' || $(i).type=='password' || $(i).type=='email'))) {
				
				$(p+'_'+h).setValue($F(i).replace("‘","'").replace("’","'"));
				if (($(i).type=='text' && $(i).hasClassName('email')==false && $(i).hasClassName('noEnt')==false) || ($(i).type =='textarea' && $(i).hasClassName('noEnt')==false)) $(p+'_'+h).setValue(htmlentities($F(p+'_'+h)));
				if (t=='textarea') $(p+'_'+h).setValue(nl2br($F(p+'_'+h),true));
			}
			else if (t=='select') {
				$(p+'_'+h).setValue($F(i));
			}
			c = c+'&'+$(p+'_'+h).serialize();
			$(p+'_'+h).remove();
				
		}
	});
	return c;
}

function nl2br (str, is_xhtml) {
    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';

    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}

function br2nl (str) {
    return str.replace(/<br\s*\/?>/mg,"\n");
}


function errorMessage (errTtl,missTtl) {
	
	if (errTtl == null) errTtl = 'Please rectify the following errors:';
	if (missTtl == null) missTtl = 'The following fields are missing:';
	
	var t='';
	
	if (missingList.length>0) {
		
		t = t + missTtl+'<br /><br />';
		
		missingList.each(function(n){
		
			t = t + n+'<br />';
		
   		});
		
	}
	if (errorList.length>0) {
		
		if (t != '') t = '<br />'+t;
		
		t = t + errTtl+'<br /><br />';
		
		errorList.each(function(n){
		
			t = t + n+'<br />';
		
   		});
		
	}
	missingList.clear();
	errorList.clear();
	messageBox(t);
}

// f = a comma seperated string of form ID's, exFl = a comma seperated string of field ID's to exclude
function validateForm (q,exFl) {
	if (exFl != null) exFl = exFl.split(',');
	else exFl = new Array();
	var form = q.split(',');
	form.each(function(f){
		
		$$('#'+f+' input[type="text"]').each(function(n){

			if (inputV[n.id]==null) inputV[n.id] = '';
			if ( ($F(n.id)=='' || $F(n.id)==inputV[n.id]) && (exFl.lastIndexOf(n.id)<0) && $(n.id).hasClassName('mandatory') ) {
				
				if ($$('label[for="'+n.id+'"]')[0] != null) missingList.push($$('label[for="'+n.id+'"]')[0].innerHTML);
				
			}
			else if ( ($(n.id).hasClassName('email')) && (isValidEmail($F(n.id))==false) ) {
				
				if ($$('label[for="'+n.id+'"]')[0] != null) errorList.push($$('label[for="'+n.id+'"]')[0].innerHTML +' - the email doesn\'t appear to be correct');
				
			}
			
		});
		
		$$('#'+f+' input[type="email"]').each(function(n){
			if (inputV[n.id]==null) inputV[n.id] = '';
			if (exFl.lastIndexOf(n.id)<0 && $(n.id).hasClassName('mandatory')) {
			
				if ($F(n.id)=='' || $F(n.id)==inputV[n.id]) {
					
					if ($$('label[for="'+n.id+'"]')[0] != null) missingList.push($$('label[for="'+n.id+'"]')[0].innerHTML);
					
				}
				else if (isValidEmail($F(n.id))==false) {
					
					if ($$('label[for="'+n.id+'"]')[0] != null) errorList.push($$('label[for="'+n.id+'"]')[0].innerHTML +' - the email doesn\'t appear to be correct');
					
				}
			
			}
			
		});
		
		$$('#'+f+' input[type="password"]').each(function(n){
			if (inputV[n.id]==null) inputV[n.id] = '';
			if (exFl.lastIndexOf(n.id)<0 && $(n.id).hasClassName('mandatory')) {
			
				if ($F(n.id)=='' || $F(n.id)==inputV[n.id]) {
					
					if ($$('label[for="'+n.id+'"]')[0] != null) missingList.push($$('label[for="'+n.id+'"]')[0].innerHTML);
					
				}
			
			}
			
		});
		
		$$('#'+f+' select').each(function(n){
			
			if (exFl.lastIndexOf(n.id)<0 && $(n.id).hasClassName('mandatory')) {
				
				if ($F(n.id)=='0') {
					
					if ($$('label[for="'+n.id+'"]')[0] != null) missingList.push($$('label[for="'+n.id+'"]')[0].innerHTML);
					else alert(n.id);
					
				}
			
			}
			
		});
		
		$$('#'+f+' textarea').each(function(n){
			if (inputV[n.id]==null) inputV[n.id] = '';
			if (exFl.lastIndexOf(n.id)<0 && $(n.id).hasClassName('mandatory')) {
			
				var c = $F(n.id);
			
				if ($(n.id).hasClassName('tinyMCE')) {
					c = tinyMCE.get(n.id).getContent().stripScripts().stripTags();
				}
				if (c=='' || c==inputV[n.id]) {
					
					missingList.push($$('label[for="'+n.id+'"]')[0].innerHTML);
					
				}
			
			}
			
		});
	});
	if (missingList.length < 1 && errorList.length < 1) return true; else return false;
}



function html_entity_decode(str) {
    var  tarea=document.createElement('textarea');
    tarea.innerHTML = str; return tarea.value;
    tarea.parentNode.removeChild(tarea);
}

function getCookie(c_name)	{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
  {
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function setCookie(c_name,value,exdays) {
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}


/********************** ALL ************************/

function setUpAll (n) {
	var doIt;
	if (n) {
		var a=$$('form');
		a.each(function(n){
			$(n.id).reset();
		});
	}
	var a=$$('input[type="text"]','textarea','input[type="password"]','input[type="email"]');
	a.each(function(n){
		if (n.id.empty()==false) {
			clearInputBox($(n.id),$F(n.id),2);
			inputV[n.id]=$F(n.id);
		}
   	});
	var a=$$('input[type="radio"]');
	a.each(function(n){
		if (n.id.empty()==false) {
			Event.observe(n.id, 'dblclick',function (e) {
				$(n.id).checked = false;
			});
		}
   	});
	if ($('listen_live') != null)
		Event.observe('listen_live', 'click',function (e) {
			Event.stop(e);
			listenLive ();
		});
	if ($('listen_live2') != null)
	Event.observe('listen_live2', 'click',function (e) {
		Event.stop(e);
		listenLive ();
	});
	var f1 = 'newsletterForm';
	if ($(f1) != null) {
		
		Event.observe(f1, 'submit',function (e) {
			Event.stop(e);
			if (working==0) {
				
				if (validateForm (f1)) {
					working=1;
					var aj1 = new Ajax.Request(baseURL+'ajaxStuff.php', {method: 'post', parameters: 'newsletter=1'+ajaxText (f1),onComplete: function(resp) {
						working=0;
						messageBox(resp.responseText);
						$(f1).reset();
					}});
				}
				else errorMessage ();
			}
		});
		
	}
	var f2 = 'searchForm';
	if ($(f2) != null) {
		
		Event.observe(f2, 'submit',function (e) {
			Event.stop(e);
				
			if ($('s').present() && $F('s') != inputV['s']) {
				window.location.href = baseURL+'isearch/s='+encodeURIComponent($F('s'));
			}
		});
		
	}
	var f3 = 'contactForm';
	if ($(f3) != null) {
		
		Event.observe(f3, 'submit',function (e) {
			Event.stop(e);
			if (working==0) {
				
				if (validateForm (f3)) {
					working=1;
					var aj1 = new Ajax.Request(baseURL+'ajaxStuff.php', {method: 'post', parameters: 'contact=1'+ajaxText(f3)+'&toID='+$F('toID')+'&pageName='+pageName,onComplete: function(resp) {
						working=0;
						messageBox(resp.responseText);
						$(f3).reset();
					}});
				}
				else errorMessage ();
			}
		});
		
	}
	var a=$$('a.share[id^=share-]');
	a.each(function(n){
		addthis.button("#"+n.id, {}, {url: $(n.id).href, title: $(n.id).title, data_track_clickback: true, ui_click: true, addthis_button_tweet: true });		
	});
	if ($('miniPlayerCont') != null) {

	var flashvars = {file: $('miniPlayerURL').href,autostart:false};
	var params = {allowfullscreen: "true",allowscriptaccess: "always",wmode: "transparent"};
	var attributes = {};
	swfobject.embedSWF(baseURL+'jw_player/player2.swf', 'miniPlayerCont', 281, 24, "9.0.0","expressInstall.swf", flashvars, params, attributes);
	
}
}

function listenLive () {
	var url = '/player.php';
	window.open(url, "myWindow", "status = 1, height = 410, width = 760, resizable = 0" );
}

function bannerWidths () {
	var w = document.viewport.getDimensions()['width'];
	$('main-home').setStyle({width:w+'px'});
	var l = Math.ceil((3000-w)/2);
	l = l-(l*2);
	$('bannerOuter').setStyle({left:l+'px'});
}
var bn=1;
function setupBanner () {
	bannerWidths ();
	Event.observe( window, 'resize',bannerWidths);
	var banners = new Array('bL','bM','bR');
	
	$('bL').insert('<div class="article greyed" id="bL2"><div id="bL2_c" class="opDiv">'+$('bM1').innerHTML.replace('banners/main/','banners/main2/')+'</div></div>');
	$('bL').insert('<div class="article greyed" id="bL3"><div id="bL3_c" class="opDiv">'+$('bR1_c').innerHTML.replace('banners/main/','banners/main2/')+'</div></div>');
	
	$('bM').insert('<div class="article" id="bM2">'+$('bR1_c').innerHTML.replace('banners/main2/','banners/main/')+'</div>');
	$('bM').insert('<div class="article" id="bM3">'+$('bL1_c').innerHTML.replace('banners/main2/','banners/main/')+'</div>');
	
	$('bR').insert('<div class="article greyed" id="bR2"><div id="bR2_c" class="opDiv">'+$('bL1_c').innerHTML.replace('banners/main/','banners/main2/')+'</div></div>');
	$('bR').insert('<div class="article greyed" id="bR3"><div id="bR3_c" class="opDiv">'+$('bM1').innerHTML.replace('banners/main/','banners/main2/')+'</div></div>');
	banners.each(function (b) {
		$(b+'1').removeClassName('selected');
		$(b+'1').setStyle({ left:'0px' });
	});
	$$('div.greyed div.opDiv').each(function (n) { $(n.id).setOpacity(0.1) });
	new PeriodicalExecuter(function(pe) {
		
		$$('#bannerOuter .wrapper .article').each( function (n) { $(n.id).setStyle({zIndex:3}); });
		var banners = new Array('bL','bM','bR'),i=1,eff = new Array(),old;
		old=bn;
		if (bn==3) bn=1; else ++bn;
		banners.each(function (b) {
			$(b+old).setStyle({zIndex:2});
			eff[i] = new Effect.Move(b+bn, { x: -1000, y: 0, mode: 'relative', afterFinish: function(){
				var i2=1;
				while (i2<4) {
					if (i2 != bn) $(b+i2).setStyle({ left:'1000px' });
					++i2;
				}
			}});
			++i;
			eff[i] = new Effect.Move(b+old, { x: -1000, y: 0, mode: 'relative'});
			++i;
		});
		
	}, 5);

}


/********************** GALLERY ************************/

var galleryCurrentPage=1, galleryPages;
function setupGallery() {
	var m = 0;
	$$('div#content1 a img').each( function (n) { 
		if (m==0) loadGalItem (n.id,1);
		m = m + parseInt($(n.id).getWidth()); 
	});
	if ($('content1') != null) {
		$('content1').setStyle({width:m+'px'});
		galleryPages = Math.ceil(m/635);
		$('scrollLeft').observe('click', function (e) {
			Event.stop(e);
			if (working<1 && galleryCurrentPage>1) {
				working=1;
				new Effect.Move('content1', { x: 635, y: 0, mode: 'relative', transition: Effect.Transitions.spring, afterFinish: function(){ working=0; --galleryCurrentPage; }});
			}
		});
		$('scrollRight').observe('click', function (e) {
			Event.stop(e);
			if (working<1 && galleryCurrentPage<galleryPages) {
				working=1;
				new Effect.Move('content1', { x: -635, y: 0, mode: 'relative', transition: Effect.Transitions.spring, afterFinish: function(){ working=0; ++galleryCurrentPage; }});
			}
		});
	}
	
}

function showVimeo (vidID, width, height, divID, colour,auto) {
	var flashvars = {clip_id: vidID,server: "vimeo.com",show_title: "0",show_byline: "0",show_portrait: "0",fullscreen: "1",color: colour,autoplay:auto};
	var params = {allowfullscreen: "true",allowscriptaccess: "always",movie: "http://vimeo.com/moogaloop.swf",wmode: "transparent"};
	var attributes = {clip_id: vidID};
	swfobject.embedSWF("http://vimeo.com/moogaloop.swf", divID, width, height, "9.0.0","expressInstall.swf", flashvars, params, attributes);
}

function showYouTube (vidID, width, height, divID, auto) {
	var flashvars = {};
	var params = {allowfullscreen: "true",allowscriptaccess: "always",wmode: "transparent"};
	var attributes = {id: "vid"};
	swfobject.embedSWF("http://www.youtube.com/v/"+vidID+"&hl=en&fs=1&rel=0&color1=0x000000&color2=0x2d2d2d&autoplay="+auto, divID, width, height, "9.0.0","expressInstall.swf", flashvars, params, attributes);
}

function showJWPlayer (t) {
	var flashvars = {file: baseURL+'file_uploads/mp3s/'+t,autostart:true};
	var params = {allowfullscreen: "true",allowscriptaccess: "always",wmode: "transparent"};
	var attributes = {};
	swfobject.embedSWF(baseURL+'jw_player/player2.swf', 'player', 355, 24, "9.0.0","expressInstall.swf", flashvars, params, attributes);
}

function loadGalItem (id,n) {
	var m = id.split('___'),a;
	if (n==null) a=1;
	switch (m[1]) {
		case 'youtube':
			$('mainContent').update('<div id="mainContent2"></div>');
			showYouTube (m[2], 685, 482, 'mainContent2',a);
			break;
		case 'vimeo':
			$('mainContent').update('<div id="mainContent2"></div>');
			showVimeo (m[2], 685, 482, 'mainContent2', 'ff0000',a);
			break;
		case 'photo':
			var pad = Math.ceil(($('mainContent').getWidth()-parseInt(m[2]))/2);
			$('mainContent').setStyle({paddingLeft: pad+'px'});
			$('mainContent').update('<img src="'+$(id.replace('gal','pic')).src.replace('main2','main')+'" />');
			break;
		  
	}
	loadGalleryItemText (m[3]);
	
}

function loadGalleryItemText (id) {
	$('itemTitle').update('&nbsp;');
	$('itemText').update('&nbsp;');
	var aj1 = new Ajax.Request(baseURL+'ajaxStuff.php', {method: 'post', parameters: 'loadGalleryItemText='+id,onComplete: function(resp) {
		var r = resp.responseText.evalJSON();
		$('itemTitle').update(r.title+'&nbsp;');
		$('itemText').update(r.text+'&nbsp;');
	}});
}

/********************** CONTACT ************************/

function setupContact() {
	
	$$('ul.staff-nav li a').each( function (n) {  
		Event.observe( n.id, 'click',function (e) {
			Event.stop(e);
			var f = n.id.replace('staffLink','staff-member'),i=1;
			while ($('staff-member'+i) != null) { $('staff-member'+i).hide(); $('staffLink'+i).removeClassName('active'); ++i; }
			$(f).show();
			$(n.id).addClassName('active');
		} );
	});
	$$('div.coverage-details ul li a').each( function (n) {  
		Event.observe( n.id, 'mouseover',function (e) {
			Event.stop(e);
			var f = (parseInt( n.id.replace('map','')) * 333)-333;
			f=f-(f*2);
			$('theMap').setStyle({left:f+'px'});
			$$('div.coverage-details ul li a').each( function (d) { $(d.id).removeClassName('active'); });
			$(n.id).addClassName('active');
		} );
		Event.observe( n.id, 'click',function (e) {
			Event.stop(e);
		} );
	});
	
}

/********************** MUSIC ************************/
var chart=1;
function setupMusic() {
	var m=0,ww=940;
	$$('div#content1 div.carousel-item').each( function (n) { 
		m = m + parseInt($(n.id).getWidth())+10; 
	});
	$('content1').setStyle({width:m+'px'});
	galleryPages = Math.ceil(m/ww);
	$('scrollLeft').observe('click', function (e) {
		Event.stop(e);
		if (working<1 && galleryCurrentPage>1) {
			working=1;
			new Effect.Move('content1', { x: ww, y: 0, mode: 'relative', transition: Effect.Transitions.spring, afterFinish: function(){ working=0; --galleryCurrentPage; }});
		}
	});
	$('scrollRight').observe('click', function (e) {
		Event.stop(e);
		if (working<1 && galleryCurrentPage<galleryPages) {
			working=1;
			new Effect.Move('content1', { x: -ww, y: 0, mode: 'relative', transition: Effect.Transitions.spring, afterFinish: function(){ working=0; ++galleryCurrentPage; }});
		}
	});
	
	$('prev-chart').observe('click', function (e) {
		Event.stop(e);
		if (chart>1) {
			--chart;
			var t = 1;
			while (t<31) {
				$('chart'+t).hide();
				++t;
			}
			t = chart*10-10;
			++t;
			var i=1;
			while (i<=10) {
				$('chart'+t).show();
				++t;
				++i;
			}
		}
	});
	$('next-chart').observe('click', function (e) {
		Event.stop(e);
		if (chart<3) {
			++chart;
			var t = 1;
			while (t<31) {
				$('chart'+t).hide();
				++t;
			}
			t = chart*10-10;
			++t;
			var i=1;
			while (i<=10) {
				$('chart'+t).show()
				++t;
				++i;
			}
		}
	});
	
	$$('a[id^=like]','a[id^=dislike]').each( function (n) {  
		Event.observe( n.id, 'click',function (e) {
			Event.stop(e);
			likeDislike(n.id);
		});
	});
	
}

function likeDislike(id) {
	
	var l = id.split('_');
	if (working==0) {
		if (getCookie(id) == null) {
			working=1;
			var aj1 = new Ajax.Request(baseURL+'ajaxStuff.php', {method: 'post', parameters: 'likeIt='+id,onComplete: function(resp) {
				if (l[0]=='like') {
					$('likesQTY_'+l[1]).update(parseInt($('likesQTY_'+l[1]).innerHTML)+1);
				}
				working=0;
				setCookie(id,1,1);
			}});
		}
	}
	
}

function setupComps() {
	
	if ($('competitionForm') != null) {
	
		Event.observe( 'competitionForm', 'submit',function (e) { 
												
			Event.stop(e);
			if (working==0) {
				
				var answer,vars;
				
				$$('input[type=radio]').each(function (n) { if ($(n.id).checked==true) answer = n.id.replace('answer_',''); });
				
				if (answer==null) missingList.push('Your answer');
				
				if ( (validateForm ('competitionForm')) && (answer!=null) ) {
					working=1;
					
					vars = 'competition=1'+ajaxText('competitionForm')+'&answer='+answer+'&compID='+$F('compID');
					
					if ($('comp-receive').checked==true) vars = vars + '&receive=1';
					
					var aj1 = new Ajax.Request(baseURL+'ajaxStuff.php', {method: 'post', parameters: vars,onComplete: function(resp) {
						working=0;
						messageBox(resp.responseText);
						$('competitionForm').reset();
					}});
				}
				else errorMessage ();
				
			}
			
												
		});
	
	}
	
}

function setupListenerParty() {
	
	if ($('competitionForm') != null) {
	
		Event.observe( 'competitionForm', 'submit',function (e) { 
												
			Event.stop(e);
			if (working==0) {
				
				var vars;
				
				if (validateForm ('competitionForm')) {
					working=1;
					
					vars = 'listener_party=1'+ajaxText('competitionForm')+'&compID='+$F('compID');
					
					if ($('comp-receive').checked==true) vars = vars + '&receive=1';
					
					var aj1 = new Ajax.Request(baseURL+'ajaxStuff.php', {method: 'post', parameters: vars,onComplete: function(resp) {
						working=0;
						messageBox(resp.responseText);
						$('competitionForm').reset();
					}});
				}
				else errorMessage ();
				
			}
			
												
		});
	
	}
	
}

var dT = new Array(), more;
function setupDJShowsHome () {
	
	dT[1] = $('contentCont').innerHTML;
	dT[2] = dT[1].stripTags();
	if (dT[2].length>400) {
		dT[2] = dT[2].substring(0,400)+'...';
		$('moreShowDJ').show();
		$('contentCont').update('<p>'+dT[2]+'</p>');
		Event.observe( 'moreShowDJ', 'click',function (e) { 
			Event.stop(e);
			
			if (more==null) {
				$('contentCont').update(dT[1]);
				$('moreShowDJ').update('&laquo; Less');
				more=1;
			}
			else {
				$('moreShowDJ').update('More &raquo;');
				$('contentCont').update('<p>'+dT[2]+'</p>');
				more=null;
			}
		});
	}
	
}

document.observe("dom:loaded", function() {
	if ($('main-home') != null) {
		
		setupBanner ();
	
	}
	if ($('contentCont') != null) {
		
		setupDJShowsHome ();
	
	}
	
	setUpAll(1);
});

function submitCode () {
	if (validateForm('winForm')==true) {
		var aj = new Ajax.Request(baseURL+'ajaxStuff.php', {method: 'post', parameters: 'submitBaristasForm=1'+ajaxText('winForm')+'&baristasFile='+baristasFile,onComplete: function(response) {
			if (response.responseText == 'Thanks, your details have been sent.') $('winForm').reset();
			messageBox(response.responseText);
		}});
	}
	else errorMessage();
}

function submitCode2 () {
	if (validateForm('winForm')==true) {
		var aj = new Ajax.Request(baseURL+'ajaxStuff.php', {method: 'post', parameters: 'submitIRockForm=1&'+$('winForm').serialize()+'&iRockFile='+iRockFile,onComplete: function(response) {
			if (response.responseText == 'Thanks, your details have been sent.') $('winForm').reset();
			messageBox(response.responseText);
		}});
	}
	else errorMessage();
}


Event.observe( window, 'load',function () { 
	
	switch (thisPage) {
		case 'igallery ig-videos':
			setupGallery();
			break;
		case 'igallery ig-photos':
			setupGallery();
			break;
		case 'shows shows-az dj-videos':
			setupGallery();
			break;
		case 'shows shows-az dj-photos':
			setupGallery();
		case 'shows pres-az dj-videos':
			setupGallery();
			break;
		case 'shows pres-az dj-photos':
			setupGallery();
			break;
		case 'contact staff1':
			setupContact();
			break;
		case 'music':
			setupMusic();
			break;
		case 'win party':
			setupListenerParty();
		case 'win':
			setupComps();
			break;
		case 'baristas':
			Event.observe('submit', 'click',function (e) { Event.stop(e); submitCode();});
			break;
		case 'baristas irock':
			Event.observe('submit', 'click',function (e) { Event.stop(e); submitCode2();});
			break;
		  
	}
});
