var lastClick;
var vid = false;

$(document).ready(function () {

	$("#menu li:not(.selected,li ul li)").hover(
		function () { $(this).addClass("selected"); },
		function () { $(this).removeClass("selected"); }
	)
	$("#menu li:not(li ul li)").hover(
		function () { $(this).children("ul").css("display", "block"); },
		function () { $(this).children("ul").css("display", "none"); }
	)
	
	$("#menu li").click(function() {
		document.location.href=$(this).find('a').attr('href');
	});

	
	$('#search').defaultValue();

	$("img.menu")
		.mouseover(function () { $(this).stop(true).animate({ width: 132, height: 322, 'margin-top': 0 }, 200, 'linear'); })
		.mouseout(function () { $(this).animate({ width: 119, height: 286, 'margin-top': 16 }, 200, 'linear'); });
	
	
	$(".left-image1").reflect( { height: 0.2 } );
	$(".left-image2").reflect( { height: 0.2, opacity: 1 } );	
	
	slideSwitch();
	setInterval("slideSwitch()", 7000 );
	
});

function slideSwitch() {
	var pause = 1;
	$(".image-fader").each( function () {
		var $active = $(this).children('img.active');
		
		if ($active.length == 0) $active = $(this).children('img:last');
		
		var $next = $active.next().length ? $active.next() : $(this).children('img:first');
		$active.addClass('last-active');

		$next.css({opacity: 0.0})
			.addClass('active')
			.delay(pause * 2000)
			.animate({opacity: 1.0}, 1800, function() {
				$active.removeClass('active last-active');
			});
		pause++;
	})
}

function getGalleryMenu(section, selected) {
	$.post(window.location.href, { "display-gallery-menu": true, "section": section, "selected": selected }, function (data) {
		$('#gallery1').html(data)
		parseGalleryMenu();
	});
	getGalleryImages(section, selected);
}

function parseGalleryMenu() {
	var $gallery1 = $('#gallery1');
	var opacity_hidden = 0.3;
	var setOpacity = function () {
		$('#textup').css('opacity', ($gallery1.scrollTop() == 0) ? opacity_hidden : 1);
		$('#textdown').css('opacity', ($gallery1.children('ul').height() <= $gallery1.scrollTop() + $gallery1.height()) ? opacity_hidden : 1);
	}
	
	$gallery1
		.animate({ 'scrollTop' : '0px' }, 0)
		.parent()
		.append('<img src="images/arrow-up2.gif" id="textup" style="position: absolute; top: 20px; right: 0px; padding: 8px;" />')
		.append('<img src="images/arrow-down2.gif" id="textdown" style="position: absolute; bottom: 10px; right: 0px; padding: 8px;" />')
		
	$gallery1
		.find('li')
		.click(function () {
			$(this)
				.addClass('selected')
				.siblings()
					.removeClass('selected');
			
			$('.cloud').hide();
			$('.right').show();
			$('.left .title').show();
			$('.bottom').eq(0).show();
			$('.bottom').eq(1).hide();
			
			var t = $(this);
			var id = $(this).attr('data-id');
			var p = $(this).attr('data-parent');
			var h = false;
			
			$('.left .title').html($(this).children('a').html());
			
			if (p == 0) {
				setTimeout(function () { $gallery1.animate({ scrollTop: t.position().top + $gallery1.scrollTop() - 6 }, 200) }, 50); // Top scroll
				$(this).siblings().each(function () {
					var p = $(this).attr('data-parent');
					//console.log(lastClick);
					if (lastClick == id && p != 0) p = -1; // Break it
					$(this).css('display', (p == id || p == 0) ? 'block' : 'none');
				});
				h = lastClick == id;
				lastClick = h ? null : id;
			}
			if (!h) getGalleryImages($('#gallery1').attr('data-section'), id);
		})
		.children('a').click(function (e) { e.preventDefault(); });
	
	var t = $gallery1.find('li.selected');
	var i = t.attr('data-id');
	var p = t.attr('data-parent');
	$gallery1.find('li[data-parent=' + i + ']').css('display', 'block');
	$gallery1.find('li[data-parent=' + p + ']').css('display', 'block');
	
	$gallery1.mousewheel(function (e, delta) { if (delta > 0) $('#textup').click(); else $('#textdown').click(); });
	
	$('#textup')
		.click(function () { 
			var scrollTo = $gallery1.scrollTop() - $gallery1.height() + 24;
			$gallery1.stop().animate({ 'scrollTop' : scrollTo }, 300, setOpacity);
		})
		.css('opacity', opacity_hidden);

	$('#textdown')
		.click(function () { 
			var scrollTo = $gallery1.scrollTop() + $gallery1.height() - 24;
			$gallery1.stop().animate({ 'scrollTop' : scrollTo }, 300, setOpacity);
		})
		.css('opacity', ($gallery1.children('ul').height() < $gallery1.height()) ? opacity_hidden : 1);
	
}

function getGalleryImages(section, selected) {
	$("div.right").html('');
	$("#gallery2").html('');
	
	$.post(window.location.href, { "display-gallery-images": true, "section": section, "selected": selected }, function (data) {
		$("#gallery2").html(data);
		parseGalleryImages();
		$("#gallery2").children('li:first').click();
	});
}

function parseGalleryImages() {
	var mover = $("#gallery2").siblings('.mover');
	$("#gallery2")
		.children('li')
			.css("cursor", "pointer")
			.css("top","0")
			.click(function () {
				
				getGalleryImage($(this).attr('data-id')); 
				mover.stop().animate({left: $(this).position()['left'] + $(this).parent().parent().scrollLeft() }, 800);
				$(this).addClass('selected').siblings('.selected').removeClass('selected');
			})
			.find('a').click(function (e) { e.preventDefault(); });
	
	resetSlider();
	
	$("#gallery2").children('li.selected').each(function () {
		mover.stop().animate({left: $(this).position()['left'] + $(this).parent().parent().scrollLeft() }, 800);
	});
	
	$("#gallery2 li div img").each(function () {
		$(this).load(function () { 
			$(this).css('top', 0); 
			setTimeout((function (_) { return function () { $(_).reflect({ height: 0.2, opacity: 0.5 }); }})($(this)), 1.6)
		});
	});
	
	
	$('#gallery2').width($('#gallery2 li').length * (86 + 16));
}

function getGalleryImage(selected) {
	$("div.right").html('<div id="main-image"></div>');
	$.post(window.location.href, { "display-gallery-image": true, "selected": selected }, function (data) {
		$("div.right").html(data);
		parseGalleryImage();
	});
}

function parseGalleryImage()
{
	vid = false;
	$('#lightbox-content object').remove();
	$('#lightbox-content span').remove();
	
	if ($('#main-image object').length && $('#lightbox-overlay').is(':visible'))
	{
		var t = $('#main-image').html();
		$('#main-image').html('');
		$('#lightbox-content img').hide();
		$('#lightbox-content').append($(t)).stop().css({ width: 480, height: 380, opacity: 1, display: 'block' });
		vid = true;
		return;
	}
	else
	{
		$('#lightbox-content img').show();
		$('#lightbox-content').css({ width: 'auto', height: 'auto' });
	}
	
	if ($('#lightbox-overlay').length == 0) {
		$('body').append('<div id="lightbox-overlay"></div><div id="lightbox-content"><img alt="" /><div class="next"></div><div class="prev"></div></div>');
		var lightbox = $('#lightbox-content');
		lightbox.children('img').load(function () {
			if ($(this).attr('src') == null) return;
			lightbox.stop(true, true).css({ display: 'none', left: ($(window).width() - 16 - lightbox.width()) / 2, top: ($(window).height() - lightbox.height()) / 2 }).fadeIn(500);
			$('div', lightbox).css({ 'top': $(this).height() / 2 - 16 });
		}).load();
		
		$('#lightbox-overlay').click(function () {
			$('#lightbox-content').prev().fadeOut(500).end().fadeOut(200, function () { $(this).css({ 'margin-left': -9999, display: 'block' }); });
			if (vid) setTimeout(function () { $('#gallery2 li.selected').click(); }, 500);
		});
		
		$('#lightbox-content').click(function (e) {
			var tag = $(e.target).get(0).tagName.toLowerCase();
			if (tag == 'a' || tag == 'object') return;
			$('#lightbox-content').fadeOut(200);
			$('#gallery2 li.selected').next().click();
		});
		
		$('#lightbox-content')
			.mouseover(function () { $('#lightbox-content div').stop().fadeTo(100, 1); })
			.mouseout (function () { $('#lightbox-content div').stop().fadeTo(100, 0); });
		
		$('#lightbox-content div').click(function (e) {
			e.stopPropagation();
			$('#lightbox-content').fadeOut(200);
			if ($(this).attr('class') == 'next')
				$('#gallery2 li.selected').next().click();
			else
				$('#gallery2 li.selected').prev().click();
		});
	}
	
	$('#main-image img')
		.load(function () {
			var t = $(this).parent().height() / 2 - $(this).height() / 2;
			if (t < 0) t = 0;
			$(this)
				.fadeIn(500)
				.css('position', 'relative')
				.css('top', t);
				
			$('#lightbox-content img').attr('src', $(this).attr('src').replace(/\.([^.]+)$/, '-large.$1'));
			
		}).css('cursor','pointer').click(function () {
			
			$('#lightbox-overlay').fadeTo(400, 0.95, function () { $('#lightbox-content').css({ 'margin-left': 0, display: 'none' }).fadeIn(500); });
			
			// Hide
		});
	
}

function resetSlider() {
	var mover = $("#gallery2").siblings('.mover');
	mover.siblings('.bottom').scrollTop('0');
	mover.css('left', -165);
}

function loadFlash(div, flash, video, width, height) {
	var params={};
	var flashvars={};
	var attributes={};
	params.menu="false";
	params.quality="high";
	params.bgcolor="#000000";
	params.allowFullScreen="true";
	params.base=".";
	params.salign="m";
	params.scale="default";
	params.wMode="transparent";
	
	attributes.allowFullScreen="true";
	attributes.id=div;
	attributes.name=div;
	
	flashvars.vidfile = video; //"QatarNationalDay2008.flv";
	flashvars.skinfile = 'SkinOverPlayStopSeekFullVol.swf'; //"QatarNationalDay2008.flv";
	
	var ts=new Date().getTime();flashvars
	
	swfobject.embedSWF(flash+'?'+ts,div,width,height,"9.0.0",false,flashvars,params,attributes);
}

	

