/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function() {
    var animation_right = false;
    var animation_left = false;
    var thumb_size = 140;
    var total_length = $('.thumbnails img').length * thumb_size;
    var max_margin_right = total_length * (-1);
    var boundary_right = max_margin_right + (6 * thumb_size);
    total_length += 'px';

    function changeImage(element) {
        var image_src = $(element).attr('src');
        var extension_spot = image_src.indexOf('.jpg');
        var new_image_src = image_src.substring(0, extension_spot);
        new_image_src += '_full.jpg';

        var new_image = $('<img />').attr('src', new_image_src);

        $('#gallery-container img').remove();
        $('#gallery-container').append(new_image);
    }

    $('.thumbnails img').first().css('opacity', 1);

    $('.thumbnails img').hover(function() {
        changeImage(this);

        $('.thumbnails img').css('opacity', 0.5);
        $(this).css('opacity', 1);
    });

    

    $('.thumbnails ul').css('width', total_length);

    $('#rightArrow').hover(function() {
       animation_right = true;
       // da puta madre
       animateRight();
    }, function() {
        animation_right = false;
    });

    $('#leftArrow').hover(function() {
       animation_left = true;
       animateLeft();
    }, function() {
        animation_left = false;
    });

    function animateRight() {
        if(animation_right == true)
        {
            var current_margin = $('.thumbnails ul').css('marginLeft');
            current_margin = current_margin.substr(0, current_margin.indexOf('px'));
            
            if(current_margin >= boundary_right)
            {
                $('#leftArrow').css('backgroundPosition', '0 0');
                $('#leftArrow').css('cursor', 'pointer');

                current_margin -= thumb_size;
                current_margin += 'px';

                $('.thumbnails ul').animate({
                    marginLeft: current_margin
                }, 'slow', function() {
                    animateRight();
                });
            }
            else
            {
                $('#rightArrow').css('backgroundPosition', '-115px 0');
                $('#rightArrow').css('cursor', 'default');
            }
        }
    }

    function animateLeft() {
        if(animation_left == true)
        {
            $('#rightArrow').css('cursor', 'pointer');
            $('#rightArrow').css('backgroundPosition', '-33px 0');

            var current_margin = $('.thumbnails ul').css('marginLeft');
            current_margin = parseInt(current_margin.substr(0, current_margin.indexOf('px')));

            if(current_margin < 0)
            {
                current_margin += thumb_size;
                current_margin += 'px';

                $('.thumbnails ul').animate({
                    marginLeft: current_margin
                }, 'slow', function() {
                    animateLeft();
                });
            }
            else
            {
                $('#leftArrow').css('backgroundPosition', '-79px 0');
                $('#leftArrow').css('cursor', 'default');
            }
        }
    }

    var host = location.hostname;
    var url;

    if(host == 'localhost')
    {
        url = 'http://localhost:8080/newid/new_id/';
    }
    else
    {
        url = 'http://www.new-id.co.uk/';
        //url = 'http://www.new-id.co.uk.php5-19.dfw1-2.websitetestlink.com/';
    }

    $('#gallery_select').change(function() {
        window.location = url + $(this).val();
    })
});

