	var timer1;
	function loadComponent(dst, component, method, params, afterCompletion, bind){
		
		$.post("/loadcomponent/"+method,{ component: component, method: method, params: params},function(ret, e) {
			if(e == 'success'){
				// new for OA
				if(typeof(dst) == 'object'){
					dst.html(ret);
				}else if(dst.indexOf('inner') == -1 && dst != ''){
					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(dst != ''){
					if(typeof(dst) == 'object'){
						$(dst).html(ret);
					}else{
						if(dst != ''){
							$("#"+dst.replace('inner_','')).html(ret);
						}
					}
				}else{
					return ret;
				}
				
				if(typeof(afterCompletion) == 'object'){
					switch(bind){
						case 'slide':
							afterCompletion.slideToggle();
							break;
						
						case 'animate':
							afterCompletion.slideToggle().animate({ opacity: 1 }, 500);
							break;
					}
					//afterCompletion.trigger(bind);
				}else 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('.topic');
		//Cufon.replace('span.header');
		
		// hook new layout
		hookNewLayout();
		
		// hook links
		hookLinks();
		
		// resize old images
		fixImages();
		
		// connect ajax functions
		hookPagers();
		
		// hook navigate bars
		hookNavigate();
		
		// hook contenders
		hookContenders();
		
		// hook extra functions
		//pageScripts();
		
		imagePreview();
		
		autoComplete();
	
		// hook search boxes
		inline_search();
		
		// Hook Admin contenders
		hookAdminContenders();
		
		// load first youtube movie.
		if($('#youtube_player').attr('id') != undefined && $('#youtube_player').attr('tag') != ''){
			changeYoutubeMovie($('#youtube_player').attr('tag'));
		}
		
		// myspace player
		if(typeof(startMyspacePlayer) == 'function') {
			startMyspacePlayer();
		}
		
		$('.confirm_delete').click(function(ev) {
			
			if($(this).parent('div').children(':checkbox').val() == 'on') {
				if(!confirm('Are you sure you want to delete this post?')) {
					ev.preventDefault();
					ev.stopPropagation();
				} 
			}
		});	
		
		$(':input.dp_enabled').datepicker( { firstDay: 1, dayNamesMin:["Sö","Må","Ti","On","To","Fr","Lö"], dateFormat: "yy-mm-dd", monthNames: ['Januari','Februari','Mars', 'April','Maj','Juni','Juli','Augusti','September','Oktober','November','December'] });
		
		/*
		$('div.sponsorHeader').mouseover(function(ev) {
			$(this).children('form').children('div.sponsorImg').show();
		}).mouseout(function() {
			$(this).children('form').children('div.sponsorImg').hide();
		});
		*/
		
		
		$(':input.rte').wysiwyg({ autoSave: true });
		
		$('.blogThumb').each(function() {
			if($(this).width() > 150) {
				$(this).width(150);
			} else if($(this).height() > 150) {
				$(this).height(150);
			}
		}).click(function() {
			variable = prompt('Copy this url to your blogpost:', $(this).attr('src'));
		});
		
	});
	
	function cornerImages(){
		$('img.corner, img.cornerLeft, img.myCorner').each(function(){
			if(!jQuery.browser.msie){
				$(this).css('background-image', 'url('+$(this).attr('src')+')');
				$(this).attr('width', $(this).attr('width'));
				$(this).attr('height', $(this).attr('height'));
				$(this).attr('src', '/images/layout/trans.png');
				$(this).css('background-position', '50% 0%');
			}
			
			$(this).next('div[class=imageOverlay]')
				.css('width', $(this).attr('width'))
				.corner('bottom 8px')
		});
	}
	
	function fixImages(){
		$('div.imageFix img').each(function(ev){
			if($(this).attr('height') < 120)
				$(this).attr('height', 120);
		});
	}
	
	function hookLinks(){
		$('.link, .row[tag!=nolink]')
			.css('cursor','pointer')
			.click(function(){ if(typeof($(this).attr('tag')) !== 'undefined'){ location.href=($(this).attr('tag') != '' ? $(this).attr('tag') : $(this).attr('id')); }})
			.find('a').attr('target', '_self')
	}

	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 showYoutube(src){
		$('#myspace_video').hide();
		$('#youtube_video').show();
		$('#'+src).prev().removeClass('selected');
		$('#'+src).addClass('selected').blur();
		
	}
	
	function showMyspace(src){
		$('#myspace_video').show();
		$('#youtube_video').hide();
		$('#'+src).next().removeClass('selected');
		$('#'+src).addClass('selected').blur();		
	}
	
	function hookContenders(eId,bId){
		clearTimeout(timer1);
		//timer1 = setTimeout("loadComponent('ListContendersHolder', 'Event', 'AjaxListContenders', '" + eId + "|" + bId + "', 'hookContenders(" + eId + "," + bId + "); hookPagers(); ');", "15000");
		
		$('div#contenders').click(function(){
			$(this).animate({ 
				scrollTop: parseInt($('div#contenders').scrollTop()) + 300
			}, 800 );
		});
	}
	
	function hookNavigate(){
		$("div#nav span[id!='']").each(function(){
			$(this).click(function(){
				loadComponent('ListBandHolder', 'Event', 'AjaxSearchBands', $(this).attr('id'), 'hookNavigate()');
			});
		});
		
		$("select[class=blog_filter]").change(function(){
			location.href=$(this).val();
		});
	}
	
	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
		
	}
	
	function youtube_scroll_up(){
		$('div.youtube_holder').animate({ 
			scrollTop: parseInt($('div.youtube_holder').scrollTop()) - parseInt($('div.youtube_holder').css('height'))
      }, 500 );
	}
	
	function youtube_scroll_down(){
		$('div.youtube_holder').animate({ 
			scrollTop: parseInt($('div.youtube_holder').scrollTop()) + parseInt($('div.youtube_holder').css('height'))
      }, 500 );
	}
	
	function changeYoutubeMovie(url){
		if($('#youtube_player').attr('id') != undefined){
			var so = new SWFObject(url, "mymovie", "300", "225", "8", "#000");
			so.write("youtube_player");
		}
	}
	
	function loadYoutubeMovie(url){
		if($('#youtube_player').attr('id') != undefined){
			var so = new SWFObject(url, "mymovie", "300", "225", "8", "#000");
			so.write("youtube_player");
		}
	}
	
	
	function changeMovie(url, width, height){
		var so = new SWFObject(url, "mymovie", width, height, "8", "#000");
		so.write("video_player");
	}
	
	var search_visible = false;
	var search_visible_wait = false;
	function inline_search(){
		// fix dropdown area
		
		$('#large_search_input').focus(function(){
			$('#large_search_criteria').slideDown('fast');
			$().mousedown(function(e){
				if(search_visible_wait == false){
					if(mouseWithin(e, $('#large_search_input_container')) || mouseWithin(e, $('#large_search_criteria')) || mouseWithin(e, $('#large_search_container'))){
						search_visible = true;
					}else{
						search_visible = false;
						$('#large_search_input').blur();
						$().unbind('mousedown');
					}
				}
			});
		});
		
		$('select')
			.focus(function(){ search_visible = true; search_visible_wait = true; })
			.blur(function(){ search_visible = true; search_visible_wait = false; });
		
		$('#large_search_input').blur(function(){
			if(search_visible == false){
				$('#large_search_criteria').slideUp('fast');
			}
		});
	
		// submit funtion
	
		$('#searchfield').submit(function(){
			var extrachunk = '';
			$('input[type!=radio],select,input[type=radio]:checked', '#searchfield').each(function(){
					if($(this).attr('name') != '' && $(this).val() != 'null' && $(this).val() != '' && $(this).val() != null){
						if(extrachunk != '')
							extrachunk += '/';
							
						extrachunk += $(this).attr('name') + '/' + $(this).val();
					}
			});
			$(this).attr('action', '/openingacts/search/' + extrachunk);
		});
	}
	
	function mouseWithin(e, obj){
		var boxTop = parseInt(obj.position().top) + parseInt($('#header_strip').css('height'));
		var boxHeight = parseInt(obj.css('height'));
		var boxLeft = parseInt($('#body_inner').position().left) + parseInt(obj.position().left);
		var boxWidth = parseInt(obj.css('width'));
		
		if(
			e.pageX < boxLeft || e.pageX > (boxLeft+boxWidth+10)
			||
			e.pageY < (boxTop+10) || e.pageY > (boxTop+boxHeight+10)
		){
			return false;
		}
		return true;
	}
	
	function scrollDown(obj){
		$('#'+obj).animate({ 
			scrollTop: parseInt($('#'+obj).scrollTop()) + parseInt($('#'+obj).css('height'))
      }, 500 );
	}
	
	function scrollUp(obj){
		$('#'+obj).animate({ 
			scrollTop: parseInt($('#'+obj).scrollTop()) - parseInt($('#'+obj).css('height'))
      }, 500 );
	}
	
	/*
		new layout scripts
	*/
	
	function hookNewLayout(){
		// fix top navigation
		//$('div.navigation div.menu').css('background-position','0px -'+(102 * parseInt($('div.navigation div.menu').attr('tag')))+'px');
		//$('div.inner_navigation div.menu').css('background-position','0px -'+(41 * parseInt($('div.inner_navigation div.menu').attr('tag')))+'px');
		$('div.inner_navigation div.submenu').css('background-position','0px -'+(41 * parseInt($('div.inner_navigation div.submenu').attr('tag')))+'px');
		
		$('div.share_navigation div').mouseover(function(){
			var hPos = 0;
			if($(this).attr('class') == 'facebook'){
				hPos = 59;
			}else if($(this).attr('class') == 'digg'){
				hPos = 59*2;
			}else{
				hPos = 59*3;
			}
			$(this).parent('div').css('background-position','0px -'+hPos+'px');
		});
		
		$('div.navigation div.menu div')
			.mouseover(function(){
				var hPos = parseInt($('div.navigation div.menu').attr('tag'));
				if($(this).attr('class') == 'home'){
					hPos = 102;
				}else if($(this).attr('class') == 'event'){
					hPos = 102*2;
				}else if($(this).attr('class') == 'oa'){
					hPos = 102*3;
				}else{
					hPos = 102*4;
				}
				$(this).parent('div').css('background-position','0px -'+hPos+'px');
			});
			
		$('div.inner_navigation div.menu div')
			.mouseover(function(){
				var hPos = parseInt($('div.inner_navigation div.menu').attr('tag'));
				if($(this).attr('class') == 'event'){
					hPos = 41;
				}else{
					hPos = 41*2;
				}
				$(this).parent('div').css('background-position','0px -'+hPos+'px');
			});
			
		$('div.inner_navigation div.submenu div')
			.mouseover(function(){
				var hPos = parseInt($('div.inner_navigation div.submenu').attr('tag'));
				if($(this).attr('class') == 'profile'){
					hPos = 41;
				}else if($(this).attr('class') == 'videos'){
					hPos = 41*2;
				}else if($(this).attr('class') == 'blog'){
					hPos = 41*3;
				}else{
					hPos = 41*4;
				}
				$(this).parent('div').css('background-position','0px -'+hPos+'px');
			});
			
		$('div.share_navigation div').mouseout(function(){  $(this).parent('div').css('background-position','0px 0px');	});
		$('div.navigation div.menu').mouseout(function(){  $(this).css('background-position','0px -'+(102*parseInt($(this).attr('tag')))+'px');	});
		$('div.inner_navigation div.menu').mouseout(function(){ $(this).css('background-position','0px -'+(41*parseInt($(this).attr('tag')))+'px');	});
		$('div.inner_navigation div.submenu').mouseout(function(){ $(this).css('background-position','0px -'+(41*parseInt($(this).attr('tag')))+'px');	});
	
		var leftOffset = $('div#body_inner').position().left;
		var topOffset = $('div.container').position().top;
	
		// icons
		$('div.tooltipbox, span.tooltipbox').mouseover(function(e){ if($(this).attr('tag') != undefined){ $('div.tooltip').html($(this).attr('tag')).show(); } });
		$('div.tooltipbox, span.tooltipbox').mousemove(function(e){ if($(this).attr('tag') != undefined){ $('div.tooltip').css('top', (e.pageY-topOffset-40)-parseInt($('div.tooltip').css('height'))).css('left', (((e.pageX-leftOffset)-15)-(parseInt($('div.tooltip').css('width'))/2))) } });
		
		$('div.tooltipbox, span.tooltipbox').mouseout(function(e){ $('div.tooltip').hide(); });
	
		// countdown
		if(typeof(countdownto) !== 'undefined'){
			setInterval("timeLeft()", 888);
		}
			
		// blogflow
		$('div[class*=blogflow]').bind('click', blogClick).css('cursor', 'pointer');
		
		// Header right links
		$('div.rightmenu a[class*=loadLayer]').bind('click', loadLayer);
		
		// start shuffle
		if(typeof($('div.event_holder').attr('class')) != 'undefined'){
			setInterval("shuffleEventImage()", "5000");
		}
		
		// radio buttons for signup etc
		$('div.radiobtn').click(function(ev){
			if($(this).children('input:radio').val() == 1){
				$(this).children('input:radio').attr('checked', false);
			}else{
				$(this).children('input:radio').attr('checked', true);
			}
			
			$('div.radiobtn').parents('form').find('input:radio[name='+$(this).children('input:radio').attr('name')+']').each(function(ev){
				if($(this).attr('checked') == false){
					$(this).parent().find('img').attr('src', $(this).parent().find('img').attr('src').replace('btn_radio_checked.', 'btn_radio.'));
				}else{
					$(this).parent().find('img').attr('src', $(this).parent().find('img').attr('src').replace('btn_radio.', 'btn_radio_checked.'));
				}
			});
		});
		
		// checkbox buttons for signup etc
		$('div.checkbtn').click(function(ev){
			if($(this).children('input:checkbox').attr('checked') == true){
				$(this).children('input:checkbox').attr('checked', false).attr('value', false);
				$(this).parent().find('img').attr('src', $(this).parent().find('img').attr('src').replace('btn_checkbox_checked.', 'btn_checkbox.'));
			}else{
				$(this).children('input:checkbox').attr('checked', true).attr('value', true);
				$(this).parent().find('img').attr('src', $(this).parent().find('img').attr('src').replace('btn_checkbox.', 'btn_checkbox_checked.'));
			}
		});
	}
	
	function saveImageCrop(e, mode){
		e.parents('div.image_preview').find('input[type=hidden]').val(e.parents('div.image_preview').find('div.image').scrollLeft() + '|' + e.parents('div.image_preview').find('div.image').scrollTop() + '|' + e.parents('div.image_preview').find('input[id$=ratio]').val());
		e.parents('div.image_preview').find('div.image').css('overflow', (mode == 'ok' ? 'hidden' : 'auto'));
	}
	
	function keepRatio(e){
		if(e.parent().children('input:checkbox').attr('checked')){
			e.parents('div.image_preview').find('img[name=work]').attr('height', parseInt(e.parents('div.image_preview').find('div.image').css('height')));
		}else{
			e.parents('div.image_preview').find('img[name=work]').removeAttr('height');
		}
	}
	
	function blogClick(obj){
		if($(this).data('open') == true){
			$(this).data('new', $(this).html());
			$(this).slideUp('normal', function(){ $(this).html($(this).data('old')).slideDown('normal'); } );
			$(this).data('open', false);
		}else{
			$(this)
				.slideToggle('', function(){
					if(typeof($(this).data('new')) != 'undefined'){
						$(this).html($(this).data('new')).slideDown('normal');
					}else{
						$(this).data('old', $(this).html());
						loadComponent($(this), 'loadcomponent', 'ajaxreadblog', $(this).attr('id'), $(this), 'slide');
					}
				})
			
			$(this).data('open', true);
		}
		
		//$(this).unbind('click', blogClick).css('cursor', '');
	}
	
	function loadLayer(){ //src, func, name, id
		if($(this).attr('class').indexOf('selected') == -1){
			$(this).parent().children().removeClass('selected');
			loadComponent('inner_'+$(this).parents('div.rightmenu').attr('tag')+'_holder', 'loadcomponent', 'ajaxread'+$(this).parents('div.rightmenu').attr('tag'), $(this).attr('id') + '|' + $(this).attr('tag'), 'hookLinks();');
			$(this).addClass('selected');
		}
	}

	function timeLeft(){
			var tl = new Date;
			sec = countdownto-parseInt(tl.getTime()/1000);
			
			r = sec;
			d = Math.floor((sec/3600/24));
			r -= (d*3600*24);

			h = Math.floor((r/3600));
			r -= (h*3600);

			m = Math.floor((r/60));
			r -= (m*60);
			
			s = r;
			
			$('div.countdown').html('<span class="big">'+d+'</span> day'+(d==1?' ':'s ')+'<span class="big">'+h+'</span> hour'+(h==1?' ':'s ')+'<span class="big">'+m+'</span> minute'+(m==1?' ':'s ')+'<span class="big">'+s+'</span> second'+(s==1?'':'s'));
	}

	function hookAdminContenders() {
		$("#all").change(function() {
			$(".contender").attr('checked',$(this).attr('checked'));
		});
		$("#updContenders").click(function(ev) {
			if(!confirm('Removing bands is irreversable, are you sure?')) {
				ev.stopPropagation();
				ev.preventDefault();
			}
		});
	}
	
	function shuffleEventImage(){
	

		if($('div.more_events_holder > div:visible').length <= 1){
			return
		}

		// update image
		$('div.event_holder > div.event_image img').attr('src', $('div.more_events_holder > div:visible:eq(0) img').attr('src').replace('eventimage', 'eventfullimage').replace('89/89', '620/249'));
		
		// update link
		$('div.event_holder').attr('tag', $('div.more_events_holder > div:visible:eq(0)').attr('tag'));
		
		// update topic
		var text = $('div.more_events_holder > div:visible:eq(0) > div.inner_row').attr('tag').split('|');
		$('div.event_holder > div.header_event_bar div.topic').html(text[0]);
		$('div.event_holder > div.header_event_bar div.subline').html(text[1]);
		
		// move and transfer event
		$('div.more_events_holder > div:visible:eq(0)').effect("transfer", {to: $('div.event_holder') }, 400).appendTo('div.more_events_holder');
	}
