/**
 * Portfolio Menu
 */
$(document).ready(function(){
	$('body.create.portfolio #port-menu .trigger').click(function(){
		$(this).toggleClass('open');
		$('#port-menu .primary').toggle();
	});
	$('body.create.portfolio #port-menu .primary').mouseleave(function(){
		$('#port-menu .trigger').toggleClass('open');
		$(this).toggle();		
	});
	$('body.create.portfolio #port-menu li').each(function(){	
		if($(this).parent().hasClass('taxonomies')){		
			$(this).hover(
				function(){
					$(this).addClass('open');
					$('.shadow', this).show();				
				},
				function(){
					$(this).removeClass('open');
					$('.shadow', this).hide();
				}
			);
		}
	});
});

/**
 * Portfolio Media
 */
$(document).ready(function(){
	$('body.create.portfolio #media').before('<ul id="media-navigation"></ul>');
	$('body.create.portfolio #media li').each(function(){
		idx = $('body.create.portfolio #media li').index(this);
		$('#media-navigation').append('<li rel="'+(idx+1)+'">'+(idx+1)+'</li>');		
		$(this).hide();
	});
	$('#media-navigation li').click(function(){
		$('#media-navigation li').removeClass('selected');
		$(this).addClass('selected');
		$('body.create.portfolio #media li').hide();
		$('body.create.portfolio #media li:nth-child('+$(this).attr('rel')+')').fadeIn(500);
	});	
	$('#media-navigation li:first-child').click();
});

/**
 * Portfolio Pagination
 */
$(document).ready(function(){

	//alert(document.cookie);

	if($('#sample-projects').length == 0 || $('#sample-projects li').length < 16) return;
	ih = 119;
	setCtrls = function(){
		fb = $('#sample-projects').offset().top + $('#sample-projects').height();
		lb = $('#sample-projects ul').offset().top + $('#sample-projects ul').height();
		$('#sample-nav .p').hide();
		$('#sample-nav .n').hide();	
		if(lb > fb) $('#sample-nav .n').show();	
		if(parseInt($('#sample-projects ul').css('margin-top').replace(/px/g, '')) != 0) $('#sample-nav .p').show();	
	};
	$('#sample-projects').css('height', ih*3);
	$('#sample-nav').show();
	setCtrls();
	$('#sample-nav .n').click(function(){
		$(this).hide();
		t = parseInt($('#sample-projects ul').css('margin-top').replace(/px/g, ''));
		t -= $('#sample-projects').height();
		$("#sample-projects ul").animate({marginTop: t+'px'}, 1500, function(){
			$(this).css('margin-top', t+'px');
			setCtrls();
		});
	});	
	$('#sample-nav .p').click(function(){
		$(this).hide();
		t = parseInt($('#sample-projects ul').css('margin-top').replace(/px/g, ''));
		t += $('#sample-projects').height();
		$("#sample-projects ul").animate({marginTop: t+'px'}, 1500, function(){
			$(this).css('margin-top', t+'px');
			setCtrls();
		});
	});	
});

function setCookie(name, value){
	if(name != '') document.cookie = name + '=' + value;
}

function getCookie(name){
	if(name == '') return('');
	name_index = document.cookie.indexOf(name + '=');
         if(name_index == -1)
            return('');
         
         cookie_value =  document.cookie.substr(name_index + name.length + 1, 
                                                document.cookie.length);
         
         //All cookie name-value pairs end with a semi-colon, except the last one.
         end_of_cookie = cookie_value.indexOf(';');
         if(end_of_cookie != -1)
            cookie_value = cookie_value.substr(0, end_of_cookie);

         //Restores all the blank spaces.
         space = cookie_value.indexOf('+');
         while(space != -1)
              { 
              cookie_value = cookie_value.substr(0, space) + ' ' + 
              cookie_value.substr(space + 1, cookie_value.length);
							 
              space = cookie_value.indexOf('+');
              }

         return(cookie_value);
         }
