jQuery(document).ready(function() {

/* ========================= */
/* ! EQUAL SIDEBAR HEIGHT    */
/* ========================= */

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


/* ========================================== */
/* ! POSITION SIDEBAR HEADERS BELOW IMAGES    */
/* ========================================== */

/*
if ($("img.sidebarimg").length) {
	$("h2.widgetheader").css({
		'margin-top' : '135px'
	});
	$("img.sidebarimg").css({
		'position' : 'absolute',
		'top': '0',
		'left':'0',
		'margin':'0'
	});
}
*/
		
/* ============ */
/* ! GALLERY    */
/* ============ */
	
	var item = 0; // Set index starting zero
	var galleryTotalItems = $('ul#gallerycontainer li').length-1;
	
	$("#gallerycredit").hide();
	
	$(".nav a.left").click( function() {
		if (item > 0) item--;
		slide(item);
		hideintro();
		return false;
	});
	
	$(".nav a.right").click( function() {
		if (item < $('ul#gallerycontainer li').length - 1 && item < galleryTotalItems) item++;
		slide(item);
		hideintro();
		return false;
	});			
						
	function slide(item) {
		var currentItemPosition = $('#gallery li:eq('+ item +')').position() ;
		$('ul#gallerycontainer').animate({
			left: -currentItemPosition.left
		});
		
		if (item == $('ul#gallerycontainer li').length-2 ) 
		{
			 loadMoreImages(5);
		}
		
		changeCredit(item);
	}

	function loadMoreImages(batch) 
	{
		var itemsInList = $('ul#gallerycontainer li').length-1
		for (i=itemsInList; i<(itemsInList + batch);i++)
		{
			loadImage(i);
		}
	}

	function loadImage(image)
	{			
		var newImage;
		$.get('http://www.trastockfestivalen.se/www/wp-content/themes/trastock2010/galleryitems.php',
		{
			reqImg: image
		},
		function(xml)
		{
			appendImage(image, xml);
			return;
		},'xml');
	}

	function appendImage(index, xml)
	{
		$('image', xml).each( function(i) {
			var image = $(this).text();
						
			var previmagepos = $('#gallery li:last').position();
			var currentimageWidth = $('#gallery li:last').width();
		
			$('ul#gallerycontainer').append('<li><'+ image +'></li>');
			var setPosition = previmagepos.left + currentimageWidth;
			$('#gallery li:last').css('left', setPosition);
		});
		
		galleryTotalItems = $('total', xml).text() -1;
	}

	// POSITION IMAGES
	var prevX = 0;
	var curWidth = 0;
	$('#gallery li').each( function(i) {
		if (!i<=0) {
			prevX = $('#gallery li:eq('+ (i-1) +')').position() ;
			curWidth = $('#gallery li:eq('+ (i-1) +')').width();
			pos = prevX.left + curWidth;
			$('#gallery li:eq('+ i +')').css('left', pos);
			//$('h1').append(" - " + pos);
		}
	});


	function hideintro() {
		$("#gallery .intro").slideUp();
		$("#gallery #gallerycredit").show();
	}
	
	function changeCredit(item) {
		var credit = ($('#gallery li:eq('+ item +') img').attr("title"));
		$("#gallery #gallerycredit").text(credit);
	}

	/* Gallery end */

// ======== 
// ! FLIP   
// ======== 

	
	if (!$.cookie('flip')) $("#prank #dontfuckwithme").after('<img src="www/wp-content/themes/trastock2010/images/prank_warning.png" alt="" width="200" height="180" />');
	if ($.cookie('flip')) $("#prank #dontfuckwithme").after('<img src="www/wp-content/themes/trastock2010/images/prank_mouahahaha.png" alt="" width="200" height="180" />');	
	$("#prank img").hide();
	
	$("#prank #dontfuckwithme").hover( function () {
		$("#prank img").fadeIn('fast');
	},
	function () {
		$("#prank img").fadeOut('fast');	
	});
	
	$("#dontfuckwithme").click(function() {
		$("#prank img").hide();
		$("#prank img").attr('src', 'www/wp-content/themes/trastock2010/images/prank_mouahahaha.png');
		flipsite();
		$.cookie('flip', 'true', { expires: 1 });
	});
	
	if ($.cookie('flip')) flipsite();

	function flipsite() {
		$('#pagewrap').css("-moz-transform", "scaleX(-1)");
		$('#pagewrap').css("-webkit-transform", "scaleX(-1)");		
		$('#pagewrap').css("filter", "fliph();");
		$('#pagewrap').css({"margin": "0 0 0 -40px"});
		$('#pagewrap').css("padding", "0");
		$('#header h1').css("display", "none");
	}


// ===============
// ! CLICKABLE BOX   
// ===============

	$(".clickablebox").mouseover(function() { 
		$(this).css('cursor', 'pointer');
	});
	$(".clickablebox").click(function() {
		var link = $(this).find('a').attr('href');
		window.location = link;
		
	});

});


