var mover=null, mover2=null, mphoto=null, malbum=null, mder=null, gH, gW, timer, timer2, timer3, timer4, so;

$(window).load(function(){
	if(jQuery.browser.opera)
		$(".bril").css("marginTop", "-100px");
	gH=($(window).height()>$("#div0").height()) ? $(window).height() : $("#div0").height();
	gW=$(window).width();
	$("#div0").css("height",gH+"px");
	$("area", "#tmMap").each(function(){
		$(this).mouseover(function(){
			$("#tmImg").attr("class", $(this).attr("id"));						   
		});							  
		$(this).mouseout(function(){
			$("#tmImg").attr("class", "");						   
			return false;
		});							  
	});
	$(".topMenu", ".topBlock:not(.noMenu)").animate({"top": "-140px", "right": "35px"}, "slow");
	$(".topMenu").mouseover(function(){
		$(this).stop();
		$(this).animate({"top": "0", "right": "0"}, "fast");
	});
	$(".topMenu").mouseout(function(){
		$(this).stop();
		$(this).animate({"top": "-140px", "right": "35px"}, "fast");
	});
	$(".playLine").mouseover(function(){
		$(this).stop();
		$(this).animate({"top": 0, "left": 0}, "fast");							  
	});
	$(".playLine").mouseout(function(){
		$(this).stop();
		$(this).animate({"top": "-71px", "left": "-137px"}, "fast");							  
	});
	
	if($("#mover",".scrollLine").length){
		if($("#mover", ".scrollLine").parent().prev().children(":eq(1)").height()>300){
			$("#mover",".scrollLine").parent().show();
			
			$(".scrollBox").children(":eq(1)").attr("mS",260-$(".scrollBox").children(":eq(1)").height());
			$(".scrollBox").children(":eq(1)").bind("mousewheel",function(event, delta) {
				y=parseInt($(this).css("top"));
				delta=delta/5;
				//alert($(this).attr("mS"));
				if(delta>0)
					if((y+delta)>0)
						$(this).css("top", 0);
					else
						$(this).css("top", (y+delta)+"px");
				else
					if((y+delta)<parseInt($(this).attr("mS")))
						$(this).css("top", $(this).attr("mS")+"px");
					else
						$(this).css("top", (y+delta)+"px");
				y= $(this).attr("mS")!=0 ? parseInt($(this).css("top"))/$(this).attr("mS") : 0;
				y2= y*260;
				$("#mover").css("top", y2+"px");
				event.stopPropagation();
				event.preventDefault();
				return false;
			});
		}
	}	
	$("#mover",".scrollLine").mousedown(function(e){
		mover=$(this);
		var y=e.pageY;
		mover.attr("cY", y-$(this).offset().top);
		mover.attr("pY", $(this).parent().offset().top);
		mover.attr("cH", 260-$(this).parent().prev().children(":eq(1)").height());
		return false;
	});
	if($("#mover",".scrollLine2").length){
		if(($("#mover", ".scrollLine2").parent().prev().width()-$("#mover", ".scrollLine2").parent().prev().children(":eq(2)").width())<0)
			$("#mover",".scrollLine2").parent().show();
	}	
	$("#mover",".scrollLine2").mousedown(function(e){
		mover2=$(this);
		var x=e.pageX;
		mover2.attr("cX", x-$(this).offset().left);
		mover2.attr("pX", $(this).parent().offset().left);
		mover2.attr("cW", $(this).parent().prev().width()-$(this).parent().prev().children(":eq(2)").width()-300);
		return false;
	});
	$(".playLine a").click(function(){
		$(this).toggleClass("pause");
		return false;
	});
	
	$("#pointL").click(function(){ return false; });
	$("#pointL").mouseover(function(e){
		clearTimeout(timer);
		if($("div:not(.active)", "#listL .contBox").length)
			$("#listL").show();
	});
	$("#pointL").mouseout(function(){
		timer2=setTimeout('$("#listL").hide()',300);
	});
	$("#pointL").mousemove(function(e){
		var x=e.pageX
		var y=e.pageY
		x=$(this).offset().left-x;
		y=$(this).offset().top-y;
		$("#listL").css({"right":25+x+"px", "bottom": 80+y+"px"});
	});
	$("#listL").mouseover(function(){
		clearTimeout(timer);
		clearTimeout(timer2);
		$(this).show();
	});
	$("#listL").mouseout(function(){
		timer=setTimeout('$("#listL").hide()',300);
	});	
	
	$("#pointR").click(function(){ return false; });
	$("#pointR").mouseover(function(e){
		clearTimeout(timer3);
		if($("div:not(.active)", "#listR .contBox").length)
			$("#listR").show();
	});
	$("#pointR").mouseout(function(){
		timer4=setTimeout('$("#listR").hide()',300);
	});
	$("#pointR").mousemove(function(e){
		var x=e.pageX
		var y=e.pageY
		x=$(this).offset().left-x;
		y=$(this).offset().top-y;
		$("#listR").css({"left":-15-x+"px", "bottom": 80+y+"px"});
	});
	$("#listR").mouseover(function(){
		clearTimeout(timer3);
		clearTimeout(timer4);
		$(this).show();
	});
	$("#listR").mouseout(function(){
		timer3=setTimeout('$("#listR").hide()',300);
	});	
	
	$(".imgBox", ".photosMoveField").each(function(i){
		if(i<3){
			i2=i;
			j=0;
		}else{
			i2=i/2;
			j=1;
		}
		x=200+i2*200+Math.random()*50;
		y=j*200+Math.random()*30;
		$(this).css({"left": x+"px", "top": y+"px"});										   
	});
	
	$(".imgBox", ".photosMoveField").mousedown(function(e){
		x=e.pageX;
		y=e.pageY;
		px=$(this).offset().left;
		py=$(this).offset().top;
		$(".imgBox", ".photosMoveField").css("zIndex", 1);
		$(this).css("zIndex", 5);
		$(this).attr("moving", "");
		$(this).attr("x", x-px);
		$(this).attr("y", y-py);
		$(this).attr("x2", $(this).parent().offset().left);
		$(this).attr("y2", $(this).parent().offset().top);
		mphoto=$(this);
		return false;
	});
	$(".imgBox", ".photosMoveField").click(function(){
		if($(this).attr("moving"))
			return false;
	});
	
	$(".album", ".albumsMoveField").each(function(i){
		x=200+i*200+Math.random()*50;
		y=Math.random()*30;
		$(this).css({"left": x+"px", "top": y+"px"});										   
	});
	
	$(".album", ".albumsMoveField").mousedown(function(e){
		x=e.pageX;
		y=e.pageY;
		px=$(this).offset().left;
		py=$(this).offset().top;
		$(".album", ".albumsMoveField").css("zIndex", 1);
		$(this).css("zIndex", 5);
		$(this).attr("moving", "");
		$(this).attr("x", x-px);
		$(this).attr("y", y-py);
		$(this).attr("x2", $(this).parent().offset().left);
		$(this).attr("y2", $(this).parent().offset().top);
		malbum=$(this);
		return false;
	});
	$(".album", ".albumsMoveField").click(function(){
		if($(this).attr("moving"))
			return false;
	});
	
	$("#main").mousemove(function(e){
		if(mover){
			var y=e.pageY;
			var mY=y-mover.attr("pY")-mover.attr("cY");
			if(mY<0) mY=0;
			if(mY>225) mY=225;
			mover.parent().prev().children(":eq(1)").css("top", mY/225*mover.attr("cH")+"px");
			mover.css("top",mY+"px");
		}
		if(mover2){
			var x=e.pageX;
			var mX=x-mover2.attr("pX")-mover2.attr("cX");
			if(mX<0) mX=0;
			if(mX>410) mX=410;
			mover2.parent().prev().children(":eq(2)").css("left",150+mX/410*mover2.attr("cW")+"px");
			mover2.css("left",mX+"px");
		}
		if(mphoto){
			mphoto.attr("moving", 1);
			x=e.pageX;
			y=e.pageY;
			x=x-mphoto.attr("x")-mphoto.attr("x2");
			y=y-mphoto.attr("y")-mphoto.attr("y2");
			if(x<0) x=0;
			if(x>800) x=800;
			if(y<0) y=0;
			if(y>250) y=250;
			mphoto.css({"left":x+"px", "top": y+"px"});
		}
		if(malbum){
			malbum.attr("moving", 1);
			x=e.pageX;
			y=e.pageY;
			x=x-malbum.attr("x")-malbum.attr("x2");
			y=y-malbum.attr("y")-malbum.attr("y2");
			if(x<0) x=0;
			if(x>800) x=800;
			if(y<-30) y=-30;
			if(y>130) y=130;
			malbum.css({"left":x+"px", "top": y+"px"});
		}
		if(mder){
			x=e.pageX;
			y=e.pageY;
			x=x-mder.attr("x")-mder.attr("x2");
			y=y-mder.attr("y")-mder.attr("y2");
			if(x<0) x=0;
			if(x>850) x=850;
			if(y<-50) y=-50;
			if(y>350) y=350;
			mder.css({"left":x+"px", "top": y+"px"});
		}
		return false;
	});
	$("#main").mouseup(function(){
		mover=null;
		mover2=null;
		mphoto=null;
		malbum=null;
		if(mder){
			if(parseInt(mder.css("left"))>420) 
				mder.animate({"left": "740px", "top": "120px"}, "fast");
			else
				mder.animate({"left": "143px", "top": "150px"}, "fast");
			mder=null;
		}
		return false;
	});
	$(window).mouseup(function(){
		mover=null;
		mover2=null;
		mphoto=null;
		malbum=null;
		if(mder){
			if(parseInt(mder.css("left"))>420) 
				mder.animate({"left": "740px", "top": "120px"}, "fast");
			else
				mder.animate({"left": "143px", "top": "150px"}, "fast");
			mder=null;
		}
		return false;
	});
	
	$("li", ".songsList").mouseover(function(){
		$(this).addClass("hover");
	});
	$("li", ".songsList").mouseout(function(){
		$(this).removeClass("hover");
	});
		
	$("#feedback").each(function(){
		this.reset();						 
	});
	$("li input", "#feedback").each(function(){
		$(this).attr("text", $(this).val());
		$(this).focus(function(){
			if($(this).attr("text")==$(this).val())
				$(this).val("");
		});
		$(this).blur(function(){
			if(!$(this).val())
				$(this).val($(this).attr("text"));
		});
	});
	$("textarea", "#feedback").attr("text", $("textarea", "#feedback").val());
	$("textarea", "#feedback").focus(function(){
		if($(this).attr("text")==$(this).val())
			$(this).val("");
	});
	$("textarea", "#feedback").blur(function(){
		if(!$(this).val())
			$(this).val($(this).attr("text"));
	});
	
	
	$("a",".photosList").click(function(){
		$("li.active","#bigPhotos").removeClass("active");
		var i=$("a",".photosList").index(this);
		$("li:eq("+i+")","#bigPhotos").addClass("active");
		$(".winBg").fadeIn("fast");
		$(".winBox").fadeIn("normal");
		return false;								
	});
	$(".winBg").click(function(){
		$(this).fadeOut("normal");
		$(".winBox").fadeOut("fast");
	});
	$(".winBox").click(function(){
		$(this).fadeOut("fast");
		$(".winBg").fadeOut("normal");
	});
	
	$("#ponts").click(function(){
		$(this).toggleClass("active");
		if($(this).hasClass("active")){
			$("#hand").addClass("active");
			$("#money").hide();
			$("#trubiL").stop();
			$("#trubiL").animate({"left": "150px", "top": "50px"}, "normal", function(){ $(this).hide(); });
			$("#trubiR").stop();
			$("#trubiR").animate({"right": "125px", "top": "-5px"}, "normal", function(){ $(this).hide(); });
			$("#lavri").stop();
			$("#lavri").hide();
			$("#dershava").show();
			$("#dershava").addClass("active");
			$(".bril").each(function(){
				$(this).stop();
				$(this).animate({"top": (gH+50)+"px"}, 300);
			});
			
			$("#dershava").animate({"left": "740px", "top": "120px"}, "normal", function(){
				$(this).bind("mousedown",function(e){
					x=e.pageX;
					y=e.pageY;
					px=$(this).offset().left;
					py=$(this).offset().top;
					$(this).attr("x", x-px);
					$(this).attr("y", y-py);
					$(this).attr("x2", $(this).parent().offset().left);
					$(this).attr("y2", $(this).parent().offset().top);
					mder=$(this);
					return false;
				});																	   
			});
		}else{
			$("#hand").removeClass("active");
			$("#dershava").removeClass("active");
			$("#dershava").animate({"left": "143px", "top": "150px"}, "normal", function(){
				$(this).unbind("mousedown");																	   
			});
			$("#trubiL").show();
			$("#trubiL").animate({"left": "-=100px", "top": "-=75px"}, "normal");
			$("#trubiR").show();
			$("#trubiR").animate({"right": "-=100px", "top": "-=75px"}, "normal", function(){ trubi(); });
			$("#money").show();
			$("#lavri").show();
			lavri();
			$(".bril").each(function(){
				$(this).css({"top": "-50px","left":Math.random()*gW+"px"});
				fly($(this), 400+Math.random()*2500);
			});
		}
		return false;
	});
	
	lavri(); 
	trubi();
	
	$(".bril").each(function(){
		$(this).css({"top": "-50px","left":Math.random()*gW+"px"});
		fly($(this), 400+Math.random()*2500);
	});
	
	if($("#player").length){
		var version = deconcept.SWFObjectUtil.getPlayerVersion();
		//so = [];
		if(version["major"]>=9){
			so = new SWFObject(root+"flash/player.swf?rnd="+Math.floor((Math.random(10000000)*10000000)), "fl_movie_1", "1", "1", "9.0.0", "#000000", true);
			so.addParam("allowScriptAccess", "sameDomain");
			so.addParam("menu", "false");
			so.write("player");
		}
	}
	if($("#playerBox").length){
		var version = deconcept.SWFObjectUtil.getPlayerVersion();
		//so = [];
		if(version["major"]>=9){
			so = new SWFObject(root+"flash/video.swf?rnd="+Math.floor((Math.random(10000000)*10000000)), "fl_movie_1", "394", "256", "9.0.0", "#000000", true);
			so.addParam("allowScriptAccess", "sameDomain");
			so.addParam("menu", "false");
			so.addParam("wmode", "transparent");
			so.write("playerBox");
		}
	}
	
	$(".play").click(function(){
		if(!$(this).hasClass("active")){
			$("li.hover2", ".songsList").removeClass("hover2");
			$(this).parents("li").addClass("hover2");
			$(".play.active").removeClass("active");
			$(".play.active").removeClass("active");
			$(this).addClass("active");
			setSong($(this).attr("href"));
		}else{
			$(this).parents("li").removeClass("hover2");
			$(this).removeClass("active");
			resume();
		}
		return false;
	});
	$(".title").click(function(){
		if(!$(this).prev().hasClass("active")){
			$("li.hover2", ".songsList").removeClass("hover2");
			$(this).parents("li").addClass("hover2");
			$(".play.active").removeClass("active");
			$(".play.active").removeClass("active");
			$(this).prev().addClass("active");
			setSong($(this).prev().attr("href"));
		}else{
			$(this).parents("li").removeClass("hover2");
			$(this).prev().removeClass("active");
			resume();
		}
		return false;
	});
	
	
	$("a", ".videosList").live("click",function(){
		if($("div.active", ".videosList").length){
			obj=$("div.active", ".videosList");
			obj.removeClass("active");
			$("#listL .contBox").append(obj);
		}
		$(this).parent().addClass("active");
		setVideo($(this).attr("href"));
		$("h2", ".arrowsBox").text($(this).text());
		if(!$(".contBox div:not(.active)", ".pointBoxR").length){
			$(".videosList",".pointBoxR").hide();
			$(".pointBoxR").hide();
		}
		if($(".contBox div:not(.active)", ".pointBoxL").length){
			$(".videosList",".pointBoxL").show();
			$(".pointBoxL").show();
			$(".videosList",".pointBoxL").fadeOut("normal");
		}
		return false;								 
	});
	$("a:first", ".videosList").click();
	
	
});

$(window).resize(function(){
	gH=($(window).height()>$("#div0").height()) ? $(window).height() : $("#div0").height();
	gW=$(window).width();
	$("#div0").css("height",gH+"px");
});


function setSong(url){
	so.setVar("START_SONG_FROM_JS", url);
}
function resume(){
	so.setVar("RESUME_FROM_JS", '1');
}
function nextSong(){
	// tut nado vizivat setSong s novim url i menyat nazvanie pesni
}
function setVideo(url){
	so.setVar("VIDEO_URL_FROM_JS", url);
}


function fly(obj, speed){
	obj.animate({"top": (gH+50)+"px"}, speed, function(){
		$(this).css({"top": "-50px","left":Math.random()*gW+"px"});
		fly($(this), 400+Math.random()*2500)
	});
}
function lavri(){
	$("#lavri").animate({"top": (-80-Math.random()*5)+"px", "left": (355-Math.random()*5)+"px"}, 200, function(){
		lavri();
	});
}
function trubi(){
	var rand=Math.random()*300;
	$("#trubiL").animate({"top":"-=20px", "left":"-=25px"}, 100+rand, function(){
		$("#trubiL").animate({"top":"+=20px", "left":"+=25px"}, 400+rand);
	});
	$("#trubiR").animate({"top":"-=20px", "right":"-=25px"}, 100+rand, function(){
		$("#trubiR").animate({"top":"+=20px", "right":"+=25px"}, 400+rand);
	});
	/*
		function(){
			trubi();																	   
		});
	*/
}