/* Author: 
*/

/** 
* Apply Cycle Pligin to single portfolio page
* Insert pagination after figure element if there is more than one image.
**/

$(window).load(function() {

function url_id() {
	var url = window.location.hash;
	var id = url.substring(url.lastIndexOf('#') + 10);
	if( id != '' ) {
		//alert( 'ID: ' + id );
		return id - 1;
	} else {
		//alert('null');
		return 0;
	}
}

$(window).bind('hashchange', function() {
	var url = window.location.hash;
	var id = url.substring(url.lastIndexOf('#') + 10) - 1;
	var id = window.location.hash.replace( "#calendar_", "0");
	var id = parseFloat( id );
    $('div#advent_calendar_gallery').cycle( id - 1 );
    //$('div#advent_calendar_gallery').ToMeToYou(2);
}); 

$(window).bind('hashchange', function() {
	var url = window.location.hash;
	var id = url.substring(url.lastIndexOf('#') + 10) - 1;
    $('div#advent_calendar_gallery').cycle(id);
    //$('div#advent_calendar_gallery').ToMeToYou(2);
}); 

function ToMeToYou( me ){
	alert(me);
	var you = me + 1;
	$('div#advent_calendar_gallery').cycle(you);
};

$('div#advent_calendar_gallery').cycle({
	//fx:     'scrollHorz', 
	fx:     'fade', 
    speed:   500, 
    timeout: 0, 
	next:   '#pagination_next',
	prev: 	'#pagination_prev',
    pause:   1,
	pager:  '#advent_calendar_nav span',
    after: function( currentSlide, nextSlide  ){
    	if(history.pushState) {
	 	   	history.pushState(null, null, '#' + nextSlide.id );
		} else {
		    location.hash = nextSlide.id;
		}
    	var slideID = nextSlide.id;
    },
	startingSlide: url_id()
});

$('.fadeThis1').append('<span class="hover1"></span>').each(function(){var $span=$('> span.hover1',this).css('opacity',0);$(this).hover(function(){$span.stop().fadeTo(500,1);},function(){$span.stop().fadeTo(750,0);});});
$('.fadeThis2').append('<span class="hover2"></span>').each(function(){var $span=$('> span.hover2',this).css('opacity',0);$(this).hover(function(){$span.stop().fadeTo(500,1);},function(){$span.stop().fadeTo(750,0);});});
$('.fadeThis3').append('<span class="hover3"></span>').each(function(){var $span=$('> span.hover3',this).css('opacity',0);$(this).hover(function(){$span.stop().fadeTo(500,1);},function(){$span.stop().fadeTo(750,0);});});
$('.fadeThis4').append('<span class="hover4"></span>').each(function(){var $span=$('> span.hover4',this).css('opacity',0);$(this).hover(function(){$span.stop().fadeTo(500,1);},function(){$span.stop().fadeTo(750,0);});});
$('.fadeThis5').append('<span class="hover5"></span>').each(function(){var $span=$('> span.hover5',this).css('opacity',0);$(this).hover(function(){$span.stop().fadeTo(500,1);},function(){$span.stop().fadeTo(750,0);});});
$('.fadeThis6').append('<span class="hover6"></span>').each(function(){var $span=$('> span.hover6',this).css('opacity',0);$(this).hover(function(){$span.stop().fadeTo(500,1);},function(){$span.stop().fadeTo(750,0);});});

$("#advent_calendar_gallery a").fancybox();
$('#gallery .entry a').fancybox();

});
