var blockQuery = 0;
$(function() {
	$(".submenu").hide();

	$("#content").ajaxStart(function() {
		$(this).append('<div id="ajaxloader" class="loading"></div>');
	});
	$("#maincontent").ajaxStop(function() {
		$("#ajaxloader").remove();
	});
	
	function setResponse(obj){
		for(k in obj){
			jQuery(k).html(obj[k]);
		}	
	}
	
	$(".parent>span:first-child").click(function(){
			var $childlist = $(this).siblings('ul');
			var $opened = $("li.parent > ul:visible");

			if ($childlist.css("display") === 'none'){
				$childlist.queue(function(){$childlist.animate({
							"height": "show",
							"opacity": "show"
						},{
							duration: 750,
							specialEasing: {
								height: 'easeInOutQuad'
							}
						});
				});

				if ($opened.length > 0){
						$opened.animate({
							"height": "hide",
							"opacity": "hide"
						}, {
							duration: 750,
							specialEasing: {
									height: 'easeInOutQuad'
							}, 
							complete: function(){$childlist.dequeue();}
							}
						);
				}
				else{
					$childlist.dequeue();
				}
			}
			else{
				$childlist.animate({
					"height": "hide",
					"opacity": "hide"
				}, {
					duration: 750,
					specialEasing: {
							height: 'easeOutCubic'
					}});
			}
		});

		$("#togglemenu").toggle(function(){
			$("#wrap").animate({
				"width" : "1116px",
			});
			$("#content").animate({
				"margin-left" : "+=149"
			}, {
				duration: 700,
				specialEasing: {
					height: 'easeOutCirc'
			}});
		}, function(){
			$("#content").animate({
				"margin-left" : "-=149"
			},
			{
				duration: 700,
				specialEasing: {
					height: 'easeOutCirc'
			}});
			$("#wrap").delay(100).animate({
					"width" : "967px",
				}, 700);
		});
		
		$("a:not(.direct)").live("click", function(){		  	
			var href = $(this).attr( "href" );		    
			if(href=="#"){
				return false;	
			}

			$.bbq.pushState({url: href});

			if ($(document).width() <= $("#wrapinner").width()){
				$("#togglemenu").click();
			}			
			return false;
		});
			
		$(window).bind('hashchange', function(e){
			$("#imageloader").remove();
			var url = $.bbq.getState("url");
			if ($("#banner").length > 0 && blockQuery == 0){
				var addr = url;
				if (!addr) addr = '/site/index/';
				$.post("/site/getPageBanner", {'page':addr}, function(data){
					if (data){
						var image = "/images/" + data.name;
						var imageElement = $("#banner").children("img:first");
						imageElement.attr("src", image);
						if (data.link){
							imageElement.wrap('<a class="direct" href="'+data.link+'"></a>');
						}
					}
					else{
						$("#banner").children("img:first").attr("src", "/images/grey.gif");
					}
				}, "json");
			}

			var	photo = $.bbq.getState("photo");
			if (photo && blockQuery == 0){
				$.cookie('photo', photo);
			}
			if (blockQuery == 1){
				blockQuery = 0;
				return;
			}
			if(!url){
				if (window.location.pathname != "/") return;
				$("#content").append('<div id="imageloader" class="loading"></div>');
				$("img.lazy").lazyload({ 
					event : "navclick"
				});
				$("img.lazy:first").load(function(){
					$("#imageloader").remove();
					$('.flexslider').flexslider({
							directionNav: false,
							controlNav: false,
							keyboardNav: false,
							slideshowSpeed: 4000,
							after: function(slider){
									$(slider.slides[slider.currentSlide]).children().first().trigger("navclick");
							}
					});		
				}).trigger("navclick");
				return;
			}
			$("a").each(function(){
				var href = $(this).attr("href");

				if (href === url){
					$(this).addClass("current"); 
					$(this).parent("li").addClass("active");
				} 
				else{
					$(this).removeClass("current"); 
					$(this).parent("li").removeClass("active");
				}
			});
			$("#tools").hide();
			var request = jQuery.ajax({
				type:"get",
				url:url,
				success:setResponse,
				statusCode: {
					404: function(){
						$("#maincontent").html("Error 404! Requested page doesn't exist.");
					}
				}
			});			
		});
		
		$(window).trigger("hashchange");
});
