var animatedBanner = {

	init : function(){
		var that = this;
		this.banner = $('.inline_banner');

		this.attachEvents();

	},

	attachEvents : function(){
		var that = this;
        this.banner.click(
			function(e){
				that.click(e);
			}
		);
	},

	click : function(e){
		ban = $(e.target.parentNode);
		banParent = ban.parent();
		if(ban.hasClass('animated')){
            ban.animate({
					width: this.beginWidth
				}, 1000, function(){
					ban.removeClass('animated');
                    ban[0].removeAttribute("style");
					$('.animated',banParent).prependTo(banParent);
				});
		}
		else{
        	if (ban.hasClass('animate')){
        		this.beginWidth = ban.width();
				if (!(ban.prev().hasClass('animated'))){
					ban.prependTo(banParent);
				}
	            ban.addClass('animated');
				ban.animate({
					width: "100%",
					marginLeft: 0,
					marginRight: 0
				}, 1000);
			}
		}
	}
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}



var showSwitcher = {

	init : function(){
		var that = this;
		this.happylogo = $('#happylogo_main');
		this.logo = $('#logo');
		this.switcher = $('#switcher a');
		this.switcherCont = $('#switcher');
		this.html = $('html');
		this.body = $('body');

		if (this.html.hasClass('classic') ){
			this.switcherCont.addClass('turnOff');
			$(this.switcher[1]).addClass('active');
		}
		else{
			$(this.switcher[0]).addClass('active');
		}

		this.change = 0;

		this.attachEvents();
	},

	attachEvents : function(){
		var that = this;
		this.switcher.click(
			function(){
				if(that.html.hasClass("show")){
					that.clickShow();
				}
				else{
					that.clickNotShow();
				}
				return false;
			}
		);
	},

	clickShow : function(e){
		var that = this;
		if(this.change == 0){
			this.change = 1;
			this.switcherCont.addClass("turnOff");
			this.switcher.toggleClass("active");
	        this.happylogo.animate({
				top: "-160px"
			}, 1000);
	        this.logo.animate({
				top: "0px"
			}, 1000, function(){
	            that.html.removeClass("show").addClass("classic");
				that.body.removeClass("show").addClass("classic");
				that.change = 0;
				if(that.body.attr("id") == "home_page"){
					$(".chel").hide();
				}
			});
			window.location.hash = "classic";
			Set_Cookie('turn_off', 1, '', '/', '', '' );
		}
	},

    clickNotShow : function(e){
    	var that = this;
		if(this.change == 0){
			this.change = 1;
			this.switcherCont.removeClass("turnOff");
			this.switcher.toggleClass("active");
	        this.logo.animate({
				top: "-123px"
			}, 1000);
			this.happylogo.show();
	        this.happylogo.animate({
				top: "0px"
			}, 1000, function(){
	            that.html.removeClass("classic").addClass("show");
				that.body.removeClass("classic").addClass("show");
				that.change = 0;
                if(that.body.attr("id") == "home_page"){
					$(".chel").show();
				}
			});
			window.location.hash = "show";
			Set_Cookie('turn_off', 0, '', '/', '', '' );
		}
	}
}

/*var freakChanger = {
	init : function(){
		var that = this;

		this.cheloveki300 = cheloveki300;

        this.cheloveki500 = cheloveki500;

		this.chels = $('.chel');

        this.chels.each(
			function(){
				new oneChel($(this), that)
			}
		);

	}

}*/

oneChel = function(jPtr, oChanger){
	this.jPtr = jPtr;
	this.oChanger = oChanger;
	this.init();
}

oneChel.prototype = {

	init : function(){
		this.nextPic = new Image();
		this.changeLink = $("a", this.jPtr);

		this.attachEvents();
	},

    attachEvents : function(){
		var that = this;
        $(this.nextPic).load(
			function(){
				that.newPic();
			}
		);
		this.changeLink.click(
			function(e){
				that.change(e)
				return false;
			}
		);
	},

	change : function(e){
		var that = this;
		this.currPic = $("img", this.jPtr);
		if (this.currPic.height() == 300){
			this.cheloveki = this.oChanger.cheloveki300;
		}
		else{
			this.cheloveki = this.oChanger.cheloveki500;
		}
		this.currPic.animate({
			top: this.currPic.height()
		}, 1000, function(){
            var re = /\w+.jpg$/;
			var arr = that.currPic.attr("src");
			if( arr == '/f/1/global/0.gif' ){
				var tmp = that.currPic.css("background-image");
				if( $.browser.msie || $.browser.opera){
					arr = tmp.substring(tmp.indexOf("/r"),tmp.length-2);
				}
				else{
					arr = tmp.substring(tmp.indexOf("/r"),tmp.length-1);
				}
				that.currPic.css("background-image","");
			}
			for (var i=0; i < that.cheloveki.length; i++){
				if( that.cheloveki[i] == arr ){
					if ( (i+1) == that.cheloveki.length ){
						that.nextPicSrc = that.cheloveki[0];
					}
					else{
						that.nextPicSrc = that.cheloveki[i+1];
					}
				}
			}
			that.nextPic.src = that.nextPicSrc;
		});
	},

	newPic : function(){
		this.currPic.attr("src", this.nextPicSrc);
		this.currPic.animate({
			top: "0"
		},1000);
	}
}

var directorGame = {
	init : function(){
		var that = this;
		this.directors = $(".director");
		this.mapLinks = $(".all_shops_onmap");
		this.aDirectors = [];
		this.begOffset = parseInt(this.directors.css("left"));

        this.directors.each(
			function(id){
				that.aDirectors.push(
					new Director($(this), id, that)
				);
			}
		);

		if ($("body").attr("id") == 'shops_page'){
			var time = setTimeout(function(){
				that.aDirectors[0].rocketDirector();
			}, 1000);
		}

		this.mapLinks.click(
			function(){
				var mapContainer = $(this).parent().next();
				$('.map',mapContainer).toggleClass("hidden");
				return false;
			}
		)

	}
}

Director = function(jPtr, id, oGame){
	this.jPtr = jPtr;
	this.id = id;
	this.oGame = oGame;
	this.init();
}

Director.prototype = {

	init : function(){
		this.initBaseProperties();
		this.attachEvents();
	},

	initBaseProperties : function(){
		this.timer = null;
		this.timer_click = null;
		this.hide = 0;
		this.flag = 0;
		this.div = this.jPtr;
		this.currImg = $("img",this.jPtr);
		this.containerW = $("#shopsContainer")[0].offsetWidth;
	},

    attachEvents : function(){
		var that = this;
        this.div.mouseover(
			function(e){
				that.startDirector(e);
			}
		);
		this.div.mouseout(
			function(e){
				if (that.hide == 0 && that.flag == 0 ){
					that.flag = 1;
                    that.currImg.animate({
						left: "0px"
					}, 80);
					that.flag = 0;
				}
				//that.endDirector(e);
			}
		);
		this.div.click(
			function(e){
				if( that.hide == 0 ){
					that.rocketDirector(e);
				}
			}
		);

	},

    startDirector : function(e){
    	var that = this;

		if (this.hide == 0 && this.flag == 0){
			this.flag = 1;
			/*clearTimeout(this.timer);
			clearTimeout(this.timer_click);
            this.timer_click = setTimeout(function(){
                that.currImg.dequeue();
	        	that.currImg.animate({
					left: "200px"
				}, 150);
			}, 100);*/
            that.currImg.animate({
					left: "-15px"
			}, 80);
			this.flag = 0;
		}
	},

    endDirector : function(e){
	   	var that = this;

		if (this.hide == 0 && this.flag == 0 ){
			clearTimeout(this.timer_click);
	        this.currImg.dequeue();
			this.timer = setTimeout(function(){
	           	that.currImg.animate({
					left: "0px"
				}, 150);
			}, randomNumber(100,1000));
		}
	},

	rocketDirector : function(e){
		var that = this;
		var width = this.containerW+250;

		//var eX = e.clientX;
		var picX = this.currImg.offset().left;

		//if (eX >= picX){
			this.hide = 1;
            this.currImg.animate({
				left: "-"+width+"px"
			},
			1000,
			function(){
				that.hide = 0;
				that.currImg.css("left","200px");
				that.endDirector();
			});
		//}
	}
}

var whirl = {

	init : function(){
		this.whirl = $("#whirl");
		this.happylogo = $("#happylogo_main");
		this.whirlImg = $("img",this.whirl);
		this.rotate = 1;
		this.attachEvents();
		this.timer = null;
	},

	attachEvents : function(){
		var that = this;

		this.happylogo.mouseover(
			function(){
				that.rotate = 1;
				that.animateWhirl();
			}
		);

        this.happylogo.mouseout(
			function(){
				that.rotate = 0;
				clearTimeout(that.timer);
			}
		);

		this.whirl.click(
			function(){
				window.location = '/';
			}
		);
	},

	animateWhirl : function(){
		if (this.rotate == 1){
			var that = this;
			var topValue =  parseInt(this.whirlImg.css("top"));
			var newValue;
			if (topValue == -88){
				newValue = 0;
			}
			else{
				newValue = topValue-44;
			}
			this.whirlImg[0].style.top = newValue+"px";

		   	this.timer = setTimeout(
				function(){
					that.animateWhirl();
				}, 75);
		}
	}

}

var bigbaba = {

	init : function(){
		this.bigbaba = $("#bigbigbaba");
		this.bigbabaLink = $("a",this.bigbaba);
		this.bigbabaCont = $(".container",this.bigbaba);
		this.showBabains = $(".title_s",this.bigbaba);
		this.hideBabains = $(".title_h",this.bigbaba);
		this.loader = bigbaba_loader;
		this.bigbaba_src = bigbaba_pic;
		this.warn = bigbaba_warning;
		this.bigbabaImg = new Image();
		this.showed = 0;

		this.attachEvents();
	},

    attachEvents : function(){
		var that = this;
        $(this.bigbabaImg).load(
			function(){
				that.showBaba();
			}
		);
		this.bigbabaLink.click(
			function(e){
				that.loadBaba(e);
				return false;
			}
		);
	},

	loadBaba : function(){
		var that = this;
		if (this.showed == 0){
            this.bigbabaCont.slideUp(1000,
				function(){
	                that.bigbabaCont.html("<img src='"+that.loader+"' />");
					that.bigbabaCont.slideDown(100,
						function(){
							that.bigbabaImg.src = that.bigbaba_src;
						}
					);
				}
			);
		}
		else{
            this.bigbabaCont.slideUp(1000,
				function(){
	                that.bigbabaCont.html("<img src='"+that.loader+"' />");
                    that.bigbabaCont.slideDown(100,
						function(){
							that.bigbabaImg.src = that.warn;
						}
					);
				}
			);
		}
	},

	showBaba : function(){
		var that = this;
		this.bigbabaCont.slideUp(500,
			function(){
                that.bigbabaCont.html("<img src='"+that.bigbabaImg.src+"' />");
				if (that.showed == 0){
                    that.hideBabains.removeClass("hidden");
					that.showBabains.addClass("hidden");
					that.showed = 1;
				}
				else{
                    that.showBabains.removeClass("hidden");
					that.hideBabains.addClass("hidden");
					that.showed = 0;
				}
				that.bigbabaCont.slideDown(500);
			}
		);
	}

}

var pdf = {

	init : function(){
		this.pdf_selector = $("#city_selector");
		this.pdf_link = $("#pdf_content a");
		this.pdf_size = $("#pdfsize");
		this.pdf_img = $("#pdfsize");
		this.attachEvents();
	},

    attachEvents : function(){
		var that = this;
        $(this.pdf_selector).change(
			function(){
				var num = $("option:selected",that.pdf_selector)[0].index+1;
				$("#allpdf img").removeClass("active");
				$("#allpdf .pdf"+num).addClass("active");
				that.pdf_link.attr("href",$("option:selected",that.pdf_selector).attr("value"));
				that.pdf_size.text($("option:selected",that.pdf_selector).attr("title"));
			}
		);
	}
}

function randomNumber (m,n)
{
	m = parseInt(m);
	n = parseInt(n);
	return Math.floor( Math.random() * (n - m + 1) ) + m;
}




$(document).ready(
  function(){

  	try{
  	initialize();
  	}
  	catch(e){
  	};
	var bodyId = $("body").attr("id");
  	animatedBanner.init();
	showSwitcher.init();
	if (bodyId == "home_page"){
		var domains = ['http://www.mediamarkt.ru'];
		if($("html").hasClass('classic')){
			multiLoader.start( $.makeArray($(".product")) , domains);
		}
		else if($("html").hasClass('show')){
			multiLoader.start( $.makeArray($(".product")).concat($.makeArray($(".chel"))) , domains);
		}
		//freakChanger.init();
		bigbaba.init();
	}
	directorGame.init();
	if (bodyId != "home_page"){
		whirl.init();
	}
	pdf.init();
});
