var block_width = 150;
var block_height = 50;

var styles = new Array(
	'blank',
	'blank',
	'white show pattern',
	'blank',
	'blank',
	'black show pattern',
	'blank',
	'blank',
	'blank',
	'blank',
	'white show pattern',
	'black show pattern'
);

$(document).ready(function() {

	$('.page-contents').removeClass('hidden');

	amend_heights($(".block"));
	
	$(".release-grid li a span, ul.links li.block a, .block a, .block b").wrapInner("<span />");

	$(".release-grid li:odd a .block, .headlines li:odd, .events li:odd .block.link, .store li:odd .block.link").removeClass("maya-blue");
	$(".release-grid li:odd a .block, .headlines li:odd, .events li:odd .block.link, .store li:odd .block.link").addClass("pacific-blue");
	
	//$(".block a, .block b").wrapInner("<span />");

	$("ul.buy-links li:odd, .store li .button:odd").removeClass("razzmatazz");
	$("ul.buy-links li:odd, .store li .button:odd").addClass("firebrick");
	
	// events
	amend_heights($(".event-info .address, .event-infp"));
	equal_heights($(".events li"));

	// store
	amend_heights($(".store-item-info .notes, .store-item-info"));
	equal_heights($(".store li"));
	
	$(".panel .article p, .events li p, .store li p").wrapInner("<span />");
	$(".panel .article h3").wrapInner("<em />");

	amend_heights($(".panel .article h3, .panel .article p"));	
	amend_heights($(".block, .panel, .event-info"));

	toggleClasses('.headlines li a', '.headlines li a#news-menu0', 'active');
	toggleElements('.articles', '#news0');
	amend_heights($(".embed .article"));


	// get width & height of page
	
	var page_width = Math.floor($(".page-contents").width()/block_width);
	var page_height = Math.floor($(".page-contents").height()/block_height);

	randomize_blocks(page_width*page_height);
	amend_heights($(".mosaic .block"));

	//fadeElements();
	$('#newsletter').css({ visibility: "hidden" }).css({ visibility: "visible" });

	$('#topOfPage').click(function(e) {
		e.preventDefault();
		$('html,body').stop(true).animate({scrollTop:0},500);
	});

/*	jQuery.ajax({
		async: false,
		type: "GET",
		url: "http://analytics.sonymusic.com/ga/js",
		data: null,
		dataType: 'script'
	});*/
	
	// buttons, behaviours
	
	$(".logo, .copyright").mouseenter(function() {
		$(".mosaic div:first-child").remove();
		style = styles[Math.round(Math.random()*styles.length)];
		$('.mosaic').append('<div class="block '+style+'" style="height: '+block_height+'px">&nbsp;'+'</div>');
	});
	
	// organise button fades

	$(".release-grid li a span.block").each(function() {
		$(this).parent().css({'background-color': $(this).css('background-color')});
	});

	var color_temp = '';
	$(".block a, .release-grid li a span.block").not(".article .block a").mouseenter(function() {
		color_temp = $(this).parent().css('background-color');
		$(this).css({'background-color': color_temp});
		$(this).animate({backgroundColor: '#ffffff'}, 1);
	}).mouseleave(function() {
		$(this).animate({backgroundColor: color_temp},250);
	});

	$(".article .block a").not(".active").mouseenter(function() {
		color_temp = $(this).parent().css('background-color');
		$(this).css({'background-color': color_temp});
		$(this).not(".active").animate({
			backgroundColor: '#000000',
			color: '#ffffff'
		}, 1);
	}).mouseleave(function() {
		$(this).animate({
			backgroundColor: color_temp,
			color: '#000000'
		}, 250);
	}).mousedown(function() {
		$(this).animate({
			backgroundColor: color_temp,
			color: '#000000'
		}, 250);
	});


	
});

$(window).scroll(function() {
});


function amend_heights(group) {

	// snaps each element of group to height block_height
	group.each(function() {
		new_height =  block_height * Math.ceil($(this).height()/block_height);
		$(this).height(new_height);
	});

}

function randomize_blocks(block_count) {

	for(i=0; i<block_count; i++) {
		style = styles[Math.round(Math.random()*styles.length)];
		$('.mosaic').append('<div class="block '+style+'">&nbsp;'+'</div>');
	}

}

function toggleElements(group_name, active_item) {
	$(group_name).children().hide();
	$(active_item).show();
}

function toggleClasses(group_name, active_item, class_name) {
	$(group_name).removeClass(class_name);
	$(active_item).addClass(class_name);
}

function fadeElements() {

	$.fn.delay = function(time, callback) {
		return this.animate({ opacity: '+=0' }, time, callback);
	}

	// fade panels up
	$('.mosaic .block, .page-contents .block, .page-contents .panel').css({'display':'block','visibility':'visible','opacity':0});
	$('.page-contents').removeClass('hidden');

	var delayMS = 0;
	
	var i=0;

	$('.mosaic .block.show').each(function() {
		delayMS = (i*15);
		$(this).delay(delayMS).animate({opacity:1}, 150);
		i++;
	});
	i=75;	
	$('.page-contents .block, .page-contents .panel').each(function() {
		delayMS = (i*15);
		$(this).delay(delayMS).animate({opacity:1}, 150);
		i++;
	});

}

function equal_heights(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}

	});
	group.height(tallest);
}
