$(document).ready(function(){
	
	//console.log($(window).width());
	
	$(window).resize(function(){
		if ($(window).width()<960) {
			//$("#slider").css('height', '300');
			$("#slider img").css('width', 'auto');
		} else {
			$("#slider img").css('width', '100%');
		}
	});
	
	//$("#slider img").css("display", "inline");
	
	$("#slider img").live("click", function(){
			$this = $(this);
			$(this).clone().prependTo("#slider");
			$(this).fadeOut("slow", function(){
				$this.remove();
			});
	});
	
	function fading() {
		$this = $("#slider img:last");
		$("#slider img:last").clone().prependTo("#slider");
		$("#slider img:last").fadeOut(1000, function(){
			$this.remove();
		});
	}
	
	f = setInterval(
		fading,
		5000
	);
	
	if ($("#mainCont").height()<$("#secCont").height()) {
		$("#mainCont").css('height', $("#secCont").height());
	}
	
	var $url = document.location.href;
	if ($url.indexOf("?") != -1) {
		var $q = document.location.href.split("?")[1]; // query string
		//var $e = $q.split("/"); // query string elements
		$e = (decodeURIComponent($q)).split("/");
	}
	
	function showMovie(moviename) {
		var flashvars = {
			videoURL: moviename+"/"+moviename+".flv",
			autoPlay: false,
			startPhotoSource: "videos/"+moviename+"/"+moviename+"_big.jpg",
			backgroundColor1: 0x333333,
			backgroundColor2: 0x222222,
			highlightColor: 0x0099ff
		};
		var params = {
			//codebase: 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
			width: 750,
			height: 470,
			w3c: true,
			src: 'videos/jcplayer.swf',
			quality: 'high',
			//pluginspage: 'http://www.macromedia.com/go/getflashplayer',
			align: 'middle',
			play: 'true',
			loop: 'true',
			scale: 'noscale',
			wmode: 'opaque',
			devicefont: 'false',
			id: 'videos/jcplayer',
			bgcolor: '#000000',
			name: 'videos/jcplayer',
			menu: 'true',
			allowFullScreen: 'true',
			allowScriptAccess: 'sameDomain',
			movie: 'videos/jcplayer',
			salign: 'TL'
		};
		var attributes = {};
		swfobject.embedSWF("videos/jcplayer.swf", "video", "750", "470", "8.0.0", "expressInstall.swf", flashvars, params, attributes);
	}
	
	function refresh_FB_like_btn(virtualLink, context) {
		$(".fb_like", context).remove();
		
		$sharedLink = document.location.hostname + document.location.pathname + "?" + virtualLink;
		
		$iframe = 
		'<iframe src="http://www.facebook.com/plugins/like.php?app_id=254417687921097&amp;href='
		+$sharedLink+
		'&amp;send=false&amp;layout=button_count&amp;width=120&amp;show_faces=false&amp;action=recommend&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:120px; height:21px;" allowTransparency="true" class="fb_like"></iframe>';
		
		$(".galleryTitle", context).append($iframe);
	}
	
	$("#switch2videos").click(function(){
		if ($q) { // query string in the url
			if ($e[0]=="photo") {
				document.location.href = document.location.pathname + "?video"; // reload
			}
		}
		$("#photoGal").hide();
		$("#videoGal").show();
		return false;
	});
	$("#switch2photos").click(function(){
		if ($q) { // query string in the url
			if ($e[0]=="video") {
				document.location.href = document.location.pathname + "?photo"; // reload;
			}
		}
		$("#videoGal").hide();
		$("#photoGal").show();
		return false;
	});
	
	if ($("#galleria").length>0) {
		// Load the classic theme
		Galleria.loadTheme('js/galleria.classic.min.js');
		
		if ($q) { // query string in the url
			if ($e[0]=="photo") {
				var source = galleries[$e[1]];
				if ($e[2]) {
					var g = galleries[$e[1]];
					for (firstindex=0; firstindex<g.length; firstindex++) {
						if (g[firstindex]['image'] == 'pic/'+$e[1]+'/'+$e[2]) {
							break;
						}
					}
				} else {
					var firstindex = 0;
				}
				
				$('#galleria').galleria(
					{
						width: 960,
						height: 480,
						transition: "fade",
						lightbox: true,
						debug: false,
						data_source: source,
						show: firstindex
					}
				);
				
				$("#galleryName").text( $("a."+$e[1]+" img").attr('alt') );
				
				$("#photoGal").data('currentGalleryName', $e[1]);
				$("#photoGal").data('currentPhoto', galleries[$e[1]][firstindex]["image"]);
			}
		} else {
			$('#galleria').galleria(
				{
					width: 960,
					height: 480,
					transition: "fade",
					lightbox: true,
					debug: false
				}
			);
			$("#photoGal").data('currentGalleryName', $("#gallerySelector a:eq(0)").attr("class"));
			$("#photoGal").data('currentPhoto', galleries[$("#gallerySelector a:eq(0)").attr("class")][0]["image"]);
		}
		
		if ($("#photoGal").data('currentPhoto')) {
			refresh_FB_like_btn(($("#photoGal").data('currentPhoto')).replace("pic","photo"), "#photoGal");
		}
		
		$("#gallerySelector a").click(function(){
			gallery = Galleria.get(0);
			gallery.splice(0,100);
			for (i=0; i<galleries[this.className].length; i++) {
				gallery.push(galleries[this.className][i]);
			}
			gallery.show(0);
			
			$("#galleryName").text( $(this).children('img').attr('alt') );
			
			$("#photoGal").data('currentGalleryName', this.className);
			$("#photoGal").data('currentPhoto', galleries[this.className][0]["image"]);
			
			refresh_FB_like_btn(($("#photoGal").data('currentPhoto')).replace("pic","photo"), "#photoGal");
			
			return false;
		})
		
		$(".galleria-image img").live('click', function(){
			currIdx = $(this).parent().prevAll().length;
			$("#photoGal").data('currentPhoto', galleries[$("#photoGal").data('currentGalleryName')][currIdx]["image"]);
			
			refresh_FB_like_btn(($("#photoGal").data('currentPhoto')).replace("pic","photo"), "#photoGal");
			
		});
	}
	
	if ($("#videoSelector").length>0) {
		
		// selecting another video 
		$("#videoSelector a").click(function(){
			$(".mediabox object").remove();
			$(".mediabox:visible").append('<div id="video"></div>');
			$moviename = $(this).attr('rel');
			showMovie($moviename);
			$("#videoName").text( $(this).children('img').attr('alt') );
			$("#videoGal").data('currentVideo', $(this).attr('rel'));
			refresh_FB_like_btn("video/"+$("#videoGal").data('currentVideo'), "#videoGal");
			return false;
		})
		
		// at startup: select first video
		//showMovie($("#videoSelector a:eq(0)").attr('rel'));
		
		if ($q) {
			if ($e[0]=="video") {
				if (!$("#videoGal").is(":visible")) {
					$("#switch2videos").click();
				}
				if ($e[1]) {
					showMovie($e[1]);
					$("#videoName").text( $("a[rel="+$e[1]+"] img").attr('alt') );
					$("#videoGal").data('currentVideo', $e[1]);
				} else {
					showMovie($("#videoSelector a:eq(0)").attr('rel'));
					$("#videoGal").data('currentVideo', $("#videoSelector a:eq(0)").attr('rel'));
				}
			}
		} else {
			showMovie($("#videoSelector a:eq(0)").attr('rel'));
			$("#videoName").text( $("#videoSelector dd:eq(0) span").text() );
			$("#videoGal").data('currentVideo', $("#videoSelector a:eq(0)").attr('rel'));
		}
		
		refresh_FB_like_btn("video/"+$("#videoGal").data('currentVideo'), "#videoGal");
	}
	
	$(".shareThis").click(function(){
		
		if (this.id=="photoShare") {
			currentPicLink = ($("#photoGal").data('currentPhoto')).replace("pic","photo");
			sharedLink = document.location.hostname + document.location.pathname + "?" + currentPicLink;
		}
		if (this.id=="videoShare") {
			sharedLink = document.location.hostname + document.location.pathname + "?video/" + $("#videoGal").data('currentVideo');
		}
		
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(sharedLink)+'&t='+encodeURIComponent(document.title), "sharer", "toolbar=0,status=0,width=626,height=436");
		
		return false;
	});
	
	$(".sendThis").click(function(){
		
		if (this.id=="photoSend") {
			currentPicLink = ($("#photoGal").data('currentPhoto')).replace("pic","photo");
			sharedLink = "http://" + document.location.hostname + document.location.pathname + "?" + currentPicLink;
			$("#photoSendLink").val(sharedLink);
			$("#photoSendBox").toggle();
		}
		
		if (this.id=="videoSend") {
			sharedLink = "http://" + document.location.hostname + document.location.pathname + "?video/" + $("#videoGal").data('currentVideo');
			$("#videoSendLink").val(sharedLink);
			$("#videoSendBox").toggle();
		}
		
		$(".sendBox a").click(function(){
			$(this).parent().hide();
			return false;
		});
		
		return false;
	});
	
});

