/* Declare a namespace for the site */

var Site = window.Site || {};
var slideImgWidth;
var intr = null;
var startIntr = null;

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function($) {
	
	//same as $(document).ready();
	$(function() {
		
		
		
		//Cufon.replace('h2');
		$("#Projects a.box").hover(function(){
			$(this).hide("fast");
		}, function(){
		});
		$("#Projects a.preview").hover(function(){}, function(){
			$(this).next().show("fast");
		});
		
		$("#Info a").click(function(){
			if($(".project:visible").length > 0){
			$(".project:visible").fadeOut("fast", function(){
				$("#Main-info").show("fast");
				});
			}else{
				$("#Main-info").show("fast");
			}
		});
		
		$("#Projects a.preview").click(function(){
			$("a.preview").animate({opacity: 1}, "fast");
			$(this).animate({opacity: 0.75}, "fast");
			var key = $(this).attr("id");
			var index = $("#Projects a").index($("#Projects #"+key));
			var position = (70 * index) + 20;
			if(index ==  $("#Projects a").length -1){
				position += 15;
			}
			if($(".project:visible").length > 0){
				$(".project:visible").fadeOut("fast", function(){
					$("article."+key).fadeIn("fast")
						.find(".project-arrow").css("margin-left", position);
				});
			}else{
				$("article."+key).fadeIn("fast")
					.find(".project-arrow").css("margin-left", position);
			}
			clearInterval(intr);
			intr = setInterval( "autoSlide()", 5000 );
			return false;
		}).each(function(i, thumb){
			$(thumb).CreateBubblePopup({
				innerHtml: $(this).find("img").attr("alt"), 
				themeName: 	($(this).find("img").attr("alt") == 'Information')? 'all-grey' : 'all-black',
				themePath: 	'img/jquerybubblepopup-theme',
				position: 'top',
				align: 'left',
				tail: {align: 'left'},
				innerHtmlStyle : {'padding': '5px', 'background-image' : 'none'},
				dropShadow: false,
				openingSpeed : 1,
				closingSpeed : 1
		   });
		});
		$("#Info a").CreateBubblePopup({
				innerHtml: "information", 
				themeName: 	'all-blue',
				themePath: 	'img/jquerybubblepopup-theme',
				position: 'top',
				align: 'left',
				tail: {align: 'left'},
				innerHtmlStyle : {'padding': '5px', 'background-image' : 'none'},
				dropShadow: false
		   });
		
		$(".slidebuttons a, a.img-container").click(function(){
			clearInterval(intr);
			var index = 0;
			if($(this).closest("ul").hasClass("slidebuttons")){
				index = $(this).parent().index();
			}else{ 
				index = $(".project:visible .slidebuttons a.active").parent().index()+1;
				if(index == $(".project:visible .slidebuttons li").length){
					index = 0;
				}
			}
			slideSwitch(index);
			intr = setInterval( "autoSlide()", 5000 );
			return false;
		});
		
		preloadImages('/thanos/img/arrow_bg.png');

	});


	$(window).bind("load", function() {
		//$(".slidebuttons").css({ "position" : "absolute",  "top" : "514px" });
		$(".loader").hide();
		$("#Projects").fadeIn("fast");
		startIntr = setInterval( '$("#Projects a.preview:first").trigger("click");clearInterval(startIntr)', 500 );
	});
	
})(jQuery);

function slideSwitch(index){
	slideImgWidth = $(".slideshow:visible img:first").width();
    var $active = $('.slideshow:visible IMG.active');
	
    if ( $active.length == 0 ) $active = $('#slideshow IMG:first');
	
    var $next =  $(".slideshow:visible img:nth-child("+(index+1)+")");
    var left = '-'+($next.index() * slideImgWidth);

    $active.addClass('last-active');
    $next.addClass('active');
    $('.slideshow:visible .img-container').animate({left: left}, 800, function() {
            $active.removeClass('active last-active');
        });
	
	$(".slidebuttons:visible a").removeClass("active");
	$(".slidebuttons:visible li:nth-child("+($next.index()+1)+") a").addClass("active");
}
		
		
function autoSlide() {
    var $active = $('.slideshow:visible IMG.active');
	var index = $active.next().length ? $active.next().index()
        : $('.slideshow:visible IMG:first').index();
	slideSwitch(index);
}

function fadePLink(elem) { 
  elem.fadeIn("fast", function() {
  	fadePLink($(this).parent().next().find("a"));
	}); 
}     

function preloadImages(images) {
    if (document.images) {
        var i = 0;
        var imageArray = new Array();
        imageArray = images.split(',');
        var imageObj = new Image();
        for(i=0; i<=imageArray.length-1; i++) {
            imageObj.src=images[i];
        }
    }
}                    
