/*
Fancy Folio
Created: 15/10/2009
By: Catalin Pinte
Contact Info: pinte_catalin@yahoo.com
*/



var $hover_speed = 300, /* Navigation Hover animation speed in miliseconds */
    $hover_easeing_method = 'jswing', /* Easing method  */
    $slideshow_interval = 10000, /* slide durration (for recent projects) in miliseconds  */
	$fadeOut_speed = 600, /* Slide FadeOut speed in miliseconds   */
	$fadeIn_speed = 800; /* Slide FadeIn speed in miliseconds   */
	
$(document).ready(function(){

	if( $("a.fancy_box").length != 0) {

        // fancybox settings ... you can find more at http://fancybox.net/howto
        $("a.fancy_box").fancybox({
              'zoomSpeedIn': 300, 
			  'zoomSpeedOut': 300, 
			  'overlayColor': "#111", 
			  'overlayOpacity': 0.7
		}); 
	}
	

// toggle sub menus on hover
$('#navigation ul li')
  .hover(function(e) {
    $(this).children('ul').hoverFlow(e.type, {
	  'opacity': '1',
      'height': 'show',
      'marginTop': 'show',
      'marginBottom': 'show',
      'paddingTop': 'show',
      'paddingBottom': 'show' 
    });
  }, function(e) {
    $(this).children('ul').hoverFlow(e.type, {
	  'opacity': '0',	 
      'height': 'hide',
      'marginTop': 'hide',
      'marginBottom': 'hide',
      'paddingTop': 'hide',
      'paddingBottom': 'hide' 
    });
  });
  
  
$("#navigation ul li ul li a").click(function(){ 
		$(".hover").hide();	  
		$("#navigation ul li ul").animate({ 'opacity':'0'}, 300);		
});

    $("#navigation ul li a").click(function(){

        var $elem = $(this),
		last = $("#navigation ul").find(".current"),
		lastPosition = last.position().left;
		last.removeClass("current");
		last.css({background:""});		 
	
		$(".hover").attr('style','left:'+ lastPosition +'px').show()
		        .animate({"left": $elem.parent().position().left + 1}, $hover_speed /* Animation speed */, $hover_easeing_method, function (){ 
		        
				where = "";
				window.location = $elem.attr('href');
		});		

		return false;   
	});
	
	if($("#recent-projects").length != 0){  
	
	   Slide_Show();
	   
	   if(!$.cookie){ slideShow.$start();}else{if($.cookie('recent-projects-slidesh0w') == "stop"){ $(".start").show(); $(".stop").hide();}else{slideShow.$start();}}

$("ul#projects-thumbs li").mouseover(function(){
           $(this).stop().animate({opacity: '1'}, 150);
	   });
	   
       $("ul#projects-thumbs li").mouseout(function(){
		   if($(this).attr("class") != "active"){$(this).stop().animate({opacity: '0.5'}, 150);}
	   });
	
	   var $timeout;
	   $("ul#projects-thumbs li a").click(function(){	
							
	       if($.cookie('recent-projects-slidesh0w') != "stop"){ 
		      slideShow.$stop(); // stops slideshow because user selected a project
			  slideShow.$start();
		   }		
  		   var $id = $(this).parent().attr("id").split('-');
	       Change_Slide($("#recent-projects #" + $id[1]+ "-" + $id[2]));		
		   return false;
	   });
       $(".start").click(function(){ 
			slideShow.$start(); 
			Slide_Show();  
			$(".stop").show(); 
			$(".start").hide();
			$.cookie('recent-projects-slidesh0w', "start" ,{ expires: 30, path: '/' });
			$(".loader").remove();
			$('<div class="loader"></div>').appendTo($("ul#projects-thumbs li.active"));
			setTimeout(function(){$(".loader").css({opacity: '0.5'}).animate({width: '100%'}, $slideshow_interval )}, 1500);
	   });

       $(".stop").click(function(){
			slideShow.$stop();  
			$(".stop").hide(); $(".start").show(); 
			$.cookie('recent-projects-slidesh0w', "stop" ,{ expires: 30, path: '/' });
			$(".loader").remove();
       });
    } 

    $("#content #top-bar ul li:last-child").css({background: "none"});
    $("#content .project ul li:first-child").attr("style", "padding-left:0");
   
   /*  Live preview variant only  */
  
   $("#sub-footer a").click(function(){
		$('body').css({ 'background':  $(this).parent().attr("rel"),
		                'background-attachment':'fixed',
						'background-position':'50% 0'
	    });
	
	    $.cookie('interactive-background-selector', $(this).parent().attr("rel") ,{ expires: 30, path: '/' });
		  
		$("#sub-footer li").removeClass("active");
		$(this).parent().parent().addClass("active");
		return false;
   });
  
   
   /*  Live preview variant only  */  
   
   
   
});


//  FUNCTIONS
var slideShow = function() {
        var $start,
            $stop;
        return {
	        $start : function(){  $start = setInterval("Slide_Show()", $slideshow_interval ); },  
            $stop : function(){
		         clearInterval($start);
	        }
     }  
}();

//this is slide_show function 
function Slide_Show(){
			var $active = $('#recent-projects ul#projects li.active');
			if ($active.length == 0) $active = $('#recent-projects ul#projects li:last-child');
			var $next =  $active.next("li").length ? $active.next("li")
			: $('#recent-projects ul#projects li:first-child');		  
		    Change_Slide($next);
}

//this changes slide image
function Change_Slide($element){

          $("ul#projects li.active").removeClass("active").fadeOut($fadeOut_speed); 
		  $element.addClass("active").fadeIn($fadeIn_speed);
	
          $("ul#projects-thumbs li").attr("class","").css({opacity: '0.5'});
		  $("#thumb-" + $element.attr("id")).addClass("active").css({opacity: '1'});
		  $("ul#controls a.p_details").attr("href",$("#thumb-" + $element.attr("id") + " a").attr("href"));
		  
          $("#arrow").css({left: $("ul#projects-thumbs li.active").position().left }).show() ;

		  if($.cookie('recent-projects-slidesh0w') != "stop"){ 
			      $(".loader").remove();
				  $('<div class="loader"></div>').appendTo($("ul#projects-thumbs li.active"));
				  setTimeout(function(){$(".loader").css({opacity: '0.5'}).animate({width: '100%'}, $slideshow_interval);	}, 1500);
		  }
}
