jQuery(function( $ ){	

	$('#pageWrap').hide();

	$(document).ready(function(e){
		
		$('#pageWrap').hide();
	
		var allowClick = true;
		
		$('.loader').fadeTo(0, 0);	
		
		var numberItems = $('.row').size();

		if ($.browser.name == "msie") {
			$('#pageWrap').css('height','850');
		}
		


		var col = 'hsl(175, 97, 84)';
		var h = 175;
		
		function getColor() {
			h = h - 10;
			if( h < 115) {
				h = 205;
			}
			col = 'hsl(' + h + ', 40%, 64%)';	
			return col;		
		}
		
		function toggleAll() {	
			
			$('.thumb').fadeTo(300, 1);	
			$('.loader').fadeTo(200, 0);
			for(i=0;i< numberItems;i++) {
				$('#row-'+i).slideUp(400);
				$('#row-'+i).empty();			
			}
		}
		
		$('#pageWrap .item').css('float','left');
	
		oldid = "";
		lastclicked = "0";	
		
		$('#pageWrap .item .content #rowNumber').css("visibility","hidden");
		$('#pageWrap .item .content').hide();
		
		$('#pageWrap').fadeIn(500);
		
		$('#pageWrap .item .thumb').click(function () {	
			
					
				if(allowClick == false){
					return;
				}
				
				the_id = (this.id).substr(5);
				
				if(the_id == lastclicked) {
					toggleAll();
					return;
				}
				
				allowClick = false;
		
				 $('.videoContent').hide();				
				
				toggleAll();				
									
				$(this).fadeTo(300, 0.5);
				
				$('#loader-'+the_id).fadeTo(200, 1);			
								
				therow = $('#post-' + the_id + ' #rowNumber').attr('class').substr(4);
				
				thecontent = $('#post-'+ the_id + ' #rowNumber').html();	
				
				oldid = therow;
				
				lastclicked = the_id;	
				
			
				    																
					$('#row-'+oldid).empty();
										
					$('#row-'+therow).css('display', 'none');
					$('#row-'+therow).html(thecontent);
					
					$('#row-'+therow+' .videoContent').css('display', 'none');
	
					$('#row-'+therow).slideDown(600, 'easeOutExpo', function(){
																	
						$('#loader-'+the_id).fadeTo(200, 0);	
						$('#fake').fadeOut(300, function(){
							$('#row-'+therow+' .videoContent').css('display', 'inline');
						});
						var tempC = getColor();
						var time = 5000;
						$('.infoText a').css('border-color', tempC);
						$('.logoBG').animate({backgroundColor: tempC , queue: false}, time);		
						$('.first').animate({color: tempC , queue: false}, time);	
						$('.navBlock').animate({backgroundColor: tempC , queue: false}, time);	
						$('.closeButton').animate({backgroundColor: tempC , queue: false}, time);	
						allowClick = true;	
						
					});
					
					$.scrollTo($('#row-'+therow), 900, {easing:'easeOutQuart', offset:-60} );
								
			})		
		
	})
	
});
