$(document).ready(function(){
	/*------------------------------------------------------------ Remove title attribute form links */
	$('a').removeAttr('title');
	/*------------------------------------------------------------ Main animation */
	if ($('#content').length) {	
		$('#content').append('<span class="trigger"><img src="assets/templates/tom_phillips/images/arrow_close.gif" width="16" height="17" alt="" /></span>');		
		$('span.trigger').toggle(function() {
			$('#content').animate({	right: "-230px" }, 500, function() { $('span.trigger').find('img').attr('src','assets/templates/tom_phillips/images/arrow_open.gif') } );
		},function() {
			$('#content').animate({	right: "0" }, 500, function() { $('span.trigger').find('img').attr('src','assets/templates/tom_phillips/images/arrow_close.gif') } );
		});	
	}
	if ($('#contentPages').length) {	
		$('#contentPages').append('<span class="trigger"><img src="assets/templates/tom_phillips/images/arrow_open.gif" width="16" height="17" alt="" /></span>');		
		$('span.trigger').toggle(function() {
			$('#contentPages').animate({	left: "-270px" }, 500, function() { $('span.trigger').find('img').attr('src','assets/templates/tom_phillips/images/arrow_close.gif') } );
		},function() {
			$('#contentPages').animate({	left: "0" }, 500, function() { $('span.trigger').find('img').attr('src','assets/templates/tom_phillips/images/arrow_open.gif') } );
		});	
	}
	/*------------------------------------------------------------ Red buttons animation */
	$('#buttons img').hover(function() {
		$(this).animate({ paddingLeft: "5px" }, 200 );						   
    },function() {
		$(this).animate({ paddingLeft: "0" }, 200	);			
	})	
	/*------------------------------------------------------------ Search form */
	$('input#ajaxSearch_input').focus(function () {
         $(this).css('border','1px solid #fff');
    });
	$('input#ajaxSearch_input').blur(function () {
         $(this).css('border','none');
    });
	/*------------------------------------------------------------- Content Scroller */
	$('#scrollArea').jScrollPane({animateTo:true});
	
	var scrollAreaHeight = $('#scrollArea').height();
	var jScrollPaneContainerHeight = $('div.jScrollPaneContainer').height();
	
	if (scrollAreaHeight > jScrollPaneContainerHeight) {
		$('#contentPages').append('<a id="up" class="scroll" rel="-100" href="#"><img src="assets/templates/tom_phillips/images/up.png" width="15" height="10" alt="" /></a><a id="down" class="scroll" rel="100" href="#"><img src="assets/templates/tom_phillips/images/down.png" width="15" height="10" alt="" /></a>');
		$('.scroll').bind('click',function() {
			$('#scrollArea')[0].scrollBy(parseInt($(this).attr('rel')));
			return false;
		});
	}
	/*-------------------------------------------------------------- Disable h1 when sub menu doesn't exist */
	if (!($('#leftSidebar ul').length)) {
		$('#leftSidebar h1').hide();	
	}
	/*----------------------------------------------------------------------------------------------------*/
	if ( $('#projects_list').length ) {	
		$('#projects_list div:last').css('margin-bottom','0');
		var projectsHeight = $('#projects_list').height();
		
		if ((projectsHeight + 106) > $('#leftSidebar').height())
			$('#leftSidebar').height(projectsHeight+106);
	}
	if ( $('#project_details').length ) {	
		if ( ($('#slideshowWrapper').length )) {
			var projectsHeight = $('#project_details').height();
			
		
		if ((projectsHeight + 126) > $('#leftSidebar').height())
			$('#leftSidebar').height(projectsHeight+126);
		}
	}
 	/*------------------- Galeries ------------------------------------------------------------------------*/
	//----------- Main Slideshow
	$('#slideshow').cycle({ 
		fx:     'fade', 
		speed:   1500, 
		timeout: 5000, 
		next:   '#next',
        prev:   '#prev',
		before:  onBefore, 
		after:   onAfter
	 });
	
	function onBefore() { 
		$('#output').animate({  
			height: "0",
			opacity: 0
		}, 500 );
		$('#output').html("");
	}
	
	function onAfter(curr,next,opts) { 
		if (this.alt != '') {
			$('#output').animate({ 
				height: "26px",
				opacity: 0.7
			}, 500 );
			//console.log(opts.slideCount);
			$('#output').append('<h2>' + this.title + '</h2><p>' + this.alt + '</p>'); 
		}
		if (opts.slideCount==1) {
			$('#slideshow').cycle('stop');
			return false;
		}
	}
	
	$('#pause').click(function() { $('#slideshow').cycle('pause'); return false; });
    $('#play').click(function() { $('#slideshow').cycle('resume'); return false; });

	/*-----------------------------------------------------------------------------------------*/
									  
}); //Close document.ready

$(window).bind("load", function() {
    //-----------  Preload images for slideshows
	
	$('#loading').hide();

	if ($('#content').length) {	
		$('#background').fadeIn(500, function() {
			$('#content').animate({	right: "0" }, 500 , function () { 
				$("#top_logo img").animate({ marginTop: "0" }, 500 );	
				$('span.trigger').show(); 
			});
		});
	}
	if ($('#contentPages').length) {
		$('#background').fadeIn(500, function() {
			$('#contentPages').animate({	left: "0" }, 500 , function () { 
				$("#top_logo img").animate({ marginTop: "0" }, 500 );	
				$('span.trigger').show(); 
			});
		});
	}
	if ( ($('#projects_list').length) || ($('#project_details').length) ) {
				$("#top_logo img").animate({ marginTop: "0" }, 500 );	
	}
	$('#slideshowWrapper').fadeIn('slow');
});
