	var timer1;

	function loadComponent(dst, component, method, params, afterCompletion, inner){
		$.post("/backend/ajax/loadComponent.php",{ component: component, method: method, params: params},function(ret) {
				if(inner == undefined || inner == ''){
					if(typeof(dst) == 'object'){
						$(dst).replaceWith(ret);
					}else{
						if(dst != '' && ret != ''){
							$("#"+dst).replaceWith(ret);
							//$("#"+dst).html(ret);
							//$("span#"+dst+"_ajax").attr('tag', "'" + dst + "','" + component + "','" + method + "','" + params + "'");
						}
					}
				}else{
					if(typeof(dst) == 'object'){
						$(dst).html(ret);
					}else{
						if(dst != '' && ret != ''){
							$("#"+dst).html(ret);
						}
					}
				}
				
				if(afterCompletion)
					eval(afterCompletion);
			});
	}
	
	Date.prototype.getWeek = function() {
		var onejan = new Date(this.getFullYear(),0,1);
		var week = Math.ceil((((this - onejan) / 86400000) + onejan.getDay())/7);
		if(week > 52)
			week -= 52;
		
		return week;
	}
	
	$(document).ready(function() {
		// fix headlines
		Cufon.replace('.menybox div');
		$('.menybox div[class$=topic]').css('font-size', 100);
		//Cufon.replace('span.header');
		
		// resize old images
		fixImages();
		
		// connect ajax functions
		hookPagers();
		
		// hook navigate bars
		hookNavigate();
		
		// hook extra functions
		//pageScripts();
		
		imagePreview();
		
		autoComplete();
	});
	
	function fixImages(){
		$('div.imageFix img').each(function(ev){
			if($(this).attr('height') < 120)
				$(this).attr('height', 120);
		});
	}
	
	function hookPagers(){
		var limit = 0;
		$('div.pager span[class~=page]').click(function(ev){
			var limit = $('div.content', $(this).closest('div.full-column-section')).length;
			loadComponent($(this).closest('div.pager').attr('id') + 'Holder', 'Event', 'Ajax'+$(this).closest('div.pager').attr('id'), limit + '|' + (parseInt($(this).attr('id'), 10)-1)*limit + '||' + $('#'+$(this).closest('div.pager').attr('params')).val(), "hookPagers(); fixImages();");
		});

		$('div[id$=Holder] span[class=SubMenuItem] a').click(function(ev){
			var limit = $('div.content', $(this).closest('div.full-column-section')).length;
			loadComponent($(this).closest('div.full-column-section').attr('id'), 'Event', 'Ajax'+$(this).closest('div.full-column-section').attr('id').replace('Holder', ''), limit + '|'+(parseInt($('div.pager span[class~=selectedPage]').attr('id'), 10)-1)*limit+'|'+$(this).attr('id')+'|'+$('#q').val(), "hookPagers(); fixImages();");
		});
	}
	
	function layerVisibility(name, mode){
		switch(mode){
			case 'show':
				$('#'+name).show();
				break;
				
			case 'hide':
				$('#'+name).hide();
				break;
		}
	}
	
	function hookContenders(eId,bId){
		clearTimeout(timer1);
		timer1 = setTimeout("loadComponent('ListContendersHolder', 'Event', 'AjaxListContenders', '" + eId + "|" + bId + "', 'hookContenders(" + eId + "," + bId + "); hookPagers(); ');", "60000");
	}
	
	function hookNavigate(){
		$("div#nav span[id!='']").each(function(){
			$(this).click(function(){
				loadComponent('ListBandHolder', 'Event', 'AjaxSearchBands', $(this).attr('id'), 'hookNavigate()');
			});
		});
	}
	
	function imagePreview(){	
		/* CONFIG */
			
			xOffset = 10;
			yOffset = 30;
			
			// these 2 variable determine popup's distance from the cursor
			// you might want to adjust to get the right result
			
		/* END CONFIG */
		$("a.preview").hover(function(e){
			this.t = this.title;
			this.title = "";	
			var c = (this.t != "") ? "<br/>" + this.t : "";
			$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 
			$("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");						
	    },
		function(){
			this.title = this.t;	
			$("#preview").remove();
	    });	
		$("a.preview").mousemove(function(e){
			$("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		});			
	};
	
	function autoComplete(){
		$('#country').change(function(){ $('#city').html('<option value="">Choose City</option>'); $('#region').html('<option value="">Choose Region</option>'); if($(this).val() != ''){ loadComponent($('#region'), 'Event', 'AjaxReadRegions', $(this).val(), '', true);  }});
		$('#region').change(function(){ if($(this).val() != '' && $('#country').val() != ''){ loadComponent($('#city'), 'Event', 'AjaxReadCities', $('#country').val() + '|' + $(this).val(), '', true);  }});
	}
	
	function validate(src, dialog){
		var error = '';
		
		$('.required', src).each(function(){
			if($(this).val() == '' || $(this).val() == 0){
				error += $(this).attr('tag') + "\r\n";
				$(this).addClass('failed');
			}
		});
		
		if(error != ''){
			if(dialog != false){
				alert('You\'ve forgotten the following fields in your profile, please scroll down and correct this, incorrect fields are now marked with yellow: \r\n\r\n' + error + '\r\nYou will not be able to participate in any events until this is addressed.');
			}
		}else{
			return true;
		}
		
		return false
		
	}