jQuery(document).ready(function(){
	
	//setup
	order_array_html=[];
	order_array_box=[];
	
	
	box_open='';
	bussy=false;
	org_pos=null;
	org_top=null;
	
	count_first_setup();
	
	
		
	
	
	
	
	
	var ii=0;
	
	
	
	
	$('.box').each(function(){
		var current_id=$(this).attr('id');
		var first_image = $('#'+current_id+' img:eq(0)');
		first_image.addClass('first');
		var clone=first_image.clone();
		$('#'+current_id+' img:eq(0)').remove();
		$(this).prepend(clone);
		 
	});
	
	
	$('.posts img').removeAttr('width');
	$('.posts img').removeAttr('height');
	 
	$('#button_reload').click(function(){
		
		
	
		
	});
	
	
	$('.button').live('click',function(){
			
			var windowHeight=parseInt($(window).height());
			var windowWidth=parseInt($(window).width());
			var left_position = ((windowWidth/2)-305);

			
			//$('.posts').css({overflow:'visible'});
			var scrollTop = $(window).scrollTop();
			
			if(scrollTop<=300){
					var new_top=300;
				}else{
					var new_top=scrollTop;
				}
			
			
			if(bussy==false){
				
				var id=$(this).attr('id');
				id=id.substring(4,id.lenght);
				idd=$('#id_'+id);
				var org_posi=idd.offset();
				
				var neww=idd.clone();
				
				neww.attr('id','new');
				
				
				$('body').append(neww);
				$('#new').wrap('<div id="posts_new" style="position:absolute; top:0; left:0px;"/>');
				$('#new').css({top:org_posi.top,left:org_posi.left});
				
				$('.box').css({opacity:0.1});
				$('#new').css({opacity:1,position:'absolute'});
				$('#new .button').css({display:'none'});
				$('#new .button_back').css({display:'block'});
				$('#new .button_back').click(function(){ 
					$('#posts_new').fadeOut(250,function(){
						$('#posts_new').remove();
						$('.box').fadeTo(750,1);
					});
					
					
					});

				$('#new').animate({width:610,left:left_position,top:new_top},1000,function(){
					$('#new .post_block').animate({'max-height':'1500'},500);
					//alert('!');
				});
				
				
			 	
				
			}
			
		});
	
	/* give font-faces tome to load, this is no solution at all !!! but works for now 
	$('body').animate({padding:0},1000,function(){
		$posts.vgrid();
	});
	*/
		
		$('#button_3').click(function(){
			var arr=[];
			arr[0]=3;
			show_loc(arr);
			$('#button_1').addClass('off');
			$('#button_4').addClass('off');
			$('#button_3').removeClass('off');
		});
		
		$('#button_4').click(function(){
			var arr=[];
			arr[0]=4;
			show_loc(arr);
			$('#button_1').addClass('off');
			$('#button_3').addClass('off');
			$('#button_4').removeClass('off');
		});
		
		$('#button_1').click(function(){
			var arr=[];
			arr[0]=1;
			show_loc(arr);
			$('#button_4').addClass('off');
			$('#button_3').addClass('off');
			$('#button_1').removeClass('off');
		});

});


function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}



function close_open_box(did){
	if(box_open){
			
			if(org_pos){
				
				var o={width:'300px','margin-bottom':10,left:org_pos,top:org_top};
			}else{
				
				var o={width:'300px','margin-bottom':10,left:org_pos,top:org_top};
			}
			
			$('#'+did+' .post_block').css({'max-height':'206px'})
			box_open.animate(o,300,function(){
			//$posts.vgrefresh();
			
			//alert(box_open.attr('id'));
			box_open.css({'z-index':0});
			$('.button').css({display:'inline'});
			$('.button_back').css({display:'none'});
			box_open='';
			bussy=false;
			$('.box').css({opacity:1});
			$('.social').hide();
			
		});
	}
}


function show_loc(l){
	
	$('.box').remove();
	var posts=$('.posts');
	for(var i=0; i<=order_array_box.length;i++ ){
		if(order_array_box[i] in oc(l)){
			posts.append(order_array_html[i]);
			//console.log(order_array_html[i]);
		}
		
	}
	
	setTimeout("$posts.vgrefresh();",250);
	
}

function count_first_setup(){
	var i=0;
	$('.box').each(function(){
			var id=$(this).attr('id');
			var dClass=$(this).attr('class');
			
			var box=dClass.substring(8,dClass.length);
			//console.log('box:'+box);
			var html=$(this);
			//console.log(id);
			//order_array[i]=id;
			
			order_array_html[i]=html;
			order_array_box[i]=box;
			
			i++;
		});
		//console.log(order_array_box.toString());
}

function img_2_lightbox(){
	$('.posts div.box a img').live('click',function(){
		//console.log('disable img link');
		//var h=$('.posts li a img').height();
		//preload_img($(this).parent().attr('href'),'#debug',false);
		return false;
	});
}


function preload_img(url,dom_replace,replace,callback){
	

	var _url = url;
	//alert(dom_replace);
	// set up the node / element
	_im =$("<img>");
	
	// hide and bind to the load event
	_im.hide();
	
	_im.bind("load",function(){ 
		
		if(replace){
			$(dom_replace).replaceWith(_im);
		}else{
			$(dom_replace).html(_im);
		}
		_im.show();
		if (typeof callback == "function"){
			callback();
		}
	});
	
	
	
	// set the src attribute now, after insertion to the DOM
	_im.attr('src',_url);

}

 
