FADE_OUT = 1000;
FADE_IN = 1000;
LOOP_SPEED = 6000;
LOOP = null;
TIMER = null;
ISIPAD = navigator.userAgent.match(/iPad/i) != null;

/* mp3 listener */
var mp3Player = new Object();

mp3Player.onInit = function(){
	if ( $("#mp3Object").length == 0 ) return false;
	$("#mp3Object")[0].SetVariable("method:setVolume", 25);
	$("#mp3Object")[0].SetVariable("enabled", true);
};

mp3Player.onUpdate = function(){
	if ( this.isPlaying == "false" ){
		if ( !$("#playPause").hasClass("off") ){
			mp3Player.play();
		}
	}
}

mp3Player.play = function(url){
		
		 player = $("#mp3Object")[0];

		 if ( ISIPAD ) return false;
		 
		 if ( url )
		 	player.SetVariable("method:setUrl", url);
		 
		 if ( url && $("#mp3Object").hasClass("paused") ) return true;

		 player.SetVariable("method:play", "");
		 $("#mp3Object").removeClass("paused");

}

mp3Player.pause =function(){
	if ( $("#mp3Object").length == 0 ) return false;
	$("#mp3Object")[0].SetVariable("method:pause", "");
	$("#mp3Object").addClass("paused");
}

/* --- */

$(function(){

	

	/*$(window).resize(function(){
			$(document)[0].location.reload();
	})*/

	$(".clickable").click(function(){
		$("#home").click();
	})

	
	Gallery.load(1,function(){
		Menu.show()
		Player.play(true);
		Menu.initLinks();
		setTimeout('mp3Player.play(Gallery.audio)',2000);
	});

	Slider.init();
	Menu.Dropdown.init();
	Menu.Dropdown.init2();



	$("#home").click(function(e){
		Gallery.load(1,function(){
			Menu.show();
			Player.play();
			Menu.initLinks();
			mp3Player.play(Gallery.audio);
		});
		e.preventDefault();
		Slider.hide();

	});
	

	$("img").live("contextmenu",function(e){
		e.preventDefault();
		return false;
	})

	$(".gallery .wrapper img").live('click',function(){
		Player.stop();
		Page.remove();
		Gallery.load($(this).attr("rel"),"new")
		Slider.load();
		
	})

	// setup slider img click
	$("#slider-container ul li a").live('click', function(){
		var theLink = $(this)
		var imgPath = theLink.addClass("active").children('img').attr('src').replace("thumbs/thumbs_","");
		var imgIndex = $.inArray(imgPath,Gallery.images);
		theLink.siblings("a.active").removeClass("active");
		Player.stop();
		Player.lastIndex = imgIndex - 1;
		Player.next();
	})

	// AJAX setup
	$("#loader")
		.ajaxStart(
			function(){
				$(this).show()
		})
		.ajaxStop(
			function(){
			$(this).hide()
		})


	// setup arrows
	$(document).mousemove(function(e){
		// if slider is visible, we are in a gallery
		if ( !$("#slider-container").hasClass("show") ) return false;
		
		// left arrow
		if ( is_region( e,0,41,$(window).width()/2,$(window).height()-40) && 1 /*($("#slider-container a.active").prev().length == 1)*/ )
			$("#left-arrow").css("display","block");
		else
			$("#left-arrow").css("display","none");

		// right arrow
		if (is_region(e,$(window).width()/2,41,$(window).width(),$(window).height()-40) )
			$("#right-arrow").css("display","block");
		else
			$("#right-arrow").css("display","none");
	})

	$("#left-arrow").click(function(){
		 //prev = $("#slider-container a.active").removeClass("active").prev().addClass("active").attr("href");
		 Player.pause()
		 Player.prev()
	})

	$("#right-arrow").click(function(){
		 //prev = $("#slider-container a.active").removeClass("active").next().addClass("active").attr("href");
		 Player.pause()
		 Player.next()
	})

	$("#btnContact").live('click', function(){

		var dataString = $("#contact").serialize();

		if ( $("#fullname").val() == "" ){
			$("#fullname").addClass("invalid");
			return false;
		}else
			$("#fullname").removeClass("invalid");

		// email
		if ( $("#emailaddr").val() == "" ){
			$("#emailaddr").addClass("invalid");
			return false;
		}else
			$("#emailaddr").removeClass("invalid");

		// uzenet
		if ( $("#messagetext").val() == "") {
			$("#messagetext").addClass("invalid");
			return false;
		}
			$("#messagetext").removeClass("invalid");


		$.ajax({
			url: MAIN_URL + "/ajax.php",
			data: dataString + "&cmd=send_message",
			type: "POST",
			success: function(){
				$("form").html("<p>Az üzenetedet megkaptadm! Hamarosan felveszem Veled a kapcsolatot.</p>");
			}
		})

		

	})




	$("#playPause").toggle(function(){
		mp3Player.pause();
		$(this).addClass("off");
	},function(){
		mp3Player.play();
		$(this).removeClass("off");
	})
	


})


var Slider = {
	
	obj: null,

	init: function(){
		
		Slider.obj = $("#slider-container").css({
			top: $(window).height() + "px"
		})

		Slider.obj
		.hover(
			function(){
				clearTimeout( TIMER );
				if ( Slider.obj.hasClass("down") )
					Slider.show();
			},
			function (){
				Slider.timed_down();
			}
			)

	},

	show: function(){
		Slider.obj
			.animate({
				top: ($(window).height()-104) + "px"
			},500)
			.addClass("show")
			.find("a:first")
				.addClass("active")
	},

	next: function(){
		var current = Slider.obj.find("a.active").removeClass("active")
		var next = current.next("a")
		if ( next.length == 0 ){

			if ( current.parent("li").next("li"))
			current.parent("li").children("a:first").addClass("active")
		}
		else
			next.addClass("active")
	},

	prev: function(){
		Slider.obj
			.find("a.active")
			.removeClass("active")
			.prev("a")
			.addClass("active")
	},


	hide: function(){
		Slider.obj
			.animate({
				top: $(window).height() + "px"
			},500,function(){
				$(this).hide();
			})
			.removeClass("show")
			
	},

	down: function(){
		Slider.obj
			.animate({
				top: $(window).height() - 24 + "px"
			},500)
			.addClass("down")
	},

	load: function(){
		$.ajax({
			url: BASE_URL + "/?page_id=5",
			data: "&cmd=get_small_thumbs&gallery_id=" + Gallery.gid,
			type: "GET",
			success: function(d,t){
				Slider.obj.show().children('.slider')
				.html(d)
				.easySlider({
					continuous: false,
					speed: 1000,
				});
				Slider.show();
			}
		})
	},

	timed_down: function(){
		TIMER = setTimeout("Slider.down()", 2000);
	}
}



var Menu = {

	Dropdown: {
		init: function(){
			
			$("#galleries").mouseenter(function(){
				
				var pos = $(this).position();
				
				var half = pos.left + ($(this).width() / 2);

				$("#galleries-menu").css({
						"left": half - ( $(this).width() / 2 ) + 10 + "px",
						"top": pos.top + 32 + "px",
				}).fadeIn(500);

			})
			.mouseleave(function(){
				$("#galleries-menu").delay(1000).fadeOut(500);
			})
			.click(function(e){
				Page.create(BASE_URL + "/?page_id=5&cmd=get_album_list", true);
				e.preventDefault();
			})
		
			$("#galleries-menu").mouseenter(function(){
				$("#galleries-menu").clearQueue();
			})
			.mouseleave(function(){
				$(this).delay(500).fadeOut(500);
			})

			$("#galleries-menu a").click(function(e){
				
				if ( $(this).attr("count") > 1 )
					Page.create( BASE_URL + "/?page_id=5&cmd=get_album_thumbs&album_id=" + $(this).attr("href"), true)
				else{
					
						Player.stop();
						Page.remove();
						Gallery.load($(this).attr("rel"),"new")
						Slider.load();
				}
					
				e.preventDefault();
				return false;
			})
		},	

		init2: function(){
			
			$("#video").mouseenter(function(){
				var pos = $(this).position();
				var half = pos.left + ($(this).width() / 2);
			
				$("#video-menu").css({
						"left": half - ( $(this).width() / 2 ) + 10 + "px",
						"top": pos.top + 32 + "px",
				}).fadeIn(500);
			})
			.mouseleave(function(){
				$("#video-menu").delay(2000).fadeOut(500);
			})
			.click(function(e){e.preventDefault()})
		
			$("#video-menu").mouseenter(function(){
				$("#video-menu").clearQueue();
			})
			.mouseleave(function(){
				$(this).delay(500).fadeOut(500);
			})

			$("#video-menu a").click(function(e){
				Page.create( BASE_URL + "/?page_id=5&cmd=get_video&video_url=" + $(this).attr("href"))
				Player.stop();
				$("#playPause").addClass("off");
				mp3Player.pause();
				Slider.hide();
				$("body").addClass("pattern");
				$(".background").hide();
				$("#left-arrow,#right-arrow").hide();
				e.preventDefault();
				return false;
			})
		}	
	},

	show: function (){
	
		$("#menu>a").css("opacity","0.0");
	
		$("#menu")
			.css("opacity","0.0")
			.animate({
						top: "0px", 
						opacity: "1.0"
					},
					1000,
					function(){
						Menu.showMenuItem($("#menu a:first"));
					})
			.removeClass("hide");

			$("#menu a.first").addClass("active");
	},

	showMenuItem: function( menu_item ){
		if ( menu_item.length == 0 ) return false;
		
		$(menu_item).animate({
								opacity: "1.0"
							},
							200,
							function(){
								Menu.showMenuItem( $(menu_item).next() );
							})
	
	},

	initLinks: function(){
		$("#menu a.loader").click(function(e){
			Page.create(BASE_URL + "/" + $(this).attr("href"));
			Player.stop();
			Slider.hide();
			$("body").addClass("pattern");
			$(".background").hide();
			e.preventDefault();
			return false;
		})

		$("#home").click(function(){
			$(".page").fadeOut(1000,function(){
				$(this).remove();
				Slider.hide();   
			})
		})
	}
}


var Page = {
	create: function(href,gallery){
		
		Page.remove();

		var container = $("<div />").addClass("page").attr("id","page-content").data("url",href);

		if ( gallery ) container.addClass("gallery");
		
		container.load(href,function(){
			$(this)
				.css({
					left: Math.round( ($(window).width() - 720) / 2 ) + "px",
					top: Math.round( ($(window).height() - 480) / 2 ) + "px",
				})
				.appendTo("body")
				.fadeIn(1000,function(){
					var img = new Image()
					$(img)
						.attr("src", MAIN_URL + "/images/round_delete.png")
						.css({
							position: "absolute",
							right: "10px",
							top: "10px",
							cursor: "pointer"
						})
						.prependTo($(this))
						.click(Page.remove)

						
						
				});

				
				
					
		})
	},

	remove: function(){
		$("#page-content").fadeOut(1000,function(){
			url = $(this).data("url");
			if ( url.indexOf("video") > -1 ){
				$("#playPause").removeClass("off");
				mp3Player.play();
				Player.play();
			}
			$(this).remove();
		})
	}

}


var Gallery ={

	gid: null,

	prev: null,
	
	images: null,

	status: null,

	count: 0,

	audio: null,

	load: function(gallery_id, callbackFunc ){
		
		Gallery.prev = Gallery.gid;
		Gallery.gid = gallery_id;

		$.ajax({
			url: BASE_URL + "/?page_id=5",
			data: "&cmd=get_gallery&gallery_id=" + gallery_id,
			type: "GET",
			success: function(d,t){
				gJson = $.parseJSON(d);
				Gallery.images = gJson.images;
				Gallery.status = gJson.status;
				$.ajax({
					url: BASE_URL + "/?page_id=5",
					data: "&cmd=get_gallery_audio&gallery_id=" + gallery_id,
					type: "GET",
					success: function(d,t){
						Gallery.audio = d;

						if (Gallery.gid > 1 || Gallery.prev != null)
							mp3Player.play(d);
						$("#startup").remove();
						Player.init( callbackFunc );
					}
				})

			}
		})

	},
}


var Player = {
	
	lastIndex: 0,
	loopIndex: 0,

	init:function(callbackFunc){
		Player.lastIndex = Gallery.images.length * 1000;
		if ( callbackFunc != "new" )
			Player.showPicture(0, callbackFunc);
		else{
			Player.lastIndex = -1;
			Player.play();
		}
	},

	stop: function(){
		// if there is an animation in progress, we shuld run it until the next image is shown
		// clear timeout
		
		Player.lastIndex = 0;
		clearTimeout(LOOP)
	},

	pause: function(){
		clearTimeout(LOOP);	
	},

	play: function(boot){
		if (!boot) Player.next();
		LOOP = setTimeout('Player.play()', LOOP_SPEED);
	},

	next: function(){
		
		Player.lastIndex += 1;

		if (!Gallery.images || $("#bg"+(Player.loopIndex-1)).is(':animated') ) return false;

		Player.showPicture( Math.abs(Player.lastIndex % Gallery.images.length) );
				
	},

	prev: function(){
		
		Player.lastIndex -= 1; 

		if (!Gallery.images || $("#bg"+(Player.loopIndex-1)).is(':animated') ) return false;
		
		Player.showPicture( Math.abs(Player.lastIndex % Gallery.images.length) );
		

		
	},

	showPicture: function(index, callbackFunc){

		src = BASE_URL + "/" + Gallery.images[index] + "_backup"

		// get window metrics for position correction #1
		windowWidth = $(window).width();
		windowHeight = $(window).height();

		// create new picture		
		pic = new Image()
		
		picture = $(pic);

		$("#loader").show();
		
		picture
			.addClass('background')
			.attr("id","bg"+Player.loopIndex)
			.load(function(){

				pre = $("#bg"+(Player.loopIndex-1));

				$("body").removeClass("pattern");
				
				if ( pre.length > 0 ){
					picture.insertAfter(pre);

					gindex = index - 1;
					if ( gindex < 0 ) gindex = Gallery.images.length - 1

					if (!Gallery.status[gindex])
						pre.delay(500).fadeOut(FADE_OUT,function(){
							$(this).delay(100).remove();	

						})
					else
						pre.delay(500).hide(FADE_OUT,function(){
							$(this).delay(100).remove();	

						})
				}
				else
					$("body").prepend(picture).css("display","none").fadeIn( FADE_IN, callbackFunc );

				// position correction #1



				if ( picture.width() >= picture.height() && !Gallery.status[index]){
					picture.height( ( windowWidth / picture.width() ) * picture.height() );
					picture.width( windowWidth );					
				}else{
					picture.width( ( windowHeight / picture.height() ) * picture.width() );
					picture.height(  windowHeight );
					picture.css({
						"left": ((windowWidth-picture.width()) / 2) + "px",
						
					})
				}
				
				top_position = -1 * Math.round( Math.abs( picture.height() - windowHeight ) / 2 ) + "px";
				
				picture.css({
					top: top_position,
					zIndex: 100000-Player.loopIndex
				})

				
				Player.loopIndex += 1;

				$("#loader").hide();

			})
			.attr("src",src)
			
	}
}



