//crunchtime slider images
/*
showcases	 =  new Array();

showcases[0] = "cts2010";
showcases[1] = "1";
showcases[2] = "2";

slider_details		= new Array();

slider_details[0]	= "Crunch Time Sports";
slider_details[1]	= "Philthy Phil's Cornhole Tournament";
slider_details[2]	= "May Madness Softball Tournament";

link_details	= new Array();
link_details[0]	= "/";
link_details[1]	= "/leagues#philthy-phils";
link_details[2]	= "/tournaments#may-madness-softball-tournament";
*/


// preload
$(window).bind('load', function() {

    var preload = new Array();

    $(showcases).each(function(i){
    	//s = "/images/slider/"+showcases[i]+".png";
		s = ""+showcases[i]+"";
		//s = showcases[i];
    	preload.push(s);
    });
    
    var img = document.createElement('img');
    $(img).bind('load', function() {
        if(preload[0]) {
            this.src = preload.shift();
        }
    }).trigger('load');

});


var month_count = 0;

$(document).ready(function() {
	$('.article-head').hover(
		function () {
			$(this).children('h1').addClass('over');
		}, 
		function () {
			$(this).children('h1').removeClass('over');
		}
	);
	$('.article-head').click(function() {
		$(this).siblings('.article-content').slideToggle();
	});

	$('h1.header').click(function() {
		$(this).siblings('.article-wrap').slideToggle();
		$(this).toggleClass('open');
	});

	$('h1.tourney').click(function() {
		$(this).siblings('.article-category').slideToggle();
		$(this).toggleClass('open');
	});


	$('.change-month').click(function() {
		$('#calendar-month').hide();
		$('#calendar-loader').show();

		var type = (($(this).attr('id') == "calendar-left") ? "prev" : "next");
		if(type == "prev") { month_count--; } else { month_count++; }
		
		$.post("index.php?calendar/fetch_calendar", { type: type, count: month_count }, function(data){
			var obj = $.evalJSON(data);
			$('#calendar-month').html(obj.month);
			$('#calendar-wrap').html(obj.table);
			$('#calendar-month').show();
			$('#calendar-loader').hide();
		});

	});

	showcase_switch_time = 4000;
	showcase_fade_time = 900;
	showcase_switch_first_time = 1000;
	
	$(document).oneTime(showcase_switch_first_time,"showcase_switch_first",function(i) {
		
		start_showcase();

	});
	
	function stop_showcase() {
		$(document).stopTime("showcase_switch");
		$(document).stopTime("showcase_switch_first");
	}


	function start_showcase(time) {
		
		if (time) {
			switch_time = time;
		} else {
			switch_time = showcase_switch_time;
		}
		
		$(document).everyTime(switch_time, "showcase_switch", function(i) {
			$('#showright').click();
		},0,true);
	}

	$('#showright').click(function(e) {
		move_showcase(1);
		if (e.pageX) {
			stop_showcase();
		}
	});

	$('#showleft').click(function(e) {
		move_showcase(-1);		
		if (e.pageX) {
			stop_showcase();
		}
	});

	function move_showcase(direction) {
		
		$('#showup').stop(true,true);
		$('#shownext').stop(true,true);
		$('#showlabel').stop(true,true);
		
		// get key of current _over
		//id = $('#showup').attr('src').replace("/emporium/wp-content/uploads/",'').replace(/.png/,'');
		id = $('#showup').attr('src');
		
		// lets take direction and find next
		next = $.inArray(id,showcases)+direction;

		// assign the next one in array
		
		if (direction > 0) { // moving right
			// check if was the last one and whether we should restart
			if (next >= showcases.length) {
				next = 0;
			}
			background_direction = "";
		} else { // moving left
			// check if was the first one we should go to end
			if (next < 0) {
				next = showcases.length-1;
			}
			background_direction = "-";
		}
		
		next_id = showcases[next];
		//next_background_src = "/images/slider/"+showcases[next]+".png";
		next_background_src = showcases[next];
		next_image_title = slider_details[next];
		next_image_link = link_details[next];
		$('#shownext').attr('src',next_background_src);
		$('#showlabel').html(next_image_title);
		$('#slider_link').attr('href',next_image_link);
		$('#showup').animate({opacity:0},showcase_fade_time,"swing",function() {
			$('#showup').attr('src',next_background_src);
			$('#showup').animate({opacity:1},{duration:0});
		});

	}
});