var right_arrow;
var left_arrow;
var container;
var slider;
var con;
var divs;
var width = 94;
function init () 
{
	$("div.n_m").hover(
		function () 
		{
			$(this).attr("class", "h_m");
		}, 
		function () 
		{
			$(this).attr("class", "n_m");
		}

	);
	$("div.n1_m").hover(
		function () 
		{
			$(this).attr("class", "h1_m");
		}, 
		function () 
		{
			$(this).attr("class", "n1_m");
		}

	);
	$("div.fn_m").hover(
		function () 
		{
			$(this).attr("class", "fh_m");
		}, 
		function () 
		{
			$(this).attr("class", "fn_m");
		}

	);
	$('div.main_left_title').click(
		function () 
		{
			if($(this).next().css('display') == 'block')
			{
				$(this).next().fadeOut("slow");
			}
			else
			{
				$(this).next().fadeIn("slow");
			}
		}
	);
	$('#right_arrow').click(
		function () 
		{
			return false;
		}
	);
	$('#left_arrow').click(
		function () 
		{
			return false;
		}
	);
	
	$(".more img").hover(
		function () 
		{
			$(this).attr('src', '/images/h_more.png');
		}, 
		function () 
		{
			$(this).attr('src', '/images/more.png');
		}

	);
}
function showImage(idx)
{
    var img = $("#central_image");
    img.attr('src', images[idx].central);
    var a = img.parent('a').get(0);
    $(a).attr('href', images[idx].big);
}
function initializePage()
{
    container = $("#ImageContainer");

    for (i=0; i<images.length; i++)
    {
        var el = document.createElement("div");
        el = $(el);
        el.css('float', 'left');

        var img = "";
        img += "<img src='"+(images[i].img)+"' onclick='showImage("+i+")' />"; 

        el.html(img);
        container.append(el);
        
    }

    right_arrow = $('#right_arrow');
    right_arrow.bind('click', moveRight);
    left_arrow = $('#left_arrow');
    left_arrow.bind('click', moveLeft);

    divs = $('div', container);
}
function moveLeft()
{
    removeHandlers();

    var first = $('div:first', container);

    first.animate({
        width: '0'
    }, 300, undefined, function(){
        container.append(first);
        first.css('width', width);
        attachHandlers();
    });
}

function moveRight()
{
    removeHandlers();

    var first = $('div:first', container);
    if (parseInt(first.css('width')) < width)
    {
        var last = first;
    }
    else
    {
        var last = $('div:last', container);
        last.css('width', 0);
        container.prepend(last);
    }

    last.animate({
        width: width
    }, 300, undefined, function(){
        attachHandlers();
    });
}

function removeHandlers()
{
    divs.stop();
}

function attachHandlers()
{
    
}

function slide(e)
{
    removeHandlers();

    // Right
    if (con.offset().left + con.width()/2 <= e.pageX)
    {
        var repeat = function() {
            var first = $('div:first', container);
            if (parseInt(first.width()) < width)
            {
                var last = first;
            }
            else
            {
                var last = $('div:last', container);
                last.css('width', 0);
                container.prepend(last);
            }

            last.animate({
                width: width
            }, 1500, undefined, repeat); 

        };

        con.bind('mouseout', cleanSlide);

        repeat();
    }
    // Left
    else
    {
        var repeat = function() {
            var first = $('div:first', container);
            if (parseInt(first.width()) <= 0)
            {
                container.append(first);
                first.css('width', width);
                first = $('div:first', container);
            }

            first.animate({
                width: 0
            }, 1500, undefined, repeat); 

        };

        con.bind('mouseout', cleanSlide);

        repeat();
    }
}

function cleanSlide()
{
    $('div', container).stop();
    
    con.unbind('mouseout', cleanSlide);
    attachHandlers();
}



