$(document).ready(function(){
	
	window.onunload = function(){};
	
	var content_width = 0;
	
	var menu_width =  $('#menu').width();
	
	$('#menu li img').load(function(){
		
		menu_width = $('#menu').width();

		set_menu_location(false);
	});
	
	function set_menu_location(resize) {
		
		if (jQuery.browser.msie && jQuery.browser.version < 8) { 
			
			$('#menu').css('margin-right',$('body').width()-content_width-12);//ie
			if (!resize) $('#menu li.back').css('margin-right',$('body').width()-content_width);//ie
			else {
				$('#menu li.back').css('left',15);
			}
		} else {
		
			$('#menu').css('margin-left',content_width-menu_width);
			$('#menu li.back').css('left',$('#menu li.current').offset().left);
		
		}
		
	}
	
	// menu control
	$(window).bind('resize', function() {
		
		content_width = 0;
		
		$('#content div.post').each(function(){
			if ($(this).offset().left+$(this).width() > content_width) content_width = $(this).offset().left+$(this).width();
		});
		
		
		set_menu_location(true);
	});
	
	$('#content div.post').each(function(){
		
		$('#'+$(this).attr('id')).cycle('destroy');
		
		$('#'+$(this).attr('id')).data('current_img',1);
		
		$(this).children('img').css('display','block');
		
		if ($(this).offset().left+$(this).width() > content_width) content_width = $(this).offset().left+$(this).width();
		
		/*if ($(this).offset().right > content_width) content_width = $(this).offset().right;
		
		console.log(content_width);*/
	});
	
	$('#content div.post').click(function(){
		
		document.location = $(this).children('.href').html();
		
	});
	
	$('#content div.post').mouseenter(function(){
		
		var this_ = $(this);
		
		$('#'+$(this).attr('id')).cycle({
			slideExpr: 'img',
			fx:      'scrollRight',//,
			//next:   $(this),
			delay: 0,
			speed:300,
			continuous:0,
			timeout:1000,
			//after: imageCount
			before: imageCount
			
		});
		
		$('#'+$(this).attr('id')).cycle('next');
		
		function imageCount(curr,next,opts) {
			$('#'+this_.attr('id')).data('current_img',opts.currSlide + 1);
		
		}
		
		$(this).children('span.title').clearQueue();
		
		$(this).children('span.title').animate({
   			
   			'height': 84,
   			'border-bottom-width':'4px'
   			
		});
		
		
		/*$(this).children('span.title').not($(this).children('span.title')).each(function(){
		
			if ($(this).css('height')>0) {
				$(this).animate({
					'height': 0,
					'border-bottom-width':'0px'
				});
			}
		
		});*/
		
		
	});
	
	$('#content div.post').mouseleave(function(){
		
		for (i=$(this).data('current_img')+1;i>1;i--) {
			$('#'+$(this).attr('id')).cycle('prev');
		}
		
		$('#'+$(this).attr('id')).cycle('destroy');
		
		
		$(this).children('span.title').animate({
   			
   			'height': 0,
   			'border-bottom-width':'0px'
   			
		});
		
		/*$(this).children('span.title').animate({
   			
   			'height': 0,
   			'border-bottom-width':'0px'
   			
		},500,function(){
			$(this).clearQueue();
		});*/
	});
	
	if (jQuery.browser.msie && jQuery.browser.version < 8) set_menu_location();
	set_menu_location(false);

})
