﻿menuSlide = function(selector,minLeft, maxLeft,hideContent){
	var th = this;
	this.init = function(){
		th.minLeft = minLeft;
		th.maxLeft = maxLeft;
		th.hideContent = hideContent;
		th.body = $(selector);
		th.container = $('#wrapper');
		th.offset = th.body.offset();
		th.size = {width:th.body.width(),height:th.body.height()};
		
		//th.makeHover();
		//th.makeUnHover();
		th.initArrow();
		
		th.initLinks();
		
	}
	this.openBlueMenu = function(){
		th.slideOut();
		$("#blueMenu .container .arrow").click(function(){
			th.slideIn();
			$("#blueMenu .container .arrow").unbind('click');
			th.body.oneTime(200,'timer4',function(){
				th.body.bind('click',th.openBlueMenu);
			});
		})
		th.body.unbind('click');
	}
	this.initArrow = function(){
		th.body.click(th.openBlueMenu);
	}
	this.slideIn = function(){
		if(!th.body.hasClass("fix")){
			th.body.removeClass('opened')
			       .addClass('closed')
			       .animate({left:th.minLeft},500);
		}
	}
	this.slideOut = function(){
		th.body.removeClass('closed')
			   .addClass('opened')
			   .animate({left:th.maxLeft},500);
	}
	this.fix = function(){
		th.body.addClass('fix');
	}
	this.unfix = function(){
		th.body.removeClass('fix');
	}
	this.initSubmenu = function(jqObj){
		jqObj.toggle(function(){
				$(this).next(".submenu").slideDown();
			},function(){
				$(this).next(".submenu").slideUp();
		});
	}
	this.initLinks = function(){
		$(".submenu").prev("a").each(function(){
			th.initSubmenu($(this));
		});
		$(".menu li a[href^=#],.submenu li a[href^=#]").not($(".menu li a[href=#],.submenu li a[href=#]")).click(function(){
			//th.fix();
			$(".menu li a.selected,.submenu li a.selected").removeClass("selected");
			$(this).addClass("selected");
		});
		/*$("#blueMenu .container .arrow").click(function(){
			th.unfix();
			th.slideIn();
		})*/
	}
	this.makeHover = function() {
		th.body.bind('mouseenter',function(){
			th.body.oneTime(200,'timer1',function(){
				th.slideOut();
			});
		});
	}
	this.makeUnHover = function(){
		th.body.bind('mouseleave',function(){
			th.body.stopTime('timer1');
			th.slideIn();
		})
	}
	this.init();
}
contentLoader = function(selector){
	var th = this;
	this.init = function(){
		th.body = $(selector);
		th.anchor = '';
		th.anchorAddress = '';
		th.anchorUrl = '';
		th.response = '';
		th.preloader =th.body.children(".preloader");
		th.preloaderImage = th.preloader.children(".preloaderImage");
		th.contentSpace = th.body.children(".container").children(".page").children(".content-container");
		th.closeButton = $("#CloseButton");
		th.loadedObjects={};
		th.indexPageBGImages=[];
		th.indexPageBGImagesLoaded=[];
		th.currentBGImage='';
		th.currentBGImageC=1;
		th.indexPage=true;
		
	    th.loadBGImage("images/products.jpg");
		th.loadBGImage("images/bg-1.jpg");
		th.loadBGImage("images/bg-2.jpg");
		th.loadBGImage("images/bg-3.jpg");


		/*
		th.body.oneTime(5000,'timer7',function(){
			th.changeImage(0);
		});*/
	}
	this.loadBGImage = function (image){
		th.indexPageBGImages[th.indexPageBGImages.length]=new Image();
		th.indexPageBGImages[th.indexPageBGImages.length-1].src=image;
		th.indexPageBGImages[th.indexPageBGImages.length-1].num=th.indexPageBGImages.length-1;
		th.indexPageBGImagesLoaded[th.indexPageBGImages.length-1]=false;
		/*
		th.body.oneTime(200,'timer1',function(){
				th.slideOut();
			});
		*/
		th.indexPageBGImages[th.indexPageBGImages.length-1].onload=function (){
			th.indexPageBGImagesLoaded[this.num]=true;
			if(this.num==0)th.changeImage(this.num);
		};
	}
	this.changeImage = function (n){
		if(!th.indexPage)return false;
		//if(image&&!th.currentBGImage){
		
			//th.currentBGImage=image;
		//}else{
		/*for(var i=0;i<th.indexPageBGImagesLoaded;i++){
		
		}*/
		//	$("#BGImage").animate({'left':$("#wrapper").width()},200);
		//}
		
		var wrapper_width=$('#wrapper').width();
		
		var cur=n;
		if(n>=th.indexPageBGImages.length)n=0;
		if(th.currentBGImageC==1){
			var p=wrapper_width-$("#BGImage").width();
			$("#BGImage2").height($('#wrapper').height()).attr("src",th.indexPageBGImages[n].src).css("display","block");
			$("#BGImageC2").css("display","block").css("left","-"+(wrapper_width-p)+"px")
			
			$("#BGImageC1").css("display","block").animate({left:wrapper_width-p},2000);
			$("#BGImageC2").css("display","block").animate({left:0},2000);
		}else{
			var p=wrapper_width-$("#BGImage2").width();
			$("#BGImage").height($('#wrapper').height()).attr("src",th.indexPageBGImages[n].src).css("display","block");
			$("#BGImageC1").css("display","block").css("left","-"+(wrapper_width-p)+"px").animate({left:0},2000);
			
			$("#BGImageC2").css("display","block").animate({left:wrapper_width-p},2000);
			$("#BGImageC1").css("display","block").animate({left:0},2000);
		}
		th.currentBGImageC=3-th.currentBGImageC;
		
		th.body.css("background","none");
		$("#content .page").css("background","none")
		
		th.body.oneTime(7000,'timer7',function(){  
			th.changeImage(n+1);
		});
	}
	this.showPreloader = function(){
		th.preloader.fadeIn(300,function(){
			th.preloaderImage.show();
		});		
	}
	this.hidePreloader = function(){
		th.preloaderImage.hide(50,function(){
			th.preloader.fadeOut();
		});		
	}
	this.getAnchorUrl = function(anchor){
		/**
		 * @todo add ajax capabilities for anchor url
		 */
		var indexof=anchor.indexOf("#");
		if(anchor.indexOf("alkotorg")==-1&&indexof!=-1&&anchor.length>indexof+1){
			th.anchor = anchor.substring(indexof+1);
			th.anchorUrl = "remote/"+th.anchor+".json";
		}else{
			th.anchor="";
			th.anchorUrl="";
		}
	}
	this.loadObject = function(ObjectName){
		if(th.loadedObjects[ObjectName]){
			th.loadedObjects[ObjectName].init();
		}
		$.ajax({
			type: "GET",
			url: 'js/'+ObjectName+".object.js",
			dataType: "script",
			success:function(jsObj){
				th.loadedObjects[ObjectName] = eval("new "+ObjectName+"()");
			},
			'cache':false
		});
	}
	this.parseResponse = function(){
		th.showFader();
		//th.body.css("background","#ffffff url("+th.response.image+") no-repeat top center")
		$("#BGImage").height($('#wrapper').height()).attr("src",th.response.image);
		$("#BGImageC2").css("display","none");
		$("#BGImageC1").css("left","0px").css("display","block");
		th.body.css("background","none");
		$("#content .page").css("background","none")
		th.contentSpace.html(th.response.content);
		th.closeButton.css("display","block");
		if(th.response.loadObject){
			th.loadObject(th.response.loadObject);
		}
		
		$("#content a").unbind("click").click(function(){
			th.getAnchorUrl($(this).attr("href"));
			if(th.anchorUrl){
				address = th.anchorUrl;
				th.loadAddress(address,"data");
			}
		});
	}
	this.parseError = function(err){
		alert("Документ в разработке");
	}	
	this.showFader = function(){
		/*th.body.children(".fader")
			.css("background","url(images/opacity4.png) repeat")
			.css("-background-image","none")
			.css("-filter:","progid:DXImageTransform.Microsoft.AlphaImageLoader(src=images/opacity4.png,sizingMethod=scale)");*/
		
			//.css("behavior","url(iepngfix/iepngfix.htc)");
		th.body.children(".fader").show();
	}
	this.hideFader = function(){
		//th.body.children(".fader").removeClass("active");//css("background","none");
		th.body.children(".fader").hide();
	}
	this.loadAddress = function(address,data){
		if(th.indexPage)
			th.fromIndexPage();
		
		$.ajax({
			url:address,
			data:data,
			async:false,
			dataType:'json',
			cache:false,
			type:'GET',
			beforeSend:function(){
				th.showPreloader();
			},
			complete:function(){
				th.hidePreloader();
			},
			error:function(err){
				th.parseError(err);
			},
			success:function(jsObj){
				th.response = jsObj;
				th.parseResponse();
			}
		});
	};
	this.hidePage = function (){
		/*if(th.anchor.indexOf("products-item")==0){
			th.toIndexPage();
			return false;
		}*/
		//th.body.css('background','#EAEAEA none');
		th.hideFader();
		//$("#content .page").css("background","url(images/contentBck.png) no-repeat center center")
		th.contentSpace.html("&nbsp;");
		th.hideCloseButton();
	}
	this.toIndexPage = function (){
		if(th.indexPage)return false;
		th.body.css('background','#EAEAEA none');
		th.hideFader();
		$("#content .page").css("background","url(images/contentBck.png) no-repeat center center")
		th.contentSpace.html("&nbsp;");
		th.hideCloseButton();
		
		th.indexPage=true;
		th.currentBGImageC=1;
		$("#BGImageC1").css("left","0px");
		$("#BGImageC2").css("left","0px");
		th.changeImage(0);
		
		$("#Warning").show();
	}
	this.fromIndexPage = function (){
		th.indexPage=false;
		$("#Warning").hide();
	}
	this.hideCloseButton = function(){
		th.closeButton.css("display","none");
	}
	this.init();
}
initLinks = function(ajaxLoader){
	var th = this;
	var address;
	$("a[href^=#]").not($("a[href=#]")).click(function(){
		ajaxLoader.getAnchorUrl($(this).attr("href"));
		address = ajaxLoader.anchorUrl;
		ajaxLoader.loadAddress(address,"data");
	});
}

var mainLoader;
$(document).ready(function(){
	var new_w=$("#wrapper").width()-630;
	if(new_w>470)new_w=470;
	$(document.body).append("<style type='text/css'>#content .container-today{width:"+new_w+"px;} \ndiv.period_switch{width:"+new_w+"px;} \n#ProductsCategories{width:"+new_w+"px;} \n#ProductDescription{width:"+(new_w-100)+"px;} \n.TextField{width:"+(new_w-115)+"px}</style>");

	var menuBlue = new menuSlide('#blueMenu',50,250,false);
	mainLoader = new contentLoader("#content","main");
	var allLinks=initLinks(mainLoader);
	
	if(location.hash!=""&&location.hash!="#"){
		mainLoader.getAnchorUrl(location.hash);
		mainLoader.loadAddress(mainLoader.anchorUrl);
	}
});