$(document).ready(function(){
	/* extend jquery :random */
	jQuery.jQueryRandom = 0;
	jQuery.extend(jQuery.expr[":"],
	{
	    random: function(a, i, m, r) {
	       if (i == 0) {
	            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
	        };
	        return i == jQuery.jQueryRandom;
	    }
	});
	
	/* funky boxes */
	var colors = ['000000','FADC75','1C4319','A8D2CF','A8C3A2','49613D','D81F26','AA0F14','FDBF21','67B3AE', 'F9B5AC'],
	colorCount = colors.length;
	function makeBoxes(){
		var width = $(document).width(), height = $(document).height(); 
		boxCount = Math.round(width/94)*Math.round(height/94)-30;
		for(i=0; i < boxCount; i++){
		  $('body').prepend('<div class="box" style="background-color:#'+colors[Math.floor(Math.random()*colorCount)]+'"/>');
		}
	}
	function boxChange(){
		$('.box:random').animate({backgroundColor:'#fff'}, 750).animate({backgroundColor: '#'+colors[Math.floor(Math.random()*colorCount)]}, 2000);
	}
	makeBoxes();
	setInterval(boxChange, 500);

	/* funky item */
	var itemClasses = ['one', 'two', 'three', 'four', 'five'],
	itemClassCount = itemClasses.length;
	$('.item').each(function() {
		$(this).addClass(itemClasses[Math.floor(Math.random()*itemClassCount)]);
	});
	
	/* funky campaign */
	function campaignBow(){
		$('#campaign').animate({backgroundColor: '#'+colors[Math.floor(Math.random()*colorCount)]}, 500);
	}
	setInterval(campaignBow, 5000);
	
	/* dynamic text */
	function dynamicTextUrl(text, textFormat) {
		text = text.replace(/\?/g,'ENCODEDQUESTION')
		text = text.replace(/\#/g,'ENCODEDHASH')
		text = text.replace(/\//g,'&#47;')
		text = encodeURIComponent(text);
		text = text.replace(/%26amp%3B/g,'%26') // fugly hack
		text = text.replace(/\./g,' ') // another one
		return("/dynamic_text/"+textFormat+"/"+text+".png");
	}
	
	function dynamicTextReplace(elmnt, text, textFormat) {
		if(!elmnt.replacementImage) {
			elmnt.replacementImage = document.createElement("img");
			elmnt.innerHTML = "";
			elmnt.appendChild(elmnt.replacementImage);
		}
		elmnt.replacementImage.src = dynamicTextUrl(text, textFormat);
		elmnt.replacementImage.alt = text;
	}
	$('.item h3 a, .large h3 a').each(function(){
		dynamicTextReplace(this, this.innerHTML, "sub_larger_2010");
		$(this).css({lineHeight:'1px'});
	});
	$('.medium h3 a').each(function(){
		dynamicTextReplace(this, this.innerHTML, "sub_shorter_2010");
		$(this).css({lineHeight:'1px'});
	});	
	$('h2').each(function(){
		dynamicTextReplace(this, this.innerHTML, "sub_2010");
		$(this).css({lineHeight:'1px'});
	});
	$('h1').each(function(){
		dynamicTextReplace(this, this.innerHTML, "head_2010");
		$(this).css({lineHeight:'1px'});
	});
	var menuLinks = '#menu li a';
	$(menuLinks).each(function(){
		this.linkText = this.innerHTML;
		if($(this).hasClass('current')) {
			dynamicTextReplace(this, this.linkText, "menu_over_2010");
		}
		else {
			dynamicTextReplace(this, this.linkText, "menu_2010");
		}
		$(this).css({lineHeight:'1px'});
	});
	$(menuLinks).hover(
		function(){ dynamicTextReplace(this, this.linkText, "menu_over_2010"); },
		function(){ dynamicTextReplace(this, this.linkText, "menu_2010"); }
	);
	
	/* height fixes */
	var fixHeight = function(){
		var fixedHeight = $(document).height()+302;
		/* $('#menu').css({height: fixedHeight}); */
		$('#content').css({height: fixedHeight+40});
		$('#artists, #sidebar').css({height: fixedHeight+177});
	}
	fixHeight();
	
	/* miscellaneous */
	// $('#newsletter .text').click(function(){$(this).val('');});
	$('#tweets.oya').liveTwitter('øya OR øyafestival OR øyafestivalen OR oyafestival OR oyafestivalen',{limit: 5});
	$('#submenu a:odd').css({color:'#ffc000'});
	$('#artists ul a:odd').css({color:'#F58F8E'});
	
	// Artists column
	window.resizeArtists = function(newHeight, artistLevel){ 
		$('#artistsFlash'+artistLevel).each(function(){ 
			this.style.height = newHeight+"px";
			fixHeight();
			/*
			var minDocHeight = $('#artists-flash').position().top + newHeight;
			jQuery.each(jQuery.browser, function(i) {
				if($.browser.msie){
					$("#tile").css("height", minDocHeight+"px");
				}else{
					$("#tile").css("min-height", minDocHeight+"px");
				}
			});
			*/
		}); 
	}
	// flash
	var params = window.flashParams;	
	$('#artistsFlashHeadliner').each(function() {
		params['showLevel'] = 'Headliner';
		swfobject.embedSWF("/flash/artists.swf", this.id, 236, 100, "7.0.0", "", params, { bgColor: "1b4219" })
	});
	$('#artistsFlashNormal').each(function() {
		params['showLevel'] = 'Normal';
		swfobject.embedSWF("/flash/artists.swf", this.id, 236, 100, "7.0.0", "", params, { bgColor: "1b4219" })
	});
	$('#artistsFlashØyanatt').each(function() {
		var params = window.flashParams;
		params['showLevel'] = 'Øyanatt';
		params['oyaNatt'] = 'true';
		swfobject.embedSWF("/flash/artists.swf", this.id, 236, 100, "7.0.0", "", params, { bgColor: "1b4219" })
	});

	$('#friends .friend_feed:last').css({borderBottom:'none', 'padding-bottom':'10px'});
});

