jQuery(document).ready(function() {
	/* portfolios preload */
	function portfolioInit($list){
		jQuery($list).find(img).preload({
			onComplete:function(data){
				jQuery($list).find(".portfolio-item").each(function(i){
					jQuery(this).find(".overlay").removeClass('preload').addClass("image_overlay").html('');
				});
				if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 9) {
					jQuery($list).find("img").css("visibility", "visible");
				}else{
					jQuery($list).find("img").animate({"opacity": "1"}, "slow");
				}
				jQuery($list).find(".portfolio_image a").each(function(){
					enable_image_hover(jQuery(this));									
				});
			}
		});
		
	}
	function portfolioQuicksand(list,data,easing){
		list.quicksand(data,{
					duration: 800,
					easing:easing,
					enhancement:function(a){
						portfolioInit(a);
					},
					attribute: function(v) {
						return jQuery(v).attr('data-id');
					}
		},function(a){
			if (typeof Cufon != "undefined"){
				Cufon.refresh();
			}
			jQuery("a[rel^='prettyPhoto']").prettyPhoto({theme:themeParam,slideshow:5000,autoplay_slideshow:false});
		});
	}
	jQuery.preload("ul.portfolio-list img",{
		onComplete:function (data){
			jQuery("ul.portfolio-list .overlay:eq("+data.index+")").removeClass('preload').addClass("image_overlay").html('');
			if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 9) {
				jQuery("ul.portfolio-list img:eq("+data.index+")").css("visibility", "visible");
			}else{
				jQuery("ul.portfolio-list img:eq("+data.index+")").animate({"opacity": "1"}, "slow");
			}
			image = jQuery(".portfolio_image:eq("+data.index+")").find('a:first');
			enable_image_hover(image);
		},
		onFinish:function(){
			jQuery('.portfolios.sortable').each(function(){
				var $filter = jQuery(this).find('#portfolio-filter');
				var $section = jQuery(this);
				var $list = jQuery(this).find('ul.portfolio-list');
				var sortableType = jQuery($filter).attr('data-sortable');
				var quickeasing = $list.attr('data-easing');
				if(sortableType == 'all'){
					$filter.find('li a').click(function(){
						if(jQuery(this).hasClass('current'))
							return;
						$filter.find('li a').removeClass('current');
						jQuery(this).addClass('current');
						var listOpts = $filter.attr('data-options');
						var category = jQuery(this).attr('title');
						jQuery.post(
							window.location.href,
							{
								portfolioAction:'sortable',
								portfolioParams:listOpts,
								category:category
							},
							function(data, textStatus){
								$section.find('.portfolio-content').find('div.pagenavi').remove();
								if(jQuery(data).length > 1){
									var page = jQuery(data)[1];
									$section.find('.portfolio-content').append(jQuery(page));
								}
								portfolioQuicksand($list,jQuery(data).find('li'),quickeasing);
							}
						);
					});
				}else{
					var $data = $list.clone();
					$filter.find('li a').click(function(){
						$filter.find('li a').removeClass('current');
						jQuery(this).addClass('current');
						var type = jQuery(this).attr('title');
						if(type == 'all')
							var $filteredData = $data.find('li');
						else
							var $filteredData = $data.find('li.'+type);
						portfolioQuicksand($list,$filteredData,quickeasing);
					});
				}
			});
		}
	});
	jQuery('.pagenavi.ajaxpage a').live('click',function(){
		var page = jQuery(this).attr('data-page');
		var cat = jQuery(this).attr('data-cat');
		var $section = jQuery(this).closest('.portfolios.sortable');
		var $list = jQuery($section ).find('ul.portfolio-list');
		var listOpts = $section.find('#portfolio-filter').attr('data-options');
		var quickeasing = $list.attr('data-easing');
		jQuery.post(
			window.location.href,
			{
				portfolioAction:'sortable',
				portfolioParams:listOpts,
				category:cat,
				portfoliopage:page
			},
			function(data, textStatus){
				var page = jQuery(data)[1];
				portfolioQuicksand($list,jQuery(data).find('li'),quickeasing);
				jQuery($section).find('.portfolio-content').find('div.pagenavi').html(jQuery(page).html());
			}
		);
	});
}
