var nbImageLB = 0;
var a = 0;
var temp = '';
var xmlToLoad = '';
var oldMenu = -1;
var nbrec = 0;

    function loadMenu() {
        $.get('xml/gallery.xml', function (m) {

			var htmlMenu = '';

            $(m).find('gallery').each(function (aa) {
				var $menu = $(this);

				if (aa == 0)
					xmlToLoad = $menu.find("xmlpath").text();

				var sectionToLoad = $menu.find("xmlpath").text();
				var sectionTitle  = $menu.find('name').text();
				var sectionButton = $menu.find('buttonName').text();

				htmlMenu += '<a href="#" id="menu' + aa + '" class="menuItem" onclick="initLib(\'' + sectionToLoad + '\'); loadSection(' + aa + ');"> ' + sectionButton + '</a> | ';
            });

			htmlMenu = htmlMenu.substring(0, htmlMenu.length - 2);

			$('#menu').append(htmlMenu);

			loadLinks();
			loadSection(0);
        });
    };

    function loadLinks() {
        $.get('xml/links.xml', function (l) {
			var pos = 0;

			var sectionDesc  = $(l).find('library').attr('description');
			var sectionTitle = $(l).find('library').attr('libName');

			var linkHtml = '';

			$(l).find('item').each(function (i) {
				var $link = $(this);
				var url = $link.attr("itemName");
				var description = $link.find('itemDescription').text();
				var linkSection = $link.attr("itemSection");

				if (i == 0)
					linkHtml += '<tr>';
				else if (((i % 4) == 0))
					linkHtml += '</tr><tr>';

				linkHtml += '<td align=left class=link onclick="javascript:window.open(\'' + url + '\');"><span class="' + linkSection + '">' + description + '</span></td>';
			});
			linkHtml += '</tr>';

			$('#lSectionName').empty().append(sectionTitle);
			$('#lSectionDesc').empty().append(sectionDesc);

			$("#tblLinks").empty().append(linkHtml);
			$("#linksTitle").append('<span class="title"> </span>');
		});
	};

	function loadSection(currentMenu) {
	    $("#tblImages").slideToggle("fast", function () {
	        loadImagesData(currentMenu, 's');
	    });
	}

	function loadImages(currentMenu) {
		$("#tblImages").fadeOut("slow", function () {
	        loadImagesData(currentMenu, 'i');
	    });
	}

	// LOAD IMAGES BASED ON XML FILE
    function loadImagesData(currentMenu, type) {
        $.get('xml/' + xmlToLoad, function (d) {

            $('#menu' + oldMenu).removeClass("menuItemCurrent");
            $('#menu' + oldMenu).addClass("menuItem");
            $('#menu' + currentMenu).removeClass("menuItem");
            $('#menu' + currentMenu).addClass("menuItemCurrent");

            oldMenu = currentMenu;

            nbrec = $(d).find('item').length;
			
            var pos = 0;
            var count = (a + 1) + '-';

            var sectionDesc = $(d).find('library').attr('description');
            var sectionTitle = $(d).find('library').attr('libName');

            if (a == 0) {
                $('#prev').attr('style', 'display:none;');
            }
            else
                $('#prev').attr('style', 'display:block;');

            $('#next').attr('style', 'display:block;');

            var thumbPath = "img/thumbs/";
            var largePath = "img/large/";

            $(d).find('item').each(function (i) {
                if ((a >= 0) && (i >= a) && (i < (a + 12))) {
                    var $image = $(this);
                    var title = $image.attr("itemName");
                    var description = $image.find('itemDescription').text();
                    var imageUrl = $image.attr('itemPath');

                    $('#img' + pos).removeAttr('style');
                    $('#img' + pos).attr('src', thumbPath + imageUrl);
                    $('#a' + pos).attr('href', largePath + imageUrl);
                    $('#a' + pos).attr('title', title + '\n' + description);

                    pos++;
                }

                if (i > (a + 11)) {
                    return false;
                }

                if ((i + 1) == nbrec)
                    $('#next').attr('style', 'display:none;');
            });

            count += (a + pos).toString();

            nbImageLB = (pos);

            for (var t = pos; t < 12; t++) {

                $('#img' + t).removeAttr('src');
                $('#img' + t).attr('style', 'visibility:hidden;');
                $('#a' + t).removeAttr('title');
                $('#a' + t).removeAttr('rel');
                $('#a' + t).removeAttr('href');

                $('#next').attr('style', 'visibility:hidden;');
            }

            $('#pSectionName').empty().append("Portfolio - " + sectionTitle);
            $('#pNbItems').empty().append(count + ' / ' + nbrec);
            $('#pSectionDesc').empty().append(sectionDesc);

            if (type == 's')
                $("#tblImages").slideToggle();
            else
                $("#tblImages").fadeIn();

            doTimer();
        });
    };




	// START ANIMATION ON IMAGES
	var c=0;
	var t;
	var timer_is_on=0;
	var prev = 0;
	var next = 0;

	function timedCount()
	{

		if (c == (nbImageLB-1))
		{
			c = 0;
			prev = nbImageLB-1;
		}
		else
		{
			prev = c;
			c = c + 1;
		}

		next = c;
		
		$('#img' + next).fadeTo("slow", 1.0);
		$('#img' + prev).fadeTo("slow", 0.7);

		t = setTimeout("timedCount()",250);
	}

	function doTimer()
	{
		if (!timer_is_on) {
			timer_is_on = 1;
			timedCount();
		}
	}


	function stopCount()
	{
		clearTimeout(t);
		timer_is_on=0;
	}
	// END ANIMATION ON IMAGES







    $(document).ready(function() {
        $('#gallery a').lightBox();

		$("#gallery img.image").hover(function(){
			//stopCount();
			$(this).fadeTo("fast", 1.0); // This should set the opacity to 100% on hover
		},function(){
			$(this).fadeTo("fast", 0.7); // This should set the opacity back to 60% on mouseout
			//doTimer();
		});

		$('#next').click(function () {
			Next();
		});

		$('#prev').click(function () {
			Prev();
		});    

	});

	function initLib(lib)
	{
		xmlToLoad = lib;
		a = 0;
	}

    function Next() {
        a += 12;
        temp = '';
        loadImages(oldMenu);
    }

    function Prev() {
        a -= 12;
        temp = '';
        loadImages(oldMenu);
    }

