// variables

// listeners
$(document).ready(function() {
	loadPngFix();
	IE7fixes();
	IE6fixes();
	setContentMinHeight();
	setProgramMinHeight();
	setProgramAnimation();
	setNavigatioAnimation();
	setFlashContent();
	setCommentForm();
	scrollHistory();
	imageGallery();
});
$(window).resize(function() {
	setProgramMinHeight();
});

// functions
function IE7fixes(){
	if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) == '7') {
		var clearer = $('<div/>',{'class':'clear'});
		clearer.insertBefore('#more');
		clearer.insertBefore('#paging');
		clearer.insertBefore('#pro div.even');
		clearer.insertBefore('#pro div.odd');
	}
}
function IE6fixes(){
	if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) == '6') {
		var clearer = $('<div/>',{'class':'clear'});
		clearer.insertBefore('#pro div.even');
		clearer.insertBefore('#pro div.odd');
		
		clearer.insertAfter('#base #content');
		var margin = (12 - $('#index').height())+'px';
		$('#base #content:has(.imgs)').css({
			'margin-top':margin,
			'z-index':'99999'
		})
		var margin = (22 - $('#index').height())+'px';
		$('.history #base #content').css({
			'margin-top':margin,
			'z-index':'99999'
		})
		
		var more = $('#more:not(.band #more)').detach();
		more.insertAfter('#sidebar');
		clearer.insertBefore('#more');
		
		var paging = $('#paging').detach();
		paging.insertAfter('#sidebar');
		clearer.insertBefore('#paging');
		
		$('#header #nav li.drop').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
	}
}
function imageGallery() {
	if ($('.imgs').height() != null) {
		$.getScript("/2010/js/jquery.fancybox-1.3.0.pack.js", function() {
			$('.imgs:first a').fancybox( {
				padding : 1,
				autoScale : true,
				centerOnScroll : true
			});
		});
	}
}
function scrollHistory() {
	var timeline = $('.history #timeline');
	var list = timeline.find('ul');
	var tWidth = timeline.width();
	var lWidth = list.width();
	timeline.mousemove(function(e) {
		timeline = $(this);
		var tPosition = e.pageX - timeline.offset().left;
		var lPosition = 0 - parseInt((tPosition / tWidth)
				* (lWidth - tWidth + 100));
		list.css( {
			left : lPosition
		});
	});
}
function setCommentForm() {
	var form = $(".band #comments form");
	var fieldset = form.find('fieldset');
	form.hide();
	if(!$.support.cssFloat){ // IE only because of bad font rendering after animation
		fieldset.find('label').css('background-color','#f4f2ef');
	};
	fieldset.fadeTo(0, .1)
	$(".band a#more").removeAttr('href').css( {
		cursor : 'pointer',
		textDecoration : 'underline'
	}).click(function() {
		form.slideDown('normal');
		fieldset.fadeTo('slow', 1);
		var top = $(this).offset().top;
		$('html,body').animate( {
			scrollTop : top
		});
	});
}
function loadPngFix() {
	if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) == '6') {
		$.getScript("js/jquery.supersleight.js", function() {
			$('body').supersleight();
		});
		$('body')
		.prepend(
				$(
						'<a/>',
						{
							id : 'IE6warning',
							text : 'You are using an outdated browser. Click here and update for free.',
							href : 'http://www.microsoft.com/windows/Internet-explorer/default.aspx',
							target : '_blank'
						}));
		$('#IE6warning').hide().delay(1000).slideDown();
	}
	if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) == '7') {
		$('body')
				.prepend(
						$(
								'<a/>',
								{
									id : 'IE6warning',
									text : 'You are using an older browser. For better performance and experience, click here and update for free.',
									href : 'http://www.microsoft.com/windows/Internet-explorer/default.aspx',
									target : '_blank'
								}));
		$('#IE6warning').hide().delay(1000).slideDown();
	}
}
function setContentMinHeight() {
	var sidebar = $('#sidebar');
	var content = $('#content');
	var sHeight = sidebar.height() + 40;
	var cHeight = content.height();
	if (cHeight < sHeight) {
		content.height(sHeight);
	}
}
function setProgramMinHeight() {
	var pro = $('#pro');
	pro.css('height', 'auto');
	var pHeight = pro.height();
	var wHeight = $(window).height();
	var bHeight = $('body').height();
	var diff = wHeight - bHeight;
	if (diff > 0) {
		pro.height(pHeight + diff)
	}
	pro.find('div.odd,div.even').each(function(){
		var bOne = $(this).find('div:nth-child(2)');
		var bTwo = $(this).find('div:nth-child(3)');
		var hOne = bOne.height();
		var hTwo = bTwo.height();
		if(hOne<hTwo){hOne=hTwo}
		bOne.height(hOne);
		bTwo.height(hOne);
	});
	if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= '6'){}else{
		pro.find('div.odd').each(function(){
			var block = $(this);
			if(!block.hasClass('first')){
				block.append($('<span/>',{style:'display:block;position:absolute;z-index:999999;width:5px;height:5px;background-color:#f4f2ef;top:0;left:0;'}));
			}
			if(!block.hasClass('last')){
				block.append($('<span/>',{style:'display:block;position:absolute;z-index:999999;width:5px;height:5px;background-color:#f4f2ef;bottom:0;left:0;'}));
			}
		});
	}
}
function setProgramAnimation() {
	var prog = $('#prog');
	var curr = 1;
	var aNext = $('.programme_buttons a.next');
	var aPrev = $('.programme_buttons a.prev');
	if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= '7') {
		var prevHeights = 0;
		var forFocus = $('#day' + curr);
		forFocus.prevAll().each(function() {
			prevHeights += ($(this).height() + 16);
		});
		prevHeights = (prevHeights + 1) + 'px';
		prog.animate({
			'scrollTop' : prevHeights
		});
	}
	aNext.removeAttr('href').css('cursor', 'pointer').click(function() {
		if (curr < 10) {
			curr++;
			var prevHeights = 0;
			var forFocus = $('#day' + curr);
			forFocus.prevAll().each(function() {
				prevHeights += ($(this).height() + 16);
			});
			prevHeights = (prevHeights + 1) + 'px';
			prog.animate({
				'scrollTop' : prevHeights
			});
		} else {
			curr = 10;
		}
	});
	aPrev.removeAttr('href').css('cursor', 'pointer').click(function() {
		if (curr > 1) {
			curr--;
			var prevHeights = 0;
			var forFocus = $('#day' + curr);
			forFocus.prevAll().each(function() {
				prevHeights += ($(this).height() + 16);
			});
			prevHeights = (prevHeights + 1) + 'px';
			prog.animate({
				'scrollTop' : prevHeights
			});
		} else {
			curr = 1;
		}
	});
}
function setNavigatioAnimation() {
	if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= '7') {
		
	}else{
		var drops = $('#nav li.drop');
		drops.each(function() {
			var drop = $(this);
			var ul = drop.find('ul');
			ul.append($('<li/>',{
				"class":"ender"
			}));
			//var ender = ul.find('li.ender:last');
			drop.hover(function() {
				if ($(this).is(':not(.busy)')) {
					var drop = $(this);
					var ul = drop.find('ul');
					var lis = ul.find('li');
					drop.addClass('busy')
					lis.each(function() {
						var li = $(this);
						if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= '7') {
							li.show();
						}else{
							li.hide().fadeIn('fast');
						}
					});
					drop.css('bottom', '12px').delay(10).animate( {
						bottom : '30px'
					}, 'fast');
					ul.hide().delay(10).slideDown('fast');
				}
			}, function() {
				if ($(this).is('.busy')) {
					var drop = $(this);
					var ul = drop.find('ul');
					var lis = ul.find('li');
					lis.each(function() {
						var li = $(this);
						if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= '7') {
						}else{
							li.show().delay(10).fadeOut();
						}
					});
					drop.animate( {
						bottom : '12px'
					}, 'normal', 'linear', function() {
						$(this).removeClass('busy');
						lis.each(function() {
							var li = $(this);
							if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= '7') {
								li.hide();
							}
						})
					});
					ul.slideUp();
				}
			});
		});
	}
}
function setFlashContent() {
	$('#flash').append($('<div/>', {
		id : 'topFlash'
	}));
	$('#player').append($('<div/>', {
		id : 'fPlayer'
	}));
	$('#sponsor').append($('<div/>', {
		id : 'sFlash'
	}));
	
	var flashvars = {};
	var params = {
		wmode : 'transparent',
		scale : 'noscale',
		menu : 'false',
		allowFullScreen : 'false'
	};
	var attributes = {};
	swfobject.embedSWF("/2010/swf/sponsor.swf", "sFlash", "139", "52", "9.0.0", "",flashvars, params, attributes);
	
	flashvars = {
			JUKEBOX_XML_PATH :  "/2010/xml/Jukebox.xml"
		};
	swfobject.embedSWF("/2010/swf/jukebox.swf", "fPlayer", "350", "129", "9.0.0", "",flashvars, params, attributes);
	
	flashvars = {
		FEATURED_ARTIST_XML_PATH :  "/2010/xml/FeaturedArtists.xml"
	};
	swfobject.embedSWF("/2010/swf/featuredArtists.swf", "topFlash", "495", "256", "9.0.0","", flashvars, params, attributes);
}
