
$(document).ready( function(){
	if($('#p-product-header').length)
	{
		bgColor = $('#p-product-header').css('background-color');
		if($('ul.portfolio-menu li.active li.active').length)
			$('ul.portfolio-menu li.active li.active').css('background-color',bgColor);
		else
			$('ul.portfolio-menu li.active').css('background-color',bgColor);

		$('.product-descr h1, .range-descr h1').css('color',bgColor);
		$('.product-descr h2, .range-descr h2').css('color',bgColor);
		$('.product-descr h3, .range-descr h3').css('color',bgColor);
		$('.product-descr h4, .range-descr h4').css('color',bgColor);
		$('.product-descr strong, .range-descr strong').css('color',bgColor);
		$('a.btn-sizes').css('background-color',bgColor);
	}
	
	 $(".portfolio-item-content").addClass('bo-hidden');
	$(".portfolio-subitem:not(.subitem-active) .portfolio-subitem-content").css('width','0px');
	
	/* clickable images in portfolio shifter */
	$('.portfolio-subitem-content').click( function () {
		var link = $(this).parent('.portfolio-subitem').find('.portfolio-subitem-link').attr('href');
		if(link) window.location.href = link;
	});
	
	/* Showing - hiding portfolio content*/
	
	$('.portfolio-item').bind('mouseenter', function(event) {
	    event.stopPropagation();
	    var $enteredBlock = $(this);
	    if(!$enteredBlock.find('.portfolio-item-content').hasClass('po-shown')) {
		$enteredBlock.data("showTimeout", setTimeout(function(){
		    animationPack.hideVertical($('.portfolio-leading-image'));
		    $(".portfolio-item-content").removeClass('po-shown');
		    $enteredBlock.find('.portfolio-item-content').addClass('po-shown');
		    animationPack.showVertical($enteredBlock.find('.portfolio-item-content'));
		    animationPack.hideVertical($(".portfolio-item-content:not(.po-shown)"));
		    //alert('sh');
		}, 200));
	    }
	});
	$('.portfolio-item').bind('mouseleave', function(event) {
	    clearTimeout($(this).data('showTimeout'));
	});
	
	/* SHOWInG HIDING garden rooms content*/
	
	$('.portfolio-subitem').bind('mouseenter', function(event){
	     event.stopPropagation();
	      var $enteredBlock = $(this);
	       if(!$enteredBlock.hasClass('subitem-active')) {
		
		$enteredBlock.data("showTimeout", setTimeout(function(){
		    $(".portfolio-subitem:not(.subitem-active)").css('width','0px');
		    animationPack.hideHorizontal($(".portfolio-subitem.subitem-active"));
		    animationPack.showHorizontal($enteredBlock, '480px');
		    $(".portfolio-subitem").removeClass('subitem-active');
		    $enteredBlock.addClass('subitem-active');
		    //alert('sh');
		}, 250));
	    }
	});
	$('.portfolio-subitem').bind('mouseleave', function(event) {
	    clearTimeout($(this).data('showTimeout'));
	    $(".portfolio-subitem:not(.subitem-active)").css('width','0px');
	});
	
	/* HEADERS */
	$('.range-descr h3').nextAll('*:not(h3)').hide();
	$('.range-descr h3').click(function(e){
	    e.preventDefault();
	    e.stopPropagation();
	    if (!$(this).hasClass('header-after-shown')) {
		$('.range-descr h3.header-after-shown').nextAll('*:not(h3)').slideUp(200);
		if($(this).nextAll('h3').length) {
		    $(this).nextUntil($(this).nextAll('h3').get(0), '*').slideDown(200);
		} else {
		    $(this).nextAll().slideDown(200);
		}
		$('.range-descr h3').removeClass('header-after-shown');
		$(this).addClass('header-after-shown');
	    } else {
		$(this).nextAll('*:not(h3)').slideUp(200);
		$('.range-descr h3').removeClass('header-after-shown');
	    }
	    
	});
	
	/* PORTFOLIO EXAMPLES LINKS */
	
	$('.portfolio-examples-content p').click(function() {
		var link = $(this).find('a.p-example-link').attr('href');
		if(link) window.location.href = link;
	});
	
	
	
	/* PORTFOLIO EXAMPLES LISTING */
	var i = 1;
	/*set pages hide all but fiirs page*/
	$('.portfolio-examples-content p:not(:first)').each(function(){
	    var pageNum = Math.ceil((i)/2);
	    $(this).addClass("page-"+pageNum);
	    i++;
	    if(pageNum > 1) $(this).hide();
	});
	/*generate paging*/
	for(var j = 0; j < (Math.ceil((i-1)/2)); j++) {
	    var $point = $('<span>', {
		'class': 'example',
		html : '&nbsp;',
		'data-page': (j+1)
	    });
	    if(j === 0) {
		$point.addClass('example-active');
	    }
	    $('.p-example-listing').append($point);
	}
	/*page swithing*/
	//$('.address').hide();
	var switchingTimeout;
	$('.p-example-listing').delegate('.example', 'click', function(e){
	   e.stopPropagation();
	   e.preventDefault();
	   if(!$(this).hasClass('example-active')) {
	       $('.portfolio-examples-content .example').removeClass('example-active');
	       $(this).addClass('example-active');
	       var pageClass = 'page-'+$(this).attr('data-page');
	       $('.portfolio-examples-content p:not(:first)').fadeOut(200);
	       switchingTimeout = setTimeout(function(){
		   $('.portfolio-examples-content p.'+pageClass).fadeIn(300);
	       }, 202);
	   }
	});
	
	
	/* READ MORE BUTTON*/
	$('.range-descr hr:first').each(function(){
		$(this).nextAll().hide();
		var $button = $('<div>',{
		html: '<img src="./img/portfolio/read-more.png" alt="read more"/>'});
		$button.find('img').css('cursor', 'pointer').click(function(){
			$(this).parent().nextAll().slideDown('fast');
			$(this).remove();
		});
		$(this).after($button);
		$(this).remove();
	});
	
	/* GARDEN ROOMS HASH */
	if(window.location.hash === "#garden-rooms") {
		$('#garden-rooms').trigger('mouseenter');
	}
	
	
	
});

var animationPack = {
    
    animateTime : 300,
    
    hideVertical : function($object){
	$object.slideUp(this.animateTime, 'swing');
    } ,
    
    
    showVertical : function($object){
	$object.slideDown(this.animateTime, 'swing');
    } ,
    
    hideHorizontal: function($object) {
	var scope = this;
	
	$object.animate({
	    width: '0px'
	  }, {
	    duration: scope.animateTime,
	    specialEasing: {
	      height: 'swing'
	    }
	   
	  });
    },
    
    showHorizontal: function($object, targetWidth) {
	var scope = this;
	
	$object.animate({
	    width: targetWidth
	  }, {
	    duration: scope.animateTime,
	    specialEasing: {
	      height: 'swing'
	    },
	    complete: function() {
		$object.css('width',targetWidth);
		animLock = false;
	    }
	  });
    }
}
