$(document).ready(function () {
	
	/*
	 * 
	 * 
	 * FadeIn of all the content
	 */
   	 $('div#main-content').addClass('display');
   	 $('div#main-content').fadeIn(1500);
   	 
   	 if(document.getElementById('portfolio')){
   	 activateProjects();
	 
		if ($('#tags').val()) {
			$('#close-xx').show();
		}else{
		$('#close-xx').hide();
		}
	}
	 
	 
	/*
	 * 
	 * 
	 * Change agency
	 */
	$('img.menu_class').click(function () {
		if ($('ul.the_menu').is(':hidden')) {
			$('ul.the_menu').slideDown('slow');
			//$('div.wrapper').addId('close');
		}
		else if ($('ul.the_menu').not(':hidden')){
			$('ul.the_menu').slideUp('slow');
		}
    });
    
    
    $(document).mousedown(function(){
  		 //console.log($('ul.the_menu').not(':hidden'));
  		 if ($('ul.the_menu').not(':hidden')){
  		 	
			$('ul.the_menu').delay(800).slideUp('slow');
		}

    });


/**************************** 
* BACK to top
*****************************/
$('.backtop').hide();
$(window).scroll(function () {
	$('.backtop').fadeIn(3000);
});

$('.backtop').click(function(){
	$('html, body').animate({scrollTop:0}, 'slow');
	return false;
});
	
	
/* ******************************
* CHANGE LAYOUTS
******************************/


//swfaddress listener
function activateProjects(){
	SWFAddress.addEventListener('change', onSWFAddressChange);
	SWFAddress.addEventListener('init', onSWFAddressInit);
	$('div.project').fadeOut(1);
}

	 
// btn Click 
$('li.layout1').click(function(){
	changeLayout(this.id);
});
	
	
$('li.layout2').click(function(){
	changeLayout(this.id);
}); 
	
$('li.layout3').click(function(){
	changeLayout(this.id);
});




$('#close-xx').click(function(){
$('#tags').val('').closest('form').trigger('submit');
$('#close-xx').hide();
});


	
// SWFAddress Set Value
function changeLayout(target){
	SWFAddress.setValue(target);
}

// SWFAddress INIT EVENT
function onSWFAddressInit(event){
 var thePath = SWFAddress.getPathNames();
  
  		if(thePath==""){
 		SWFAddress.setValue("1");
 		}

}

// SWFAddress CHANGE EVENT	
function onSWFAddressChange(event) { //on url change
   var thePath = SWFAddress.getPathNames();
  
  			 
	 	if(thePath=="2"){
			$('div.project').fadeOut('fast', function() {
			$(this).fadeIn('slow').removeClass('img-grid');
			$(this).fadeIn('slow').removeClass('txt-grid');
	  		$(this).fadeIn('slow').addClass('txt-img-grid'); 
			});
		}else if (thePath=="3"){
		 	$('div.project').fadeOut('fast', function() {
			$(this).fadeIn('slow').removeClass('txt-img-grid');
			$(this).fadeIn('slow').removeClass('img-grid');
	  		$(this).fadeIn('slow').addClass('txt-grid'); 
			});
		}else if(thePath=="1"){
	  		$('div.project').fadeOut('fast', function() {
			$(this).fadeIn('slow').removeClass('txt-img-grid');
			$(this).fadeIn('slow').removeClass('txt-grid');
	  		$(this).fadeIn('slow').addClass('img-grid');
			});
		}else{
			thePath="1";
			$('div.project').fadeOut('fast', function() {
			$(this).fadeIn('slow').removeClass('txt-img-grid');
			$(this).fadeIn('slow').removeClass('txt-grid');
	  		$(this).fadeIn('slow').addClass('img-grid');
			});
		
		}
		
		// set layout buttons
		$('li.layout1').removeClass('block1');
		$('li.layout2').removeClass('block2');
		$('li.layout3').removeClass('block3');
	  
		$('li.layout'+thePath).addClass('block'+thePath);
				
		// changes layout in pagination
		$('span.pages > a').attr('href', setLink($('span.pages > a').attr('href'))+"#/"+thePath);
		$('span.next > a').attr('href', setLink($('span.next > a').attr('href'))+"#/"+thePath);
		$('span.prev > a').attr('href', setLink($('span.prev > a').attr('href'))+"#/"+thePath);
			
	 
 }
	
	
	
	
	
	function setLink(str){
	
		if(str){
			var str2=str.substring(0,str.lastIndexOf("#"));
			
		}else{
			var str2="1";
		}
		//console.log('str2: '+str2);
		return str2;
		
	}
	
	
	
	/*
	 * 
	 * 
	 * Form layout changes
	 */
	 $('form.jqtransform').jqTransform();
	 
	 
	 
	 
	 
	 $('a[rel="external"]').click(function() { 
	 	$('div.wrapper').fadeOut('slow')});
});
