$(document).ready(function(){
	var objects		= $('ul.boxes > li.box');
	var maxItems	= 7;
	var countItems	= objects.length;
	var openItem	= 3;

	var closebox	= function(element) {
		if (element.length > 0) {
			element.slideUp();
			$.doTimeout(400,function(){
				element.removeClass('active');					
			})
			$('ul.boxes > li.box:nth-child(n + 5)').each(function() {
				$(this).switchClass('down','', 200);
			})
		}
	}

	openbox	= function(index,reload) {
		var box			= $('ul.boxes > li:eq(' + index + ')');
		var element 	= $('.' + box.attr('id'));
		
		/* CSS class for position */
		element.addClass('active');

		if (element.length > 0) {
			
			if (element.is(':hidden')) {
				element.slideDown();

				$('ul.boxes > li.box:nth-child(n + 5)').each(function() {
					$(this).switchClass('','down', 200);
				})
				
				if (reload != 1) {
					$('#backgroundimage > div').animate({opacity: 0},200,function(){
						$('#backgroundimage > div').css('background-image','url(/uploads/photos/' + box.attr('background') + ')');
						$('#backgroundimage > div').animate({opacity: 1},400,function(){})
					})
					$.ajax({url: "/index/mediabox/", type: "POST", data: ({id : box.attr('id')}),dataType: "json" })
				}
			} else {
				closebox(element);
			}
		} else {
			if ($('#' + box.attr('id') + '_link').length > 0 && reload != 1) {
				$.ajax({url: "/index/mediabox/",async: false, type: "POST", data: ({id : box.attr('id')}),dataType: "json" })
				document.location.href	= $('#' + box.attr('id') + '_link').attr('href');
			}
		}

	}
	
	var refresh			= function() {
		objects			= $('ul.boxes > li.box');
	}


	var rotate = function(direction) {
		//console.log(direction);
		
		if (direction != 0) {
			objects.each(function(index, element) {
				
				next			= (direction > 0) ? index - 1 : index + 1;
				
				if (next < 0) {
					next	= countItems - 1;
				} else if (next >= countItems)	{
					next	= 0;
				}
				
				currentClass	= 'item' + ((index >= maxItems) ? '' : index);  
				nextClass		= 'item' + ((next >= maxItems) ? '' : next);
				
				$(this).switchClass(currentClass,nextClass, 100);
				
				if (index == ((direction < 0)?countItems - 1:0)) {
					$.doTimeout(100,function() {
						var li 		= $('#' + objects[index].id).detach();
						if (direction < 0) {
							$('ul.boxes').prepend(li);
						} else {
							$('ul.boxes').append(li);
						}
					});
				}
			})
			$.doTimeout(100,function() {
				refresh();
				direction	= (direction > 0) ? direction - 1 : direction + 1;
				if (direction != 0) {
					rotate(direction);
				} else {
					$.doTimeout(100,function() {
						openbox(openItem);
					})
				}
			});
//		

		}
	}
	
	/* START the process */
	refresh();
	objects.each(function(index) {
		$(this).addClass('item' + ((index > maxItems)?'':index));
		$(this).unbind();
		$(this).click(function() {
			var i		= $(this).prevAll().length;
			if (i - openItem == 0) {
				openbox(openItem);
			} else {
				var box			= $('ul.boxes > li:eq(3)');
				var element 	= $('.' + box.attr('id'));
				if (!element.is(':hidden')) {
					closebox(element);
					$.doTimeout(400,function() {
						rotate(i - Math.floor(maxItems/2));
					});
				} else {
					rotate(i - Math.floor(maxItems/2));
				}
			}
		});
	})
	
	$('#twitterClose > a').click(function() {
		$('#wrapperTwitter').hide();
		$('#wrapperTwitterClosed').show();
		$.ajax({url: "/index/twitter/", type: "POST", data: ({status : 0}),dataType: "json" })
	})
	$('#wrapperTwitterClosed > a').click(function() {
		$('#wrapperTwitterClosed').hide();
		$('#wrapperTwitter').show();
		$.ajax({url: "/index/twitter/", type: "POST", data: ({status : 1}),dataType: "json" })
	})

});

