/*
 * (C) elixir productions 2010
 * Created By: Jason Howmans (jasonhowmans.com)
 *
 * JS Application
 */

function shrinkHeading(H1element) {
	if (H1element.text().length > 50) {
		H1element.find('a').css('font-size', '15px');
	}
}

$(document).ready( function() {
	
	/*
	 * Initiate Fancybox
	 */
	$(".fancybox").fancybox({
		'hideOnContentClick': true
	});
	
	/*
	 * Blog Actions
	 */
	var initLoad = [];
	$('.searchContain input').each( function(i,v) {
		initLoad[i] = true;
		$(this).focus( function() {
			if (initLoad[i]==true) {	
				$(this).attr('value','');
				$(this).css('color', 'black')
				initLoad[i] = false;
			}
		});
	});
	
	// Sort out heading problem
	$('h1.entry-title').each( function() {
		shrinkHeading($(this));
	});
	
	/*
	 * Resources Accordion Page
	 */
	$('.resources_accordion h2').click( function() {
		if ($(this).next().css('display') !== 'none') {
			$(this).next().slideUp('slow');
		}else{
			$('.resources_accordion h2').next().slideUp('slow');
			$(this).next().slideDown('slow');
		}
		return false;
	}).next().hide();
	//$('.resources_accordion dl:first').show();
	
	/* 
	 * Archive Page
	 */
	var performanceLoad = $('#dynamic_performance');
	var performanceLoadStatus = $('#dynamic_performance_status');
	
	// Loader Function
	function performanceloader (performanceLoad, performanceLoadStatus, performanceFile) {
		performanceLoad.hide();
		performanceLoadStatus.show();
		performanceLoadStatus.html('<img src="'+SITE_URL+'images/ajax-loader.gif" />');
		performanceLoad.load(SITE_URL+'productions/archived/'+performanceFile+'.php', function(response, status, xhr) {
			if (status == "error") {
				performanceLoadStatus.html("Couldn't load the page...");
			}else{
				performanceLoadStatus.fadeOut(300,function() {
					performanceLoad.fadeIn();
				});
			}
		});
		// Activate Link
		$('ul#productions_carousel li a').removeClass('active');
		$('ul#productions_carousel li#loadin_'+performanceFile+' a').addClass('active');
	};
	
	if ($('#page_archive').length > 0 && !dynamicLoad) {
		
		// Initiate Billy...
		$('#productions_carousel').billy({
			autoAnimate: false,
			nextLink: $("#carousel_right_arrow"),
			prevLink: $("#carousel_left_arrow"),
			scrollAmount: 245,
			loop: false,
			scrollSpeed: 300
		});
	
		// Hover Items
		var opacity_val = "0.7";
		$("#productions_carousel li a:not(.active)").hover(function() {
				$(this).animate({'opacity': '1.0'}, 300);
		}, function() {
				$(this).animate({'opacity': opacity_val}, 600);
		});
		
		var hash;
		var performanceFile;
		var activateload;
		var actualCurrent;
		var freshLoad = true;
		
		// Default Load Sequence
		hash = window.location.hash;
		if (!hash) {
			hash = $("#productions_carousel li a:first").attr('href');
		}
		performanceFile = hash.replace('#','');
		
		// Load Fresh page (no click)
		if (freshLoad) {
			performanceloader(performanceLoad, performanceLoadStatus, performanceFile);
			freshLoad = false;
		}
		
		// Click
		$("#productions_carousel li a").click(function() {
			
			// Carousel Active items
			$("#productions_carousel li a").removeClass('active');
			$(this).addClass("active");
						
			// Update Page
			hash = $(this).attr('href');
			performanceFile = hash.replace('#','');
			performanceloader(performanceLoad, performanceLoadStatus, performanceFile);
						
		});	
	
	}
	
	// Dynamic Load!
	if (dynamicLoad) {
		
		// Hash update
		hash = window.location.hash;
		
		// Add a listner to anchors
		$('a.switchperformance').click( function () {
			anchorhash = $(this).attr('href');
			// Load new page
			performanceFile = anchorhash.replace('#','');
			performanceloader(performanceLoad, performanceLoadStatus, performanceFile);
		});	
	}
	
	
	/*
	 * Show Hidden Area
	 */
	$('.hidden_block').hide();
	$("a.show_hidden").click( function () {
		var showelement = $(this).attr('href');
		$('.hidden_block'+showelement).toggle();
		$(this).hide();
		return false;
	});
	
	/*
	 * Homepage Past Productions
	 */
	$('#past_productions li a').hover( function() {
		$(this).parent().addClass('active')
		$(this).find('span.java_overlay').fadeIn('fast');
	}, function() {
		$(this).parent().removeClass('active')
		$(this).find('span.java_overlay').fadeOut();
	});
	
	/*
	 * About/Industry Page Tabber
	 */	
	$('#about_tabs li a, #industry_tabs li a').click( function() {
		var tabupdate = $(this).attr('href');
		// Update Tab area
		$('.tab').removeClass('tabactive');
		$('.tab'+tabupdate+'_tab').addClass('tabactive');
		// Update Tabs
		$('#about_tabs li, #industry_tabs li').removeClass('active');
		var this_parent = $(this).parent()
		this_parent.addClass('active');
		return false;
	});	
	
	/*
	 * Production Page Tabber
	 */
	var tabber_hash = window.location.hash;

	if ($(tabber_hash+'_tab').length > 0) {
		var tabupdate = tabber_hash;
		// Update Tab area
		$('.tab').removeClass('tabactive');
		$('.tab'+tabupdate+'_tab').addClass('tabactive');
		// Update Tabs
		$('#production_tabs li').removeClass('active');
		var this_parent = $('a[href$="'+tabupdate+'"]').parent()
		this_parent.addClass('active');
	}
	
	$('#production_tabs li a').click( function() {
		var tabupdate = $(this).attr('href');
		// Update Tab area
		$('.tab').removeClass('tabactive');
		$('.tab'+tabupdate+'_tab').addClass('tabactive');
		// Update Tabs
		$('#production_tabs li').removeClass('active');
		var this_parent = $(this).parent()
		this_parent.addClass('active');
		return false;
	});	
	
});
