var initHovers = function()
{
	$('#hover > div').hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		}
	);
}
var initPopup = function()
{
	$('#hiddenModalContent a.close').click(function(e){
		e.preventDefault();
		tb_remove();
		return false;
	});
}
var initAjax = function()
{
	$('ul.content a.title-section').click(function(e){
		e.preventDefault();
		var link = $(this).attr('rel');
		if (link){
			if ($(this).parent().find('div.section-content').size()==0){
				var content = $('<div class="section-content">').insertAfter($(this));
				content.hide();
				$(content).load(link, function(){
				content.slideDown('slow', function(){
				   $('a[@rel*=jqlightbox]').lightBox();
				   $(document).ready(function(){   
						tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
						imgLoader = new Image();// preload image
						imgLoader.src = tb_pathToImage;
					});
					if(isIE){addIECorners(); }else {addCorners();};
					fixTrans();
					initSubItems(content);
					});
				$(this).parent().addClass('open');
				});
			}
			else{
				$(this).parent().find('div.section-content').slideToggle('slow');
				$(this).parent().toggleClass('open');
			}
		}
	});
}

var initSubItems = function(content){
	if (content.find('a.item-link').size()){
		content.find('a.item-link').click(function(e){
			e.preventDefault();
			var link = $(this).attr('rel');
			var parent = $(this).parents('div.list');
			
			$("div.list").find("li").each(function(i) {
				$(this).removeClass("active");
			});
			$(this).parents('li').addClass("active");
			
			if (link){
				if ($(this).parents('div.section-content').find('div.table').size()){
					$(this).parents('div.section-content').find('div.table').slideUp('slow',function(){
					$(this).remove();
					var itemCont = $('<div class="table">').insertAfter(parent);
					itemCont.hide();
					$(itemCont).load(link, function(){
						itemCont.slideDown('slow', function(){
							$('a[@rel*=jqlightbox]').lightBox();
							$(document).ready(function(){   
								tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
								imgLoader = new Image();// preload image
								imgLoader.src = tb_pathToImage;
							});
							if(isIE){addIECorners(); }else {addCorners();}
							initCloseLink(itemCont);
							});
						});
					});
				}
				else{
					var itemCont = $('<div class="table">').insertAfter(parent);
					itemCont.hide();
					$(itemCont).load(link, function(){
					itemCont.slideDown('slow',function(){
						$('a[@rel*=jqlightbox]').lightBox();
						$(document).ready(function(){   
							tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
							imgLoader = new Image();// preload image
							imgLoader.src = tb_pathToImage;
						});
						if(isIE){addIECorners(); }else {addCorners();}
						initCloseLink(itemCont);
						});
					});
				}
			}
		});
	}
}

var initCloseLink = function(itemCont){
	itemCont.find('a.close').click(function(e){
		e.preventDefault();
		$(this).parents('div.table').slideUp('slow',function(){$(this).remove();})
	});
}

$(document).ready(function(){
	initHovers();
	initPopup();
	initAjax();
})
