jQuery(function($){

 	/* Fancy, magic navigation dude */
 	var $el, leftPos, newWidth,
        $mainNav = $("#nav");

    $mainNav.append("<li id='magicArrow'></li>");
    var $magicLine = $("#magicArrow");

    if ($(".current_page_item").length > 0) {
	    $magicLine
	        .width($(".current_page_item").width())
	        .css("left", $(".current_page_item a").position().left)
	        .data("origLeft", $magicLine.position().left)
	        .data("origWidth", $magicLine.width());
    }

    $("#nav li a").hover(function() {
        $el = $(this);
        leftPos = $el.position().left;
        newWidth = $el.parent().width();
        $magicLine.stop().animate({
            left: leftPos,
            width: newWidth
        });
    }, function() {
        $magicLine.stop().animate({
            left: $magicLine.data("origLeft"),
            width: $magicLine.data("origWidth")
        });
    });
    
    
    
    
    
    
    
    /* Fancy, magic navigation dude */
 	var $el, leftPos, newWidth,
        $mainNav = $("#event-buttons");

    $mainNav.append("<li id='magicArrow'></li>");






	/*
	//sticker fixed position
	var top = $("#mashup-btn a").offset().top - parseFloat($("#mashup-btn a");
		
	$(window).scroll(function(event) {
    	
		// what the y position of the scroll is
    	var y = $(this).scrollTop();

    	// whether that's below the form
    	if (y >= top) {
      		
			// if so, ad the fixed class
      		$("#mashup-btn a").css({
      			position: 'fixed',
      			top: '20px',
      			left: 'auto',
      			right: '20px',
      			margin: 0,
      		});
    	
		} else {
      		
			// otherwise remove it
      		$("#mashup-btn a").css({
      			"position":"absolute",
      			margin: '-38px 0 0 -141px',
      			top: '50%',
      			left: '50%',
      			right: 'auto'
      		});
    	}
		});
*/


	//galleria
	$("#podcast-slides").galleria({
		preload: 3,
		height: 125,
		width: 225,
		image_crop: "width",
		image_position: "center",
		transition: "slide", //fade, flash, slide, fadeslide
		transition_speed: 800,
		autoplay: false
	});

	//galleria
	//initialize flickr plugin api key
    var api_key = "65b80ce96804e3376962384d44756e3e"; // get your API key at Flickr
    var flickr = new Galleria.Flickr(api_key);

	flickr.setOptions({
	    max: 10,
	    size: "big",//small,big,original
	    sort: "date-posted-asc" //doesn't work - see "RAM" in flickr js
	});

	//flickr set
	//flickr.getSet("72157625955446064", function(data) {
	flickr.getSet("72157626501981000", function(data) {
	    $("#banner-slides").galleria({
	        data_source: data,
			preload: 3,
			height: 320,
			width: 960,
			image_crop: true,
			transition: "slide", //fade, flash, slide, fadeslide
			transition_speed: 400,
			autoplay: false
	    });
	});
	
	



	//events
	//evts();

	
	//click
	/*$("#event-status ul li").click(function() {
		o = $(this);
		i = $("#event-status ul li").index(o);
		evtsPlay = false;
		clearTimeout(evtsTimer);
		evts(i);
	});*/
	
	
	//hover
	$(".sidebar-nav ul li a div").hover(
		function () {
	    	$(this).css("background-position","0 -31px");
		}, 
	  	function () {
	    	$(this).css("background-position","0 0");
	  	}
	);
	
	
	// modifying subscribe
	var subscribePs = $('.subscribeCont p');
	if ( subscribePs.length > 1 ) {
		var realP = subscribePs[1];
		$(realP).prepend( $('input' , subscribePs[0]) );
		$('input[type=submit]', realP).val('GO');
		$(subscribePs[0]).hide();
	}
	
/*
	// modifying countdown
	var countdownCont = $('.fergcorp_countdownTimer_event_li');
	$('#focus .inner').append( '<div id="countDown">' + countdownCont.html() + '</div>' );
	$('#countDown').css( {
		'position' : 'absolute',
		'bottom' : '3px',
		'right' : '10px'
	} );
*/
	

});


function evts(c) {
	if(c !== undefined) { evtsCurr = c; } 

	$("#event-status ul li").removeClass("active");
	$("#event-status ul li:eq("+evtsCurr+")").addClass("active");
	
	
	$("#header #event-status #event-status-content div").hide().html(evtsContent[evtsCurr]).fadeIn();


	if(evtsCurr == (evtsTotal-1)) { evtsCurr = 0; } else { evtsCurr = evtsCurr + 1; }		

	if(evtsPlay) {
		evtsTimer = setTimeout("evts()", 5000);
	}
}


/**
 * Pad a number with leading zeroes
 * so the resulting string is "length" length
 */
function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}




/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];
 
  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}



/* Whazzup stuff */
var evtsContent = new Array();
var evtsBtn = new Array();

jQuery(function($) {
	var i;
	if (whazzupIcons == "")
		whazzupIcons = "0,2,1,3";
	whazzupIcons = whazzupIcons.split(",");	// e.g. ["0","2","1","3"]

	i = 0;
	$("#event-status-content div.event-status-content-part").each(function(){
		$(this).attr("id", "event-status-content-" + i);
		if (!$(this).attr("rel")) {
			$(this).attr("rel", "0");
			evtsBtn.push(whazzupIcons[i]);	//evtsBtn.push(i); //evtsBtn.push(0);
		}
		i ++;
	});

	evtsBtnHtml = '';
	for (i = 0; i < evtsBtn.length; i ++) {
		var active = i==0 ? "active" : "";
		evtsBtnHtml += '<li class="' + active + '"><a href="#" style="background-image:url(\'' + whazzupIconPath + evtsBtn[i] + '.png\')"></a></li>';
/*
		if (i == 0) {
			evtsBtnHtml += '<li class="icon' + evtsBtn[i] + ' active"><a href="#">Twitter</a></li>';
		} else if (i == 1) {
			evtsBtnHtml += '<li class="icon' + evtsBtn[i] + '"><a href="#">Webcam</a></li>';
		} else if (i == 2) {
			evtsBtnHtml += '<li class="icon' + evtsBtn[i] + '"><a href="#">Rankings</a></li>';
		} else if (i == 3) {
			evtsBtnHtml += '<li class="icon' + evtsBtn[i] + '"><a href="#">Weather conditions</a></li>';
		} else {
			evtsBtnHtml += '<li class="icon' + evtsBtn[i] + '"><a href="#">meh</a></li>';
		}
*/
	}
	$("#event-buttons").html(evtsBtnHtml);

	evtsTotal = evtsBtn.length;	// update it to the right value.
	//events
	evts_mod(0);
	
	
	//click
	$("#event-status ul li").click(function() {
		var i;
		o = $(this);
		i = $("#event-status ul li").index(o);
		evtsPlay = false;
		clearTimeout(evtsTimer);
		evts_mod(i);
	});
});

var evtsTotal = evtsContent.length;
var evtsCurr = 0;
var evtsPlay = false;
var evtsTimer;

function evts_mod(c) {
	if(c !== undefined) { evtsCurr = c; }

	jQuery("#event-status ul li").removeClass("active");
	jQuery("#event-status ul li:eq("+evtsCurr+")").addClass("active");
	
	jQuery("#event-status-content div.event-status-content-part").hide();
	jQuery("#event-status-content-" + evtsCurr).fadeIn();
	if(evtsCurr == (evtsTotal-1)) { evtsCurr = 0; } else { evtsCurr = evtsCurr + 1; }		
}


