$(function() {
		   
		   
	$('.testimonial').eq(0).append('<a href="javascript:void(0)"><img src="images/portfolio/new-trans.png" alt="New Website" /></a>');	   
		   
		   
	$('.colContainer a').not('.no').hover(function(){
										
	$('#slideshow a img').stop().fadeTo(500, '0.5');						
										
	
	$('.colContainer a img').not($('img',this)).stop().fadeTo(500, '0.5');

	
	},function(){
	
	$('.colContainer a img,#slideshow a img').stop().fadeTo(500, '1.0');
	
	});
	
	$('#slideshow').hover(function(){
										

										
	
	$('.colContainer a img').stop().fadeTo(500, '0.5');

	
	},function(){
	
	$('.colContainer a img').stop().fadeTo(500, '1.0');	
	});	


	$('#checkedPaths:not(:has(table))').css({display:'none'});
	$('#brokenPaths:not(:has(table))').css({display:'none'});


	if($('#checkedPaths:visible') && $('#brokenPaths:visible') ){ $('#results').css({padding:'0'})}

	$(window).load(function(){

	jQuery("#results h1").trigger("click");});

// ******************************************* PATHNAME FUNCTION ****************************************** 		
	
	var pathname = window.location.pathname.split("/")[window.location.pathname.split("/").length - 1];
	
	// Page scroll to top function
	$("#toTop").click(function(){$("html, body").animate({scrollTop:0},1000);});	  


		
// ******************************************* COPYRIGHT FUNCTION ***************************************** 			 
		 
	// Set Copyright year			
	$('span#copyright').html(new Date().getFullYear());
	
// ********************************************* FLICKR FUNCTION ****************************************** 	
			  
	// Flickr ID
	var id='49269824@N04';
		
	// Flickr Photostream feed link.
	$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=" + id + "&lang=en-us&format=json&jsoncallback=?", 
		
		function(data){$.each(data.items, function(i,item){
			
		// Number of thumbnails to show.            
		if(i < 5){
			
			// Create images and append to div id flickr and wrap link around the image.
			$("<a href='" + item.link + "' title='" +  item.title +"'><img src=" + item.media.m.replace('_m', '_s') +" /></a>").appendTo("#flickr").hide().fadeIn(500);
				
		}
			
		}); 
	
	$('#flickr a').hover(function(){
										

	$('#flickr a img').not($('img',this)).stop().fadeTo(250, '0.4');

	
	},function(){
	
	$('#flickr a img').stop().fadeTo(250, '1.0');
	
	});
	
	}); 
	
	
	
	$('#linkchecker button').click(function(){
		
		var tv = $('input#url').val();
		
		if(tv == 'http://www.' || tv == "" || tv == "http://" || tv == "www" || tv == "http://www"){
		$('input#url').css({borderColor:'red'}).focus();
		$('#linkError').html('Please enter a valid website address.').css({color:'red'});
		
		return false;
		
		}
		else{
			
		$('input#url').css({borderColor:'#ccc'});	
		$('#linkError').html('Checking website, please wait...').css({color:'green'});	
		
		}
		
		
													
	});
	

	
// ********************************** Portfolio Functions *************************************	
	
	$('.next a').click(function(){ 
	
		var cnt = $(this).index('.next a')+1;
		
		var offset = $('.website h1:eq('+ cnt +'),.logo h1:eq('+ cnt +'),.product img:eq('+ cnt +'),.project img:eq('+ cnt +')').offset();
		
		var test = offset.top;	
		
		$("html, body").stop().animate({scrollTop:test-12},500);
	
	});
		
	
	$(".ol").fadeTo(1, '1');
	
	$('.testimonial').has('.ol').click(function(){	
	
	
		if($(this).find('a').hasClass('close')){
			
			$(this).find('a').removeClass('close');
			
			$(this).find('img').fadeTo(500,'1.0');
		
			$(this).find('a').html('<img src="images/portfolio/testimonial-trans.png" alt="ATI Tank Hire - Testimonial" />');
											
			$(this).find('.ol, p').stop().animate({marginTop:'-500px'}, 500);
		
	
		}
		
		else{
		
			$(this).find('a').addClass('close');
			
			$(this).find('img').fadeTo(500,'0.0');
			
			$(this).find('a').html('<img src="images/portfolio/close-trans.png" alt="ATI Tank Hire - Testimonial" />');
			
			$(this).find('.ol, p').stop().animate({marginTop:'0'}, 500);
			
		}
	
		
		return false;
	
	});
	
// ********************************** Preload Images Function *************************************

	var cache = [];

	// Arguments are image paths relative to the current page.	
	$.preLoadImages = function() {
		
    	var args_len = arguments.length;
    	
		for (var i = args_len; i--;) {
			
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
    	
		}
  	}

	jQuery.preLoadImages("images/portfolio/close-trans.png");
			

	
// *************************************** LOAD TWEETS FUNCTION ************************************* 	
		
	startTwitter();
	
	setInterval(function() {startTwitter();}, 30000);

	
	// Twitter API function
	function startTwitter(){
	
		var url = "http://twitter.com/statuses/user_timeline/ChillWebDesigns.json?count=20&callback=?";  
	
		$.getJSON(url, function(data) {
							
			$("#twitter").html('');	
			
				$(data).each(function(i){
									  
					if(i < 2){
						
					var text = this.text;
											  
					$("#twitter").append('<p>' + text + '</p><p><i>' + relative_time(this.created_at)+'</i></p>');
					}
				});
		
			$('#twitter p').css({color:'#999'});
	
		}).error(function(){
					
		$("#twitter").html('').append('<p></p>');	
		$('#twitter p').css({color:'#ff0000'}).html('Well this is embarrassing, there was a problem loading my recent tweets.<br /><br />' + 
							 'Thoses guys at Twitter seem to be having some problems.<br /><br />' +
							 'My tweets will automatically update once Twitter has resolved the problem.');		
					
		});
	
	}

function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}
// *************************************** SLIDESHOW FUNCTION ************************************* 	

	if(pathname == "index.html" || pathname == "" ){
		
		// Start Slideshow	
		slideShow();
	
	}
	
	
	$('.colContainer a').each(function(){
			  
			var info =	$(this).attr('rel');
				
				$(this).find('span').html(info);
						
	});

	$('.colContainer a').hover(function(){
								   
								   tip = this.title; 
								   
								   this.title = '';},function(){
									   
									   this.title = tip;});	
	
	});


function slideShow(){
		
	$('#slideshow a').css({opacity:0});
	
	$('#slideshow a:first').css({opacity:1}).addClass('show');
	
	$('#slideshow .caption').animate({height:'25px'},500);
	
	
	var caption = "";	 
	
	caption = $('#slideshow a:first').attr('rel');
	href = $('#slideshow a:first').attr('href');
	
	var title = this.title;
	
	 
	$('#slideshow .content').html('<a href="' + href + '">' + caption + ' </a>');
				
	var pauseTimer = setInterval('slideshow()', 15000);
	
	var tip = "";
	
	$('#slideshow a').hover(function(){
								   
								   tip = this.title; 
								   
								   this.title = ''; 
								   
								   clearInterval(pauseTimer);},function(){
									   
									   this.title = tip;
									   
									   pauseTimer = setInterval('slideshow()', 15000);});	
}


function slideshow(){
	
	var current = ($('#slideshow a.show')?  $('#slideshow a.show') : $('#slideshow a:first'));

	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#slideshow a:first') :current.next()) : $('#slideshow a:first'));	
	
	next.css({opacity:0}).addClass('show').fadeTo(1000,'1');

	current.fadeTo(1000,'0').removeClass('show');
	
	$('#slideshow .caption').animate({height:'0'}, 300 , function(){$('#slideshow .content').html('');
	
	$('#slideshow .caption').animate({height:'25px'},500);
	
	var caption = "";	 
	
	caption = next.attr('rel');
	
	$('#slideshow .content').html('<a href="' + href + '">' + caption + ' </a>');
	
	});	
	

	
}	
