$(document).ready(function(){

	// Init Hovers
	if($.browser.msie){
		$('input:submit').hover(
			function(){$(this).addClass('hover');},
			function(){ $(this).removeClass('hover'); }
		);
		$('input:reset').hover(
			function(){$(this).addClass('hover');},
			function(){ $(this).removeClass('hover'); }
		);
		$('input:button').hover(
			function(){$(this).addClass('hover');},
			function(){ $(this).removeClass('hover'); }
		);
		$('ul.main_nav li').hover(
			function () {$(this).addClass('hover');},
			function(){$(this).removeClass('hover');}
		 );
	};
	
	//init Custom upload box
	$('input.upload').css('opacity', 0);
	$('input.upload').change(getStats);
	function getStats(){
	
		fullName = this.value;
		shortName = fullName.match(/[^\/\\]+$/);		
		$('#file-name').val(shortName);
	}

	//init Collapse
	$('div.comments div.title div a.show').click(function(e){
		e.preventDefault();
		$(this).parents('div.comments').addClass('active').find('div.comments_area').hide().slideDown('slow');
	});
	$('div.comments div.title div a.hide').click(function(e){
		e.preventDefault();
		$(this).parents('div.comments').removeClass('active').find('div.comments_area').show().slideUp('slow');
	});
	$('div.hide-button a').click(function(e){
		e.preventDefault();		
		if ($(this).text() == 'back to top') {
			$(document).scrollTo($('#content'),'slow');
		} else {
			var offset = $(this).parents('div.hide-block').position().top - $(document).scrollTop();
			if (offset<0) { offset = 0-offset+50; $(document).scrollTo('-='+offset,'slow');}
			$(this).parents('div.hide-button').prev().slideToggle('slow');
			$(this).text( $(this).text()=='hide'?'show':'hide');
			if ($(this).text()=='hide')
				$(this).removeClass('show').addClass('hide');
			else
				$(this).removeClass('hide').addClass('show');
		}
	});

	// Hide popup's on load
	/* $('.popup').hide(); 

	//Popup's [close] button
	$('.popup a.close').click(function(event){
		event.preventDefault();
		$(this).parents("div.popup").hide();
	});
	
	$('.popup div.buttons ul li:first input').click(function(event){
		event.preventDefault();
		$(this).parents("div.popup").hide();
	});*/
	

	// 5.1.6_ManageSystematicReviews_PopUp.psd - collapsing gray block in sidebar
	$('.gray-box a.hide').click(function(event){		event.preventDefault();
		$(this).parents("div.gray-box").removeClass('active');
		$(this).parents("div.gray-box").children("div.cont").slideUp('slow');
		$(this).parents("div.gray-box").children("div.categoryTree").slideUp('slow');	
		$(this).parents("div.gray-box").children("div.extended-search").slideUp('slow');
		$('.popup').hide();
	});
	$('.gray-box a.show').click(function(event){
		event.preventDefault();
		$(this).parents("div.gray-box").addClass('active');
		$(this).parents("div.gray-box").children("div.cont").slideDown('slow');
		$(this).parents("div.gray-box").children("div.categoryTree").slideDown('slow');
		$(this).parents("div.gray-box").children("div.extended-search").slideDown('slow');
	});

	//5.1.6_ManageSystematicReviews_PopUp.psd - popup on click [select categories] in sidebar
	$('.gray-box a.select_categories').click(function(event){		event.preventDefault();		$('.popup').show();	})

	//5.2.4_AER_PortalFindaSR(PopUp).psd - popup on click [Summarize This Review]
	$('a').click(function(event){		if ($(this).html() == 'Summarize This Review'){			event.preventDefault();			//get popup title
			title = $(this).parents('tr').children('td:first').children('p').html().substring(0,20);
			//set popup title
			$('.popup h4').html(title);
			//show popup
			$('.popup').css({
				'top': $(this).offset().top + 7,
				'left': $(this).offset().left + 190
			});
			$('.popup').show();
		};	});

	//5.1.3_ManageIndexes_popup.psd - popup on click [edit]
/*	$('a').click(function(event){
		if ($(this).html() == 'edit'){
			event.preventDefault();
			//get popup title
			title = $(this).parents('ul').parent('li').html(); 
			if ($.browser.msie){
				title = $.trim(title.substring(158));
			} else {
				title = $.trim(title.substring(138));
			}			
			//set popup title
			$('.popup #category-name').val(title);
			//show popup
			
			div_id = $('.popup'); 
			if ($.browser.opera)
		   		_height = window.innerHeight
		    else
		   	  _height =  $(window).height();		   
		   		   	   
		   var pleft = ($(window).width() - div_id.width())/2;
	       var ptop = (_height>div_id.height())?(_height-div_id.height())/2 + $(document).scrollTop():0;
	       div_id.css('margin-left', 0);
	       div_id.css('left',pleft+'px');
	       div_id.css('top',ptop+'px');
	       div_id.show();			
			
		};
	});
	
		$(window).resize(function(){
			if ($('.popup').get(0)){				
							
			   div_id = $('.popup');				
				
				if ($.browser.opera)
			   	_height = window.innerHeight
			   else
			   	_height =  $(window).height();
				
			   var pleft = ($(window).width() - div_id.width())/2;
		       var ptop = (_height>div_id.height())?(_height-div_id.height())/2 + $(document).scrollTop():0;
		       div_id.css('margin-left', 0);
		       div_id.css('left',pleft+'px');
		       div_id.css('top',ptop+'px');
		  }
			
		});*/


	//5.1.5.2_EvidenceAnalysis.psd - order of grey block
		
	function initBoxes(){
		$('a.move-up').show();
		$('a.move-down').show();
		
		li_len = $('ul.box_list > li > ul > li').length;
		$('ul.box_list > li > ul > li').each(function(indx, li){
			li.id = 'box-cont-'+(indx+1);		
						
			if (indx == 0){				
				$(li).find('a.move-up').hide();				
			}
			if (indx+1 == li_len){				
				$(li).find('a.move-down').hide();
			}
		});
		
		$('div.comments div.title div a.show').click(function(e){
			e.preventDefault();
			$(this).parents('div.comments').addClass('active').find('div.comments_area').hide().slideDown('slow');
		});
		$('div.comments div.title div a.hide').click(function(e){
			e.preventDefault();
			$(this).parents('div.comments').removeClass('active').find('div.comments_area').show().slideUp('slow');
		});
				
		$('a.move-down').unbind('click');
		$('a.move-up').unbind('click');
		
		$('a.move-down').click(function(event){
			event.preventDefault();
			box = $(this).parent().parent().parent().parent();
			moveBox(box, 'down');
		});
		$('a.move-up').click(function(event){
			event.preventDefault();
			box = $(this).parent().parent().parent().parent();
			moveBox(box, 'up');
		});
				
	}
	
	
	initBoxes();
	
	

	function moveBox(box_obj, direction){
		box = box_obj;		
		ul = box.parent().parent();
		boxes_length = ul.children('li').length;
		match = box.parent().attr('id').match(/^box-cont-(\d)$/); 				
		box_id = match[1];
		switch (direction){
			case 'up':
				swap_id = box_id - 1; 
			break;
			case 'down':
				swap_id = parseInt(box_id) + 1;
			break;
		}
		
		temp = $('#box-cont-'+swap_id).html();
		$('#box-cont-'+swap_id).html( $('#box-cont-'+box_id).html() );
		$('#box-cont-'+box_id).html(temp);
		
		/*
		if (swap_id == 1){
			$('#box-cont-'+swap_id)
		}
		
		renumberBoxes();
		*/
		initBoxes();		
	}

	//init Font reSizes
	if($.cookie('fclass')){
		var curFontSize = $.cookie('fclass');
		$('body').attr('className',curFontSize);
		$('ul.text_size a').removeClass('active');
		switch (curFontSize){
			case 'small': $('ul.text_size li.small_size a').addClass('active'); break;
			case 'normal': $('ul.text_size li.medium_size a').addClass('active'); break;
			case 'big': $('ul.text_size li.big_size a').addClass('active'); break;
		};
	};
	$('ul.text_size li.small_size a').click(function(e){
		e.preventDefault();
		$('ul.text_size a').removeClass('active');
		$(this).addClass('active');
		$('body').addClass('small');
		$('body').removeClass('big');
		$('body').removeClass('normal');
		$.cookie('fclass','small');
	});
	$('ul.text_size li.medium_size a').click(function(e){
		e.preventDefault();
		$('ul.text_size a').removeClass('active');
		$(this).addClass('active');
		$('body').addClass('normal');
		$('body').removeClass('small');
		$('body').removeClass('big');
		$.cookie('fclass','normal');
	});
	$('ul.text_size li.big_size a').click(function(e){
		e.preventDefault();
		$('ul.text_size a').removeClass('active');
		$(this).addClass('active');
		$('body').addClass('big');
		$('body').removeClass('normal');
		$('body').removeClass('small');
		$.cookie('fclass','big');
	});

});