jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

if(!Array.indexOf){
  Array.prototype.indexOf = function(obj){
   for(var i=0; i<this.length; i++){
    if(this[i]==obj){
     return i;
    }
   }
   return -1;
  }
}

var pid = "";
var currentProject = 0;
var filter = "";
var show = "";

var speed = 200;
var size = 60;

var isIE = !$.support.opacity;
var isIE6 = isIE && !window.XMLHttpRequest;

$(document).ready(function(){
		
	var myHeight = 0;
	if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	
	$('#popupOverlay').css({"opacity": 0.45});
	
	if (isIE6) {
		$(window).bind('resize.cboxie6 scroll.cboxie6', function () {
			$('#popup').css({width: $(window).width(), top:$(window).scrollTop()+ ($(window).height()/2)});
			$('#popupOverlay').css({position:'absolute', width: $(window).width(), height: $(window).height(), top: $(window).scrollTop(), left: $(window).scrollLeft()});
		}).trigger("scroll.cboxie6");
	}
	
	$(".project").click(function(){
		var y = $(document).scrollTop();
		if (typeof(window.innerHeight) != 'number') {
			document.documentElement.style.overflow = document.body.style.overflow = 'hidden';
			$('html').css('border-right','16px solid #CCC');
			$('#plane').css('margin-right','16px');
		}
		
		$(document).scrollTop(y)
        $('#popupOverlay').fadeIn();
		$('#loading').show();
        $('#popup').fadeIn();
		pid = $('a',this).attr('href');
		
		updateProject();
		return false;
	});
	
	$('.closeButton').click(function(event){
		if ($(event.target).hasClass('closeButton')) {
			$('#project').hide();
			$('#popup').hide();
			$('#popupOverlay').fadeOut();
			var y = $(document).scrollTop();
			if (typeof(window.innerHeight) != 'number') {
				document.documentElement.style.overflow = document.body.style.overflow = 'auto';
				$('html').css('border-right','none');			
				$('#plane').css('margin-right','0');
			}
			$(document).scrollTop(y)
			pid = "";
		}
	});
	
	$('#next').hide();
	
	$('#previous').hide();
	
});

function updateProject(){
	$.ajax({
		type:"get",
		dataType:"json",
		url:"/portfolio.front.getProjectJSON",
		data:{resourceUrl: pid},
		success: function(data){
			var string = ""
			if (data.small_logo) {
				if (data.clienturl) {
					string += '<a href="/clients' + data.clienturl + '.cfm" title="Click to visit the clients page for ' + data.clientname + '">';
				}
				string += '<img alt="' + data.clientname + ' Logo" src="' + data.small_logo + '"  width="111" height="70" />'
				if (data.clienturl) {
					string += '</a>\n';
				}
				$('#details #client').html(string).show();
			} else {
				$('#details #client').html(string).hide();
			}
			
            $('#details #title h2').html(data.title);
			
			$('#details #print a').attr('href', '/portfolio.front.getProjectPDF?resourceUrl=/' + pid);
					
			if(data.externalurl != ""){
				$('#details #url a').attr('href', data.externalurl).show();
				var ma = data.externalurl.match(/^(https?:\/\/)(www\.)?([^/]+)?/i);
				if(data.externalurl == ma[1] + ma[2] + ma[3]){
					$('#details #url a').html(ma[2] + ma[3]);
					$('#details #url').show();
				} else {
					$('#details #url a').html('View Live Project');
					$('#details #url').show();	
				}
			} else	{
				$('#details #url').hide();
			}
			
			$('#details div#body').html(data.keyfeatures + data.body);
			
            var slides = '<div id="slides">\n';
            $.each(data.slides, function(i, objValue){
				totalLoading++;
                slides += '<img class="loadable-image" src="' + objValue + '"/>\n';
            });
            slides += '</div>';
            $('#slideshow').html(slides);
			updateSlideShow();
			
			$('.loadable-image').each(function(){
				if($(this).complete){
					totalLoading--;
				}
			});
			
			if (totalLoading == 0) {
				displayProject();
			} else {
				$('.loadable-image').load(checkDisplay);
			}
		},
		timeout: function(data){
			alert("timeout");
		}
	});
}

var totalLoading = 0;
function checkDisplay(){
	totalLoading--;
	if(totalLoading == 0){
		displayProject();
	}
}
function displayProject(){
	$('#loading').hide();
	if ($('#popup').is(':visible') && $('#project-container').is(':hidden')) {
		$('#project').show();
		$('#details div#body').scrollTop(0);
	}
}

var timer;

function slideTimer(){
	timer = setInterval("nextSlide()",4000);
}

function nextSlide(){
	var current = $('#slides').children('img:visible');
	current.fadeOut();
	var next;
	if(current.next().is('img')){
		next = current.next().fadeIn();
	} else {
		next = current.parent().children(':first').fadeIn();
	}

	$('#thumbnails').children('li').children('img').parent().removeClass('current');
	$('#thumbnails').children('li').children('img[sid=' + next.attr('sid') + ']').parent().addClass('current');
}

function updateSlideShow(){
	
	if(timer){
		clearInterval(timer);
	}
	
	$('#slides').after('<ul id="thumbnails" style="list-style-type:none;" />');
	
	$('#slides').children('img').each(function(i){
		$('#thumbnails').append('<li class="ui-corner-all"><img sid="' + i + '" class="thumbnail" src="' + $(this).attr('src') + '" /></li>');
		$(this).addClass('off stage').attr('sid',i);
	});
	
	slideTimer();
	
	$('#thumbnails').children('li').children('img').click(function(){
		clearInterval(timer); 
		$('#slides').children('img:visible').fadeOut();
		$('#slides').children('img[sid=' + $(this).attr('sid') + ']').fadeIn();
		
		$(this).parent().parent().children('.current').removeClass('current');
		$(this).parent().addClass('current');
		return false;
	});
	
	$('#slides').children('img:first').removeClass('off');
	$('#thumbnails').children('li:first').addClass('current');
}
