var pixastic_parseonload = true;
$(document).ready(function() {

	$(".top_nav th:last-child").addClass('last').prev().addClass('last');
	$(".top_nav th").wrapInner('<div class="positioner"></div>').not(".top_nav th:last-child").after('<td>|</td>');
	$(".top_nav .positioner").append('<div class="line"></div>');
	$(".top_nav ul").wrap('<div class="bg"></div>');
	$(".top_nav th").has(".positioner .bg").hover(
		function(){$(this).addClass("hover").children().children(".bg").fadeIn(150)},
		function(){$(this).children().children(".bg").fadeOut(150);$(this).removeClass("hover")}
	);
	$(".index_column .power_sub_box th").append('<div class="black_line"></div>').wrapInner('<div class="positioner">');
	
	$(".carousel li").wrapInner('<div class="bg"></div>').children(".bg").append('<div class="mask"></div>');
	$(".carousel li").hover(
		function(){$(this).addClass("hover")},
		function(){$(this).removeClass("hover")}
	);
	if ($(".carousel").size()>0){$('.carousel ul').carousel();}
	
	if ($(".trail").size()>0){
		$('.trail li').not('.trail li:last-child').after(' | ');
		$('.trail li:first-child').addClass('first');
		$('.trail li:last-child').addClass('last');
	}
	$(".left_column .menu a").hover(
		function(){$(this).parent().addClass("hover")},
		function(){$(this).parent().removeClass("hover")}
	);
	if($('.image_box').size()>0){$(".right_column h1").detach().appendTo('.right_column .image_box');}
	$(".years td:last-child").addClass('last');
	$(".certificates_detail li").wrapInner('<div class="padder"></div>');
	$(".certificates_list li:nth-child(3n), .certificates_detail li:nth-child(3n)").not(".certificates_list li:last-child, .certificates_detail li:last-child").after('<br clear="all" />');
	$(".certificates_list li:last-child, .certificates_detail li:last-child").after('<br clear="all" />');
	
	
	
	$(".gallery_list li:last-child").after('<br clear="all" />');
	
	$(".gallery_albums li:nth-child(2n)").not(".gallery_albums li:last-child").after('<br clear="all" />');
	$(".gallery_albums li:last-child").after('<br clear="all" />');
	$(".gallery_albums li").wrapInner('<div class="padder"></div>');
	
	$(".direction li:nth-child(2n)").not(".direction li:last-child").after('<br clear="all" />');
	$(".direction li:last-child").after('<br clear="all" />');
	$(".direction li").wrapInner('<div class="padder"></div>');
	
	$(".gallery_album_detail li").wrapInner('<div class="bg"></div>');
	$(".gallery_album_detail li:last-child").after('<br clear="all" />');
	
	$(".vacancy_title a").click(function(){
		$(this).toggleClass("expanded").parent().nextAll(".vacancy_detail_description").slideToggle(400);
		return false;
	});
	
	/* GALLERY DETAIL */
	$(".gallery_album_detail a").click(function(){
		var url = $(this).attr("href");
		$.ajax({
			  url: url,
			  success: function(data) {
				$(".big_pic").remove();
				$(".previews").remove();
				$(".gallery_popup .close").after(data);
				$(".gallery_popup").width(754);
				var additional_height=$(".gallery_popup").height()+$(window).scrollTop()-$('.common_container').height()+180;
				if (additional_height>0){$('body').animate({paddingBottom:additional_height+'px'}, 300);}
				$(".shade").fadeTo(300, 0.83, function(){$(".gallery_popup").css({top:$(window).scrollTop()+50+'px', left:($(window).width()-$(".gallery_popup").width())/2+'px'}).slideDown(300, function(){do_show_scroll_bar()})});
				
				checkPrevNextPhoto();

				$(".previews ul li a").live("click", function(){
					var newImg = $(this).attr("href");
					$("#gallery_picture").attr("src", newImg);
					$(".previews ul li").removeClass("active");
					$(this).parent().addClass("active");
					checkPrevNextPhoto();
					return false;
				});
				
				$("a.next").live("click", function(){
					var newImg = $(this).attr("href");
					$("#gallery_picture").attr("src", newImg);
					var nextEl = $(".previews ul li.active").next("li").children("a");
					$(".previews ul li").removeClass("active");
					$(nextEl).parent().addClass("active");
					checkPrevNextPhoto();
					return false;
				});
				$("a.prev").live("click", function(){
					var newImg = $(this).attr("href");
					$("#gallery_picture").attr("src", newImg);
					var prevEl = $(".previews ul li.active").prev("li").children("a");
					$(".previews ul li").removeClass("active");
					$(prevEl).parent().addClass("active");
					checkPrevNextPhoto();
					return false;
				});
			}
		});
		return false;
	});
	
	function checkPrevNextPhoto() {
		var nextEl = $(".previews ul li.active").next("li").children("a");
		var prevelEl = $(".previews ul li.active").prev("li").children("a");
		if ($(nextEl).size() > 0) {
			/*$(".previews ul li").removeClass("active");
			$(nextEl).parent().addClass("active");*/
			var nextUrl = $(nextEl).attr("href");
			$("a.next").attr("href", nextUrl);
			$("a.next").show();
		} else {
			$("a.next").hide();
		}
		if ($(prevelEl).size() > 0) {
			var prevUrl = $(prevelEl).attr("href");
			$("a.prev").attr("href", prevUrl);
			$("a.prev").show();
		} else {
			$("a.prev").hide();
		}
	}
	/* /GALLERY DETAIL */
	
	/* CERTIFICATE DETAIL */
	
	function checkPrevNext(element) {
		if ($(element).parent().parent().next("li").children().children("a").size() > 0) {
			var nextUrl = $(element).parent().parent().next("li").children().children("a").attr("href");
			$("a.next").attr("href", nextUrl);
			$("a.next").show();
		} else {
			$("a.next").hide();
		}
		if ($(element).parent().parent().prev("li").children().children("a").size() > 0) {
			var prevUrl = $(element).parent().parent().prev("li").children().children("a").attr("href");
			$("a.prev").attr("href", prevUrl);
			$("a.prev").show();
		} else {
			$("a.prev").hide();
		}
	}
	
	$(".certificates_detail a").click(function(){
		var imgUrl = $(this).attr("href");
		$(".gallery_popup").width(490);
		$(".big_pic").remove();
		var licHtml = '<div class="big_pic"><a href="" class="prev"></a><a href="" class="next"></a><img id="lic_img" src="'+imgUrl+'" alt="" /></div>';
		$(".gallery_popup").append(licHtml);

		checkPrevNext($(this));

		$("a.next, a.prev").live("click", function(){
			var newImg = $(this).attr("href");
			$("#lic_img").attr("src", newImg);
			checkPrevNext($('a[href="'+newImg+'"]'));
			return false;
		});
		
		var additional_height=$(".gallery_popup").height()+$(window).scrollTop()-$('.common_container').height()+180;
		if (additional_height>0){$('body').animate({paddingBottom:additional_height+'px'}, 300);}
		$(".shade").fadeTo(300, 0.83, function(){$(".gallery_popup").css({top:$(window).scrollTop()+50+'px', left:($(window).width()-$(".gallery_popup").width())/2+'px'}).slideDown(300, function(){do_show_scroll_bar()})});
		return false;
	});
	/* /CERTIFICATE DETAIL */
	
	/* BIG POPUP */
	$(".big_popup").append('<div class="corner_lt"></div><div class="corner_rt"></div><div class="corner_lb"></div><div class="corner_rb"></div><div class="corner_t"></div><div class="corner_l"></div><div class="corner_r"></div><div class="corner_b"></div>');
	$(".big_popup .previews li:last-child").after('<br clear="all" />');
	$(".big_popup .close").click(function(){
		$(this).parent().slideUp(300, function(){$(".shade").fadeOut(300)});
		$(".scroll_bar").remove();
		$(".big_popup .previews ul").css("left",'0px');
		$('body').animate({paddingBottom:'0px'}, 300);
		return false;
	});
	
	function do_show_scroll_bar(){
		if ($(".big_popup .previews ul").width()-40>$(".big_popup .previews").width()){
			$('.big_popup .previews').append('<div class="scroll_bar"><div class="scroll_tab_container"></div></div>');
			var sliderOpts = {max: $(".big_popup .previews ul").width()-$(".big_popup .previews").width()-40, orientation: 'horizontal', value: 0, slide: function(e, ui) {$(".big_popup .previews ul").css("left","-"+ui.value+'px')}};
			$(".big_popup .scroll_tab_container").slider(sliderOpts);
	
			$('.big_popup .previews').mousewheel(function(event, delta) {
				$(".big_popup .scroll_tab_container").slider("value", $(".big_popup .scroll_tab_container").slider("value")-delta*30);
				$(".big_popup .previews ul").animate({left: "-" + $(".big_popup .scroll_tab_container").slider("value")},{duration:200,queue:false});
				return false;
			});
		}
	}
	/* /BIG POPUP */
	
	
	
	
	
	/* INDEX BOXES */
	$(window).resize(function(){
		do_equalize();
	});
	$(window).load(function(){
		do_equalize();
	});	
	
	function do_equalize (){
		$(".index_left_column .box").each(function(index){
			if ($(this).height()>$(".index_center_column .box:eq("+index+")").height()) {$(".index_center_column .box:eq("+index+")").css("height",$(this).height()+'px')}
			else {$(this).css("height",$(".index_center_column .box:eq("+index+")").height()+'px')};
		});
	}
	/* /INDEX BOXES */
	
	/* SEARCH NAV */
	$(".searchnav a").click(function(){
		var actionUrl = $(this).attr("href");
		$('#searchform').attr("action", actionUrl);
		$('#searchform').submit();
		return false;
	});
	/* /SEARCH NAV */
	
});

