$(window).load(
    function() {
		$(".work_simple img").pixastic("desaturate");
    }
);

jQuery(document).ready(function($){






//var BaseUrl= "http://localhost:8888/signature";
var BaseUrl= "http://www.signaturecreative.com";
var theDiv="";
var islaunched=false;

var theWrapper= $('#work_wrapper');
var isWorkpage= true;
var activeClient= "";

var isiPad = navigator.userAgent.match(/iPad/i) != null;

var $scrollingDiv = $("#header");
var $sidebarDiv = $("#lightboxsc");
var $overlayDiv = $("#overlay");
var elemTop = $scrollingDiv.offset().top;


if(isiPad)
{
	$(window).scroll(function(){
	$scrollingDiv.stop();
	$sidebarDiv.stop();
	$overlayDiv.stop();
	
	if ($(window).scrollTop()>elemTop) {
		$scrollingDiv.animate({"marginTop": ($(window).scrollTop()-elemTop) + "px"}, "slow" );
		$sidebarDiv.animate({"marginTop": ($(window).scrollTop()-elemTop) + "px"}, "slow" );
		$overlayDiv.animate({"marginTop": ($(window).scrollTop()-elemTop) + "px"}, "slow" );
		} else {
		$scrollingDiv.animate({"marginTop": "0px"}, "slow");
		$sidebarDiv.animate({"marginTop": "0px"}, "slow");
		$overlayDiv.animate({"marginTop": "0px"}, "slow");
	}
	});
} else {
	
	$(window).scroll(function(){
	$scrollingDiv.stop();
	
	if ($(window).scrollTop()>elemTop) {
		$scrollingDiv.animate({"marginTop": ($(window).scrollTop()-elemTop) + "px"}, "slow" );
		} else {
		$scrollingDiv.animate({"marginTop": "0px"}, "slow");
	}
	});
}






//// ADD CLIENT CLASS

$('.works').each(function(){
	var ClientClass = $('.work_client',this).val();					  
	$(this).addClass(ClientClass);
	});

/////// MASONRY

$('#work_wrapper').masonry({
	singleMode: false,
	columnWidth: 235, 
	resizeable: true,
	itemSelector: '.works:not(.invis)',
	animate: false
});

////// NAVIGATION CONTROLS
$("#view_categories").click(function(){
	$("#view_clients").removeClass('selected');	
	$(this).addClass('selected');
	$('#client_wrapper').slideUp(300);
	$('#menu-categories').slideDown(300);
	return false;						   
});

$("#view_clients").click(function(){
	$("#view_categories").removeClass('selected');	
	$(this).addClass('selected');
	$('#menu-categories').slideUp(300);
	$('#client_wrapper').slideDown(300);
	$('#client_list').animate({marginTop: 0}, 1500,'easeOutBack', function(){
		makeScrollable("#client_wrapper", "#client_list");																	   
	});

	
	return false;						   
});

/* ////// TOOLTIP ////// */

function InitTooltip(reload) { 
    $(".tooltip").bind("mouseover", function() { 
        var me = $(this); 
        if(!me.hasClass('tooltipactivated')) {
            me.tooltip({ 
			onBeforeShow: function() {
				//theclass=this.getTrigger().attr('class');
				//this.getTip().addClass(theclass);
				this.getTrigger().fadeTo(300, 0.5);
			},
			onBeforeHide: function() {
				this.getTrigger().fadeTo(300, 1);
			},
		   effect: 'slide',
		   layout: '<div><span class="bottom_arrow"></span></div>',
		   tipClass: "tool",
                api: true 
            }).show(); 
            me.addClass('tooltipactivated');
        }
    }); 

    $(".tooltipwhite").bind("mouseover", function() { 
        var me = $(this); 
        if(!me.hasClass('tooltipactivated')) {
            me.tooltip({ 
			onBeforeShow: function() {
				//theclass=this.getTrigger().attr('class');
				//this.getTip().addClass(theclass);
				this.getTrigger().fadeTo(300, 0.5);
			},
			onBeforeHide: function() {
				this.getTrigger().fadeTo(300, 1);
			},
		   effect: 'slide',
		   layout: '<div><span class="bottom_arrow"></span></div>',
		   tipClass: "toolwhite",
                api: true 
            }).show(); 
            me.addClass('tooltipactivated');
        }
    }); 
}
InitTooltip();



////// SLIDE CLIENT LIST

function makeScrollable(wrapper, scrollable){
  var wrapper = $(wrapper), scrollable = $(scrollable);
  var inactiveMargin = 100;         
  var wrapperWidth = wrapper.width();
  var wrapperHeight = wrapper.height();
  var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
  wrapper.mousemove(function(e){
    var wrapperOffset = wrapper.offset();
    var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight  - inactiveMargin;
    if (top < 0){
      top = 0;
    }
    wrapper.scrollTop(top);
  });       
}



///////////

$(".postmetadata a").live('click',function(){
		return false;						   
});



$(".works").live("mouseenter", function(e) {
										
/*	var theimg = $('.work_thumb', this);
	var theimgColor = $('.work_thumb_color', this);
	theimg.animate({opacity: 0}, 500,'easeOutQuad');
	theimgColor.fadeIn(300);*/
	
	var theimg = $('.work_thumb_color', this);
	$('.work_thumb_color', this).animate({opacity: .7}, 300,'easeOutQuad');
	if ($(this).hasClass('faded')){
	$(this).animate({opacity: .7}, 300,'easeOutQuad');
	}
});
$(".works").live("mouseleave", function(e) {
	/*var theimg = $('.work_thumb', this);
	var theimgColor = $('.work_thumb_color', this);
	theimgColor.fadeOut(300);
	theimg.animate({opacity: 1}, 500,'easeOutQuad');
	*/
	var theimg = $('.work_thumb_color', this);
	theimg.animate({opacity: 1}, 300,'easeOutQuad');
	if ($(this).hasClass('faded')){
	$(this).animate({opacity: 1}, 300,'easeOutQuad');
	}
});





//////HISTORY

///// CLOSE THE DETAIL
function closeWork(){
	$('.works').removeClass('faded');
	$('.works').animate({opacity: 1}, 300,'easeOutQuad');
	$(".work_thumb").fadeOut();
	
	$('.work_inside:visible').hide(1, function(){
		$('.works').removeClass('work_detail');
		$('.works .work_preview').show();
		$(this).html('');
		$('#work_wrapper').masonry();
	});
	
	islaunched=false;
}

function closePage(){
		$('#page_navigation a').removeClass('selected');								   
		$("#page_wrapper").html('');
		$("#page_wrapper").fadeOut();							   
		$('#work_wrapper').masonry();
		$(".work_thumb").fadeOut();
	
}
function closeCategory(){
		$('#work_wrapper').children('.invis').toggleClass('invis').fadeIn(300);
		$('#menu-categories a').animate({backgroundColor: '#333333'}, 300,'easeOutQuad');						   
		$('#menu-categories a').removeClass('selected');
		$('#menu-categories a span').remove();
		$('#work_wrapper').masonry();

}

function closeClient(){
		$('#work_wrapper').children('.invis').toggleClass('invis').fadeIn(300);
		$('#client_list a').animate({backgroundColor: '#333333'}, 300);						   
		$('#client_list a').removeClass('selected');
		$('#client_list a span').remove();
		$('#work_wrapper').masonry();

}


$("a.close").live('click',function(){
	href = "closework";
	$.bbq.pushState({ url: href });
	closeWork();
	return false;
});

$("a.pageclose").live('click',function(){
	var href = "closepage";
	$.bbq.pushState({ url: href });
	closePage();
	return false;
});

///// PREPARE THE DETAIL


function h2prepareIt(post_id,original_post){
	if (islaunched){
if(!isiPad)
{
		$('.works').animate({opacity:1},200).removeClass('faded');
};
		
		$('.work_inside:visible').hide(1, function(){
			$('.works').removeClass('work_detail');
			$(this).html('');
			$('.works .work_preview').show();
			Launchit(post_id,original_post);
		});
		islaunched=false;
		
	} else {
			Launchit(post_id,original_post);
			theDiv=original_post;
	}
		return false;
}

///// LAUNCH THE DETAIL

function Launchit(post_id,original_post){
		theDiv=original_post;
		islaunched=true;
		$('.work_inside', theDiv).load(BaseUrl+"/ajaxpost/",{id:post_id}, function(){
		/*	var theTitle = $('.work_inside #facebook_info', theDiv).attr('title');
			var theImgSrc = $('.work_inside #facebook_info', theDiv).html();
			//var theFbCode = '<meta property="og:title" content="'+theTitle+'" /> <meta property="og:image" content="'+theImgSrc+'"/>';
		$('head').append('<meta property="og:title" content="'+theTitle+'" />');
		$('head').append('<meta property="og:image" content="'+theImgSrc+'"/>');*/
		$('.loading').fadeOut().remove();
		$('.work_preview', theDiv).hide();
		theDiv.addClass('work_detail');
		$('.work_inside', theDiv).show(100, function(){
			$('#work_wrapper').masonry();
		});
		var theY = $(theDiv).offset().top-20;											 
		$.scrollTo(theY,500);
		$("#header").scrollTo(0,500);
		if(!isiPad)
		{
				$(".works:not(.work_detail)").animate({opacity:.3},300).addClass('faded');
		};
		
		if(isiPad){
			$scrollingDiv.animate({"marginTop": ($(window).scrollTop()-elemTop) + "px"}, "slow" );
			//$scrollingDiv.animate({"marginTop": "0px"}, "slow");
		}
		$(".work_thumb").fadeIn();
		});
		
		
}
/////// RELATED

$(".related a").live('click', function(){
		var post_id = $(this).attr("rel");
		var original_post = $(this).parent().parent().parent().parent().parent().parent().parent();
		original_post.prepend("<div class='loading'>...Loading</div>");
		 h2prepareIt(post_id,original_post); 
        $(this).addClass( "current" );
	return false;				   
});

///// CLIENT LINK

/*
$("a.client_link").live('click', function(){
		var href = "client/"+$(this).attr("title");
		$.bbq.pushState({ url: href });

return false;				   
});*/

//////// BBQ

/* Categories*/

	$('#menu-categories a').click(function(){
		if ( $(this).hasClass('selected')){
			var href = "closecategory";
			$.bbq.pushState({ url: href });
		}
		else {
			var href = "category/"+$(this).attr("title");
			$.bbq.pushState({ url: href });
		}
		return false;
	});

/* Clients*/

	$('#client_list a').click(function(){
		if ( $(this).hasClass('selected')){
			var href = "closeclient";
			$.bbq.pushState({ url: href });
		}
		else {
			var href = "client/"+$(this).attr("title");
			$.bbq.pushState({ url: href });
		}
		return false;
	});

/* Pages*/

	
	$('#page_navigation a').click(function(){
		if (!$(this).parent().hasClass("external")){
			var href = $(this).attr("href");
			$.bbq.pushState({ url: href });
			return false;
		} 
	});

/* All Link .capture*/

	$("a.capture").live('click',function(){
		var href = $(this).attr( "href" );
		$.bbq.pushState({ url: href });
		return false; 
	}); 



///////// BBQ HASCHANGE



$(window).bind( "hashchange", function(e) {
    // In jQuery 1.4, use e.getState( "url" );
	bbq_works();
	bbq_pages();
	bbq_categories();
	bbq_clients();
	
});

/*----------*/

function bbq_works(){
	var url = $.bbq.getState( "url" );
    $(".works").each(function(){
		$(this).click(function(e){
			if($(e.target).is('a')) return;				   
			$(this).find('a.posturl').click();				   
		});
			
		var href = $('a.posturl', this).attr( "href" );
		var post_id = $('a.posturl', this).attr("rel");
		var original_post = $(this);
      if ( href === url ) {
		closePage();
		original_post.prepend("<div class='loading'>...Loading</div>");
		 h2prepareIt(post_id,original_post);
        $('a.posturl', this).addClass( "current" );
		//_gaq.push(['_trackEvent', 'Related Works', 'View', href]);
      } else {
      $('a.posturl', this).removeClass( "current" );
      }
    });
}

/*----------*/

function bbq_pages(){
	var url = $.bbq.getState( "url" );
	$("#page_navigation a").each(function(){
		$(this).click(function(e){
			if($(e.target).is('a')) return;				   
		});
		
		href = $(this).attr("href");
		if ( href === url ) {
			closeWork();
			closeCategory();

			$(this).addClass('selected');
			$('#main_loading').fadeIn(300);
			$("#page_wrapper").load(href, function(){
			$("#page_wrapper").fadeIn(100, function(){
			$(".3columns").columns(3,10);
			//$(".3columns").makeacolumnlists({cols:3,colWidth:0,equalHeight:true,startN:1});
										
			$('#work_wrapper').masonry();
			$.scrollTo(0,500);
			$('#main_loading').fadeOut(300);
		//_gaq.push(['_trackEvent', 'Page', 'View', href]);
		});
			
			});
		} else {
			$(this).removeClass('selected');								   
		}
    });
}

/*----------*/

function bbq_categories(){
	var url = $.bbq.getState( "url" );
	$("#menu-categories a").each(function(){
		$(this).click(function(e){
			if($(e.target).is('a')) return;				   
		});
		
		href = "category/"+$(this).attr("title");
		
		if (url === 'closecategory') {
			closeCategory();
		} else {
			if ( href === url ) {
			closePage();	
			closeWork();
			
			$('#client_list a span').remove();
			$('#client_list a').removeClass('selected');						
			$('#client_list a').animate({backgroundColor: '#333333'}, 300);
			
			$('#menu-categories a span').remove();
			$('#menu-categories a').removeClass('selected');
			$('#menu-categories a').animate({backgroundColor: '#999999'}, 300,'easeOutQuad');	
			
			$(this).append('<span class="close_category">X</span>');
			$(this).animate({backgroundColor: '#333333'}, 300,'easeOutQuad');				
			$(this).addClass('selected');	
			
			var colorClass = '.category-' + $(this).attr("title");
			$('#work_wrapper').children().not(colorClass).not('.invis').toggleClass('invis').fadeOut(500);
			$('#work_wrapper').children(colorClass+'.invis').toggleClass('invis').fadeIn(500);
			$('#work_wrapper').masonry();
		//_gaq.push(['_trackEvent', 'Category', 'View', href]);
		}}

    });
}

/*----------*/

function bbq_clients(){
	var url = $.bbq.getState( "url" );
	$("#client_list a").each(function(){
		$(this).click(function(e){
			if($(e.target).is('a')) return;				   
		});
		
		href = "client/"+$(this).attr("title");
		if (url === 'closeclient') {
			closeClient();
		} else {
			if ( href === url ) {
			closePage();	
			closeWork();
			
			$('#menu-categories a span').remove();
			$('#menu-categories a').removeClass('selected');
			$('#menu-categories a').animate({backgroundColor: '#333333'}, 300);						   
			
			$('#client_list a span').remove();
			$('#client_list a').removeClass('selected');						
			$('#client_list a').animate({backgroundColor: '#999999'}, 300);		
			
			$(this).append('<span class="close_category">X</span>');
			$(this).animate({backgroundColor: '#333333'}, 300,'easeOutQuad');				
			$(this).addClass('selected');	
			
			var colorClassClient = $(this).attr("title");
			$('#work_wrapper').children().not('.'+colorClassClient).not('.invis').toggleClass('invis').fadeOut(500);
			$('#work_wrapper').children('.'+colorClassClient+'.invis').toggleClass('invis').fadeIn(500);
			$('#work_wrapper').masonry();

		}}

    });
}

/*----------*/

function bbq_clientsInside(){
	var url = $.bbq.getState( "url" );
	$("a.client_link").each(function(){
		$(this).live('click',function(e){
			if($(e.target).is('a')) return;				   
		});
		href = "client/"+$(this).attr("title");

		
		//href = "category/"+$(this).text().toLowerCase();
		
		if (url === 'closeclient') {
			closeClient();
		} else {
			if ( href === url ) {
			closePage();	
			closeWork();
			// Prevent the default click behavior.
			var colorClassClient = $(this).attr("title");
			$('#work_wrapper').children(':not(.'+colorClassClient+')').not('.invis').toggleClass('invis').fadeOut(500);
			$('#work_wrapper').children('.'+colorClassClient+'.invis').toggleClass('invis').fadeIn(500);
			$('#work_wrapper').masonry();

		}}

    });
}

/*----------*/

$(window).trigger( "hashchange" );
});
