var $j = jQuery.noConflict();
$j(function(){

	/*
	 *	COOKIES
	 */
	if (!$j.cookie("portfoliorows")) 			$j.cookie("portfoliorows", '1');
	if (!$j.cookie("projectdescriptions"))		$j.cookie("projectdescriptions", 'open');
	
	
	
	/*
	 * FEATURED PROJECTS
	 */
	var projects = $j('.front-post'); 
	projects.push($j('.front-post-last'));
	
	// Abs positioning for featured project descriptions
	projects.each(function(i){
		$j(this).hover(
			function(){ $j(this).find('.featured-content').css({opacity:'0.9'}).fadeIn(200); }, 
			function(){ $j(this).find('.featured-content').fadeOut(200);	}
		);
		
		// Style the Project info text for JS enabled browsers
		getBox(null, $j(this)).css({position:'relative'});
		$j(this).find('.featured-content').css(
			{	position:'absolute', top:'0px', left:'0px',
				height:'201px',	fontSize:'12px', 		
				zIndex:'999', background:'#000000', color:'#ffffff'
			}).hide();
		$j(this).find('.featured-content').find('p').css(
			{	padding:'10px 35px 0', color:'#ffffff' }
		);
		$j(this).find('.moretext a').addClass('jsContinueReading');
	});
	
	/*
	 * PORTFOLIO + deafult Project behaviour
	 */
	projects.push($j('.latest-post-wrap'));
	
	// Add general listeners for all front page projects
	projects.each(function(i){     
		$j(this).bind('mouseover', projectOver);     
		$j(this).bind('mouseout', projectOut);     
		$j(this).bind('click', projectClick);
		getBox(null, $j(this)).css({cursor:'pointer'});
	});

    function projectOver(e){     
		var bx = getBox(e, null);
		bx.find('h2 a').css({color:'#fff'});
	}  
	
	function projectOut(e){
		var bx = getBox(e, null);
		bx.find('h2 a').css({color:'#0B96D0'});		
	}
	
	function projectClick(e){
		var lnk = getBox(e, null).find('a:first').attr('href');
		window.location.href = lnk;
	}
	
	function getBox(e, target){
		var target = (e) ? $j(e.target) : target ;
		var box = target.closest('.front-post');
		if (!box.length) box = target.closest('.front-post-last');
		if (!box.length) box = target.closest('.latest-post-wrap');
		return box;
	} 
	
	/*
	 *	PORTFOLIO ROWS COOKIE
	 */
	var portfolioposts = $j('.latest-post-wrap');
	
	// More / Less projects buttons
	$j('#latest-wrap .content').append('<div id="show-me-more-button" style="width:672px;">More projects...</div>');
	$j('#latest-wrap .content').append('<div id="show-me-less-button" style="width:672px;">Back to the top</div>');
	$j('#show-me-more-button').click(moreProjectsClick);
	$j('#show-me-less-button').click(lessProjectsClick);
	
	var noOfRows = parseInt($j.cookie("portfoliorows"));
	initRows(noOfRows);
	
	function initRows(rows){		
		var showTotal = (rows==0) ? 4 : rows*4;
		portfolioposts.each(function(i){
			if (i < showTotal) { 
				$j(this).show();
			} else { 
				$j(this).hide(); 
			}
		});

		var showMeMore = $j('#show-me-more-button');
		var showMeLess = $j('#show-me-less-button');
		
		// Hide / reveal appropriate buttons
		if ( $j('.latest-post-wrap:hidden').size() ) {
			showMeLess.hide();
			showMeMore.fadeIn();
		} else {
			showMeMore.hide();
			showMeLess.fadeIn();
		}
		initDescriptions();
		
		$j.cookie("portfoliorows", ""+rows, { expires: 7 });
	}
	
	function moreProjectsClick(e){
		var dir = (e.target.id == "show-me-more-button") ? 1 : -1 ;
		noOfRows = parseInt($j.cookie("portfoliorows")) + dir;
		initRows(noOfRows);
		
		var sVal = (dir == 1) ? '+=330px' : '-=330px' ;
		$j('html, body').animate({
									scrollTop:sVal
								}, 800);
	}
	
	function lessProjectsClick(e){
		// Open tags
		//$j.cookie("viewtags", 'closed');
		//tagsTitleClicked(null);
		
		// Scroll Up
		$j('html, body').animate({
									scrollTop:0
								}, 600);
	}
	
	/*
	 *	HIDE / SHOW DESCRIPTIONS
	 */
	// create link + add listener
	var showhideaction = ($j.cookie("projectdescriptions")=='closed') ? 'Show' : 'Hide' ;
	$j('#latest-wrap > h2').prepend('<span id="jsShowHideDescriptions" style="font-size:14px; width:160px; float:right; text-align:center; position:relative; top:0px; background:#eee; padding:7px; color:#333; cursor:pointer;">'+showhideaction+' descriptions</span>');
	$j('#jsShowHideDescriptions').click(toggleDescriptions);

	function initDescriptions() {
		($j.cookie("projectdescriptions")=='closed') ? hideDescriptions() : showDescriptions();
	}
	
	function toggleDescriptions(e){
		if ($j.cookie("projectdescriptions")=='closed') {
			showDescriptions();
		} else {
			hideDescriptions();
		}
	}
	
	function showDescriptions(){
		$j('.latest-content').each(function(i){
			$j(this).fadeIn();
			$j(this).prev().css({'marginBottom':'0px'});
			$j.cookie("projectdescriptions", 'open');
		});
		$j('#jsShowHideDescriptions').html('Hide descriptions');
	}
	
	function hideDescriptions(){
		$j('.latest-content').each(function(i){
			$j(this).fadeOut();
			$j(this).prev().css({'marginBottom':'10px'});
			$j.cookie("projectdescriptions", 'closed');
		});
		$j('#jsShowHideDescriptions').html('Show descriptions');	
	}
	
	/*
	 *	VIEW TAGS COOKIE
	 */	
	if ($j('#js-is-front-page').length) $j('#tag_cloud-2').hide();
	/*
	var tagsTitle = $j('#tag_cloud-2 h2.widgettitle');
	var tagsDiv = $j('#tag_cloud-2 div');
	
	// Hide tags by default on the front page
	if ($j('#js-is-front-page').length) {
		tagsTitle.css({cursor:'pointer'}).html('View tags').click(tagsTitleClicked);
		tagsDiv.slideUp();
	}
	
	if (!$j.cookie("viewtags")) { $j.cookie("viewtags", 'closed', { expires: 1 }); };
	
	function tagsTitleClicked(e){
		if ($j.cookie("viewtags")=='closed') {
			tagsDiv.slideDown();
			tagsTitle.html('Close tags');
			$j.cookie("viewtags", 'open', { expires: 1 });
		} else {
			tagsDiv.slideUp();
			tagsTitle.html('view tags');
			$j.cookie("viewtags", 'closed', { expires: 1 });
		}
	}
	*/
	
}); 