$(function () {
//////////////////////////////////////////////////////////////////////////
////// START - CODE FOR LOADING THE TOP MENU CHASING BAR
//////////////////////////////////////////////////////////////////////////

	var $doc = $(document), $win = $(window), $chaser, $forch,
		forchBottom, visible;
	function shown() {
		visible = true;
	}
	function hidden() {
		visible = false;
	}
	$chaser = $('#header ul.menu').clone().hide().appendTo(document.body).wrap("<div class='chaser'></div>");
	$forch = $('.forchaser').first();
	forchBottom = $forch.height() + $forch.offset().top;
	hidden();
	$win.bind('scroll', function () {
		var top = $doc.scrollTop();
		if (!visible && top > forchBottom) {
			$chaser.slideDown(300, shown);
		} else if (visible && top < forchBottom) {
			$chaser.slideUp(200, hidden);
		}
	});
	

jQuery(document).ready(function(){
	var $p_filter_height = '';
	jQuery('ul.p_filter li:first-child a').addClass('current');
	jQuery('.p_cat_filter').attr({name: jQuery('ul.p_filter li a.current').attr('name')});
		
	jQuery('ul.p_filter li a').click(function(){
		newFilter = jQuery(this).attr('name');
		newFilterText = jQuery(this).text();
		jQuery('.p_cat_filter').text(newFilterText).attr({title:newFilterText}).attr({name:newFilter});
	});
	
	jQuery('.p_filter_container').hover(function(){
		jQuery(this).find('.p_cat_filter').addClass('hover');
		$p_filter_height = jQuery(this).find('ul.p_filter li').length * 28;
		jQuery(this).find('ul.p_filter').stop().animate({height: $p_filter_height + 'px', paddingTop: '5px', paddingBottom: '5px'}, 250);
	}, function(){
		jQuery(this).find('ul.p_filter').stop().animate({height: 0, paddingTop: 0, paddingBottom: 0}, 250);
		jQuery(this).find('.p_cat_filter').removeClass('hover');
	});
	
	jQuery('.p_cat_filter').click(function(){
		return false;
	});
})



$(window).load(function() {
		

//////////////////////////////////////////////////////////////////////////
// SWAPPERS FROM SLIDESHOW
//////////////////////////////////////////////////////////////////////////
	var sp = 300;
	$('#swappers div.swapper').hover(
		function() {
			$(this).stop().animate({top:'-180px'}, {queue: false, duration: sp, easing: "easeOutExpo"});
			$(this).find('.bgr').stop().animate({'opacity':0.9}, {queue: false, duration: sp, easing: "easeOutExpo"});
			$(this).find('p').stop().animate({'opacity':1}, {queue: false, duration: sp, easing: "easeOutExpo"});
			$(this).find('.indicator').css('background-position','center top');
		},
		function() {
			$(this).stop().animate({top:'0px'}, {queue: false, duration: sp, easing: "easeOutExpo"});
			$(this).find('.bgr').stop().animate({'opacity':0.5}, {queue: false, duration: sp, easing: "easeOutExpo"});
			$(this).find('p').stop().animate({'opacity':0.5}, {queue: false, duration: sp, easing: "easeOutExpo"});
			$(this).find('.indicator').css('background-position','center bottom');
		}
	);


//////////////////////////////////////////////////////////////////////////
// ADD A LAST CLASS TO DIFFERENT ELEMENTS TO REMOVE MARGINS/PADDINGS
//////////////////////////////////////////////////////////////////////////
	$(".projects .items li:nth-child(4n)").addClass("last");
	

//////////////////////////////////////////////////////////////////////////	
// ADD THE TRIGGER OF THE TOOLTIPS
//////////////////////////////////////////////////////////////////////////
	$(".hasTip").tipTip();



	})// end of window load
})// end of window load


//////////////////////////////////////////////////////////////////////////	
// Image Preloader 
//////////////////////////////////////////////////////////////////////////	

jQuery(function(){
	var $imgContainerClassG = '.portfolio.gallery .post';
	var $images = jQuery($imgContainerClassG+' a img.image_shadow');
	var $max = $images.length;
	jQuery('.image_shadow_container a.preloader2').each(function(){
		jQuery('<span class="image_shadow_container_img" />').prependTo(jQuery(this));
	});
	$images.remove();
	if ($max > 0){
		LoadImage(0, $max);
	}
	
	function LoadImage(index, $max){
		if (index < $max){
			jQuery('<span id="img'+(index+1)+'" class="p_img_container" />').each(function(){
				jQuery(this).prependTo(jQuery('.post .image_shadow_container a.preloader2 .image_shadow_container_img').eq(index));
			});
			var $img = new Image();
			var $curr = jQuery('#img'+(index+1));
			jQuery($img).load(function(){
				jQuery($curr).append(this);
				jQuery(this).animate({opacity: 1}, 200, 'easeInOutSine', function(){
					jQuery(this).parent().parent().css({backgroundImage: 'none'});
					if (index != ($max-1)){
						LoadImage(index+1, $max);
					}
				});
			}).error(function(){
				jQuery($curr).remove();
				LoadImage(index+1, $max);
			}).attr({
				src: jQuery($images[index]).attr('src'), 
				title: jQuery($images[index]).attr('title'), 
				alt: jQuery($images[index]).attr('alt')
			}).addClass(jQuery($images[index]).attr('class'));
		}
		
		if (index == ($max-1)){
			if (jQuery('#middle #home').find('.p_options_block').html() != null){
				LoadSorting2();
			}
		}
	}
	
	function LoadSorting2(){
		if (jQuery.browser.msie && jQuery.browser.version < 9){
			jQuery('.p_options_loader').css({display:'none'});
			jQuery('.p_options_block').css({display:'block'});
		} else {
			jQuery('.p_options_loader').css({display:'none'});
			jQuery('.portfolio').cmsmasters_portfolio_sort({items:'.post'});
		}
	}
});

jQuery(function(){
	var $imgContainerClass = '.portfolio';
	var $images = jQuery($imgContainerClass+':not(.gallery) .post a img.image_shadow');
	var $max = $images.length;
	jQuery('.image_shadow_container a.preloader').each(function(){
		jQuery('<span class="image_shadow_container_img" />').prependTo(jQuery(this));
	});
	$images.remove();
	if ($max > 0){
		LoadImage(0, $max);
	}
	
	function LoadImage(index, $max){
		if (index < $max){
			jQuery('<span id="img'+(index+1)+'" class="p_img_container" />').each(function(){
				jQuery(this).prependTo(jQuery('.post .image_shadow_container a.preloader .image_shadow_container_img').eq(index));
			});
			var $img = new Image();
			var $curr = jQuery('#img'+(index+1));
			jQuery($img).load(function(){
				jQuery($curr).append(this);
				jQuery(this).animate({opacity: 1}, 200, 'easeInOutSine', function(){
					jQuery(this).parent().parent().css({backgroundImage: 'none'});
					if (index != ($max-1)){
						LoadImage(index+1, $max);
					}
				});
			}).error(function(){
				jQuery($curr).remove();
				LoadImage(index+1, $max);
			}).attr({
				src: jQuery($images[index]).attr('src'), 
				title: jQuery($images[index]).attr('title'), 
				alt: jQuery($images[index]).attr('alt')
			}).addClass(jQuery($images[index]).attr('class'));
		}
		
		if (index == ($max-1)){
			if (jQuery('#middle #home').find('.p_options_block').html() != null){
				LoadSorting();
			}
		}
	}
	
	function LoadSorting(){
		if (jQuery.browser.msie && jQuery.browser.version < 9){
			jQuery('.p_options_loader').css({display:'none'});
			jQuery('.p_options_block').css({display:'block'});
		} else {
			jQuery('.p_options_loader').css({display:'none'});
			jQuery('.portfolio').cmsmasters_portfolio_sort({items:'.post'});
		}
	}
});
	

