$('html').removeClass('no-js').addClass('loading');

function init() {	
	nav();
	gallery();
	trans();
	
	$("img").mousedown(function(){
	    return false;
	});
	
	if ($("ul#news").length) ticker();
	
	
	$("a.lightbox").colorbox({transition:"fade"}).live('click', function(event){
		event.preventDefault();
	});
		
}

function ticker () {
	$('ul#news').cycle({
			timeout: 7500,
			pause: 2,
			containerResize: 0,
			slideResize: 0,
			width: "100%",
			fit: 1
	});
}

function trans () {
	var clicker = $('a').not(".nofollow, .thumb, .filter, .lightbox");
	
	if ($("#wrapper.index").length) {
		var faders = $('#menu, #content, #images, #text, #home li, #news, #footer');
		fadeInEach("#menu li.level1", 250);
		$('#thumbs, h2, #info').hide();
	} else if ($("#isotope").length) {
		var faders = $('h2, #filters, #footer');
		$("#images, #thumbs").remove();
	} else if ($(".imageElement").length) {
		var faders = $('#content, h2, #images, #thumbs, #text, #info, #footer');
	} else {
		var faders = $('#content, h2, #thumbs, #text, #info, #map, #footer');
	}

	faders.hide();
	$('logo').hide().fadeIn("slow");

	clicker.click(function(event){
		event.preventDefault();
		linkLocation = this.href;
		faders.fadeOut('normal', redirectPage);
		
		var list = $(this).parents("li.level1.active.clicked").find("a:first");
		if (list) list.click();		
	});

	function redirectPage() {
		window.location = linkLocation;
	}

	var i = 0;
	faders.each(function() {
		$(this).delay(i).fadeIn('slow');
		i += 500;
	});
	
	fadeInEach("#thumbs li", 250);
}

function  gallery() {
	if ($('.imageElement').length) {
		$('#images').after('<ul id="thumbs">');
		$('#gallery').cycle({
	        timeout: 5000,
			fx:     'fade', 
		    speed:  'slow', 
		    pager:  '#thumbs', 
			pause:         1, 
			slideResize: 0,
			containerResize: 0,
			pauseOnPagerHover: 1,

		    pagerAnchorBuilder: function(idx, slide) { 
				var $img = $(slide).find("img").attr("src");
		        return '<li><a href="#" class="nofollow"><img src="' + $img + '" /></a></li>'; 
		    }
		
	    });
	
	  // $('body').append('<div id="overlay"><div id="close">CLOSE</div><img src="" /><h3>&nbsp;</h3></div>');
	  // 	
	  // 		$("#overlay").hide();				
	  // 	
	  // 		$('.imageElement').click(function() {
	  // 			$img = $(this).find("img").attr("src");
	  // 			$title = $(this).find("img").attr("alt");
	  // 			
	  // 			$("#overlay").find("img").attr("src", $img).end().find("h3").text($title).end().fadeIn("slow");
	  // 						
	  // 		});
	  // 		
	  // 		$('#close').live('click', function() {
	  // 			$(this).parents("#overlay").fadeOut("slow", function() {
	  // 				// $('#gallery').cycle('resume'); 
	  // 			});
	  // 		});
	  // 		
	  // 		$('#overlay img').live('click', function(){
	  // 			$(this).parents("#overlay").fadeOut("slow", function() {
	  // 				// $('#gallery').cycle('resume'); 
	  // 			});		
	  // 		});
	
	} else {
		$('#images').hide();
	}
}

function tips() {
	$('.tips, a.zoom').qtip({
		position: { target: "mouse", adjust: { x: 20, y: 20 } },
		style: { tip: { corner: false } },
		show: { effect: function(offset) { $(this).fadeIn("slow"); } },
		hide: { effect: function(offset) { $(this).fadeOut("slow"); } }
	});
}

function nav() {

	$('#menu ul').accordion();
	
	$('#menu a.nofollow').click(function(e) {
		e.preventDefault();
	});
	
	$('li.level1.active a.nofollow:first').click();
	$('li.level2.active a.nofollow:first').click();
	$('li.level2.active a:first').addClass("highlight");
	$('li.level3.active a.nofollow:first').click();
}

function fadeInEach(list, speed) {
	var j = speed;
	$(list).hide().each(function() {
		$(this).delay(j).fadeIn(speed*2);
		j += speed;
	});
}
function redirectPage() {
	window.location = linkLocation;	
}
$(document).ready(function() {
	init();	
	$('html').removeClass('loading');
});

$(function() { setTimeout(function() { 
	if ($("div.pic").length) grid();
	}, 1000); 
});		

$(window).bind("unload", function() {
	//
});
$(window).resize(function() {
	// resize();
});
