(function($) {
	jQuery.jQueryRandom = 0;
	jQuery.extend(jQuery.expr[":"], {
		random: function(a, i, m, r) {
			if (i == 0) { jQuery.jQueryRandom = Math.floor(Math.random() * r.length); };
			return i == jQuery.jQueryRandom;
		}
	});

$(document).ready(function() {
	if($('#next_back')) {
		///////////////////////////////////////////////////
		///////// PARTNERS NEXT /////////
		$('#next').click(function() {
			var active = $('#box_controls').find('.active');
			var parent = $(active).parent('a');
			var next = $(parent).next('a').attr('href');
			if(!next) { next = $($('#box_controls').find('a').get(0)).attr('href'); }
			window.location = next;
		});
		
		///////////////////////////////////////////////////
		///////// PARTNERS BACK /////////
		$('#back').click(function() {
			var active = $('#box_controls').find('.active');
			var parent = $(active).parent('a');
			var prev = $(parent).prev('a').attr('href');
			if(!prev) {
				var links = $('#box_controls').find('a');
				var length = $(links).length -1;
				prev = $($(links).get(length)).attr('href');
			}
			window.location = prev;
		});
	}


/////////////////////////////////////////////////////////
//////////// PROJECT BOXES ////////////
/////////////////////////////////////////////////////////
	if($('.proj_row')) {
		$('.proj_img div').click(function() {
			if($(this).hasClass('activeproject')) {
				closeProject(this);
				return;
			} else {
				closeProject($(this).parents('.proj_img').find('.activeproject').get(0));
			}

			$(this).addClass('activeproject');
			$(this).animate({'height': '258px'}, 'slow');
			$($(this).find('img').get(0)).animate({'margin-top': '0px'}, 'slow');
		});

		if($('#prort_proj_view')) {
			$($(".proj_img").find('div').get(0)).click();
		} else {
			$(".proj_img").find('div:random').click();
		}
	}
	
	if($('#sidemenu')) {
		$('#sidemenu li.main_level ul').hide();
		$($('#sidemenu').find('.current_page_item').get(0)).parent().show();
		$('#sidemenu a.main_link').click(function() {
			$(this).next('ul').toggle();
		})
	}


//////////////////////////////////////////////////////////////
//////////// HOME PAGE BOXES ////////////
//////////////////////////////////////////////////////////////
	if($('.cat_row')) {
		var stophide = false;
		$('.cat_row').find('.port_link').click(function() {
			stophide = true;
			return;
		});

		$('.cat_row').click(function() {
			if($(this).hasClass('activehome')) {
				if(stophide == false) { closeHome(this); }
				return;
			} else {
				closeHome($('.cat_row').parents().find('.activehome').get(0));
			}

			$(this).addClass('activehome');
			$($(this).find('.cat_title div').get(0)).animate({'margin-top': '145px'}, 'slow');
			$($(this).find('.cat_img').get(0)).animate({'height': '320px'}, 'slow');
			$($(this).find('.cat_img div').get(0)).animate({'height': '322px'}, 'slow');
			$($(this).find('.cat_img img')).animate({'top': '0px'}, 'slow');
			$($(this).find('.cat_title span').get(0)).animate({'height': '320px'}, 'slow');
		});

		$(".cat_row:random").click();
		$(".cat_img").each(function() {
			$(this).find('img:random').show();
		});
	}


////////////////////////////////////////////////////////////
//////////// HOME LINK CHECK ////////////
////////////////////////////////////////////////////////////
	if($('.cat_title')) {
		$('.cat_title a').click(function() {
			if($(this).parents('.activehome').length == 0) {
				$(this).parents('.cat_row').click();
				return false;
			}
		});
	}
});

///////////////////////////////////////////////////////////////////////////
//////////// HOME PAGE BOXES CLOSE ////////////
///////////////////////////////////////////////////////////////////////////
function closeHome(this_ref) {
	$($(this_ref).find('.cat_title div').get(0)).animate({'margin-top': '0px'}, 'slow');
	$($(this_ref).find('.cat_img').get(0)).animate({'height': '20px'}, 'slow');
	$($(this_ref).find('.cat_img div').get(0)).animate({'height': '20px'}, 'slow');
	$($(this_ref).find('.cat_img img')).animate({'top': '-200px'}, 'slow');
	$($(this_ref).find('.cat_title span').get(0)).animate({'height': '0px'}, 'slow', '', function() {
		$(this_ref).removeClass('activehome');
	});
}

///////////////////////////////////////////////////////////////////////
//////////// PROJECT BOXES CLOSE ////////////
///////////////////////////////////////////////////////////////////////
function closeProject(this_ref) {
	$(this_ref).animate({'height': '20px'}, 'slow');
	$($(this_ref).find('img').get(0)).animate({'margin-top': '-120px'}, 'slow');
	$(this_ref).removeClass('activeproject');
}

})(jQuery);
