var objectsGap = 0;

$(document).ready(function() {

    var objectsGap = $(window).width()/2.5;
    var element_prefix = '';

    var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);

    var swipeOptions = {swipe:swipe, threshold:0}
            
    $(function() {
        if (agentID)
            $(".object_picture").swipe( swipeOptions );       
    });
       
       
    $('.payment_button_a').click(function() {
        window.location.href = "http://uniservis.org/cgi-bin/payment.cgi";
    });
            
    $('.object_tab2 a').click(function() {
        window.location.href =  "/commercial/";
    });

    $('.object_tab1 a').click(function() {
        window.location.href =  "/";
    });
                   
    function swipe(event, direction) {

        var object_id = ($(event.target).parent().attr('id').replace('_picture', ''));
        var object_num = parseInt(object_id.replace('object_', ''));

        if ($('#'+object_id+'_picture').hasClass('object_picture_active')) {
            if (direction == 'left')
                object_num++;
            
            if (direction == 'right')
                object_num--;
        }

        if (object_num > $(".object_picture").size())
            object_num = $(".object_picture").size();

        if (object_num < 1)
            object_num = 1;

        selectObject($('#object_'+object_num));

    }

    $('.page_object_details .object_picture').click(function() {
        if (agentID)
            return ;

        if ($(this).hasClass('object_picture_active'))
            return ;
        selectObject($('#'+$(this).attr('id').replace('_picture', '')));
    });

    $('.object_label table').click(function() {
        selectObject($(this).find('.link'));
        scrollContent();
    });

    $('.object_label').mouseenter(function() {
        $(this).addClass('hover');
    }).mouseleave(function() {
        $(this).removeClass('hover');
    });

    $('.object_picture').mouseenter(function() {
        var el_id = $(this).attr('id').replace('picture', 'label');
        $('#'+el_id).addClass('hover');
    }).mouseleave(function() {
        var el_id = $(this).attr('id').replace('picture', 'label');
        $('#'+el_id).removeClass('hover');
    });

    $('.page_object').click(function() {
        selectObject($(this).find('a'));
        scrollContent();
    });

    if ($('#modal_background').is(':visible'))
        resizePaymentModal();

    $('#modal_background, #payment_modal .modal_close').click(function() {
        $('#modal_background').hide();
        $('#payment_modal').hide();
    });

    $('.payment_col1 .link').click(function() {
        showPaymentModal();
        resizePaymentModal();
    });



    setInterval('slideNews();', 10000);

    jQuery.preLoadImages("/images/sky.jpg", "/images/disp-box.png", "/images/bushes.png", "/tmp/arcodisole-big.png", "/images/buttom-label-left.png", "/images/buttom-label-center.png", "/images/buttom-label-right.png", "/images/object-active-left.png", "/images/object-active-right.png", "/images/object-active-center.png");

});

$(window).resize(function() {

    if ($('#modal_background').is(':visible')) {
        resizePaymentModal();
    }
        

	if (!$('.page_object_details').is(':visible'))
	    return;

    setPreviewPosition();

});

function selectObject(activeElement) {
    $('.page_object').removeClass('page_object_active');

        //activeElement = $(this);

        if ($(activeElement).hasClass('link')) {
            var active_id = $(activeElement).parents('.object_label').attr('id').replace('_label', '');
            $('#'+active_id).parents('.page_object').addClass('page_object_active');
            element_prefix = '#'+active_id;
        } else
           $(activeElement).parents('.page_object').addClass('page_object_active');

        if (!$('.page_object_details').is(':visible')) {

            var element_prefix = '#'+$(activeElement).attr('id');
            if (!$(element_prefix+'_picture').length)
                return;
            $(element_prefix+'_picture').addClass('object_picture_active');
            $(element_prefix+'_label').addClass('active_label');
            $('.page_object_details').show();

            $('.page_object_details').animate({height: '500px'}, 'fast', function() {
                $(element_prefix+'_content').fadeIn();
                setPreviewPosition();
                $('.object_details_background').fadeIn();
                scrollContent();
            });  
             
            $('.object_labels_container').show().animate({height: '100px'});  

            setPreviewPosition();

        } else {

            objectsGap = $(window).width()/2.5;

            if (!$(activeElement).hasClass('link'))
                element_prefix = '#'+$(activeElement).attr('id');

            if (!$(element_prefix+'_picture').length)
                return;
            
            if ($(element_prefix+'_picture').hasClass('object_picture_active'))
                return ;

            $('.object_picture').removeClass('object_picture_active');
            $(element_prefix+'_picture').addClass('object_picture_active');


            var active_index = 0;
            for(var i=0; i<object_images.length; i++) {
                if (object_images[i]['id']+'_picture'==$('.object_picture_active').attr('id')) {
                        $('#'+object_images[i]['id']+'_picture').animate({width: object_images[i]['im_width'], height: object_images[i]['im_height'], left: i*objectsGap-object_images[i]['im_width']/2}, 'slow');
                        $('#object_picture_container').animate({left: $(window).width()/2-parseInt($('.object_picture_active').css('left'))-parseInt($('.object_picture_active').css('width'))/2}, 'slow', function() {
                            $('.object_label').removeClass('active_label');
                            $('#'+active_index+'_label').addClass('active_label');
                            $('#'+active_index+'_content').fadeIn();
                            $('.labels_container').stop().fadeIn();
                            for(var j=0; j<object_images.length; j++) {
                                $('#'+object_images[j]['id']+'_label').show().css({left: j*objectsGap-$('#'+object_images[j]['id']+'_label').width()/2});
                            }
                            
                        });
                        $('.labels_container').fadeOut(100).animate({left: $(window).width()/2-parseInt($('.object_picture_active').css('left'))-parseInt($('.object_picture_active').css('width'))/2}, '100');
                        $('.object_content').fadeOut();
                        active_index = object_images[i]['id'];
                } else {
                        $('#'+object_images[i]['id']+'_picture').animate({width: object_images[i]['prev_width'], height: object_images[i]['prev_height'], left: i*objectsGap-object_images[i]['prev_width']/2}, 'slow');
                }
            }            

        }
}

function scrollContent() {
    $('html,body').animate({scrollTop: $('.page_object_details').offset().top}, 400);
}

function showPaymentModal() {
    $('#modal_background').show();
    $('#payment_modal').show();
}

function resizePaymentModal() {
    $('#modal_background').height($('.page_container').height()).width($('.page_container').width());
    $('#payment_modal').css({left: ($(window).width()-$('#payment_modal').width())/2, top: $(window).scrollTop()+($(window).height()-$('#payment_modal').height())/2});
    $('#modal_background').height($(document).height());
}

function slideNews() {
    
    $('.page_news_slider').animate({top: parseInt($('.page_news_slider').css('top'))-$('.page_news_slider .news').height()}, 'slow', function() {
        if (($('.page_news_slider').height()+parseInt($('.page_news_slider').css('top')))<=$('.page_news_slider .news').height()) {
            $('.page_news_slider').css({top: 0});
        }
    });
}

function setPreviewPosition() {

    $('.object_details_background').width($(window).width()).height($(window).width()/1.46);
    $('.hidden_container').height($('.object_details_background').height());
    
    //$('.object_details_background').width($('.hidden_container').width());

    objectsGap = $(window).width()/2.5;

    for(var i=0; i<object_images.length; i++) {
        if (object_images[i]['id']+'_picture'==$('.object_picture_active').attr('id')) {
            $('#'+object_images[i]['id']+'_picture').width(object_images[i]['im_width']).height(object_images[i]['im_height']).show();
            $('#'+object_images[i]['id']+'_picture').css({left: i*objectsGap-object_images[i]['im_width']/2});
        } else {
            $('#'+object_images[i]['id']+'_picture').width(object_images[i]['prev_width']).height(object_images[i]['prev_height']).show();
            $('#'+object_images[i]['id']+'_picture').css({left: i*objectsGap-object_images[i]['prev_width']/2});
        }
        $('#'+object_images[i]['id']+'_label').show().css({left: i*objectsGap-$('#'+object_images[i]['id']+'_label').width()/2});
    }

    $('.labels_container').css({left: $(window).width()/2-parseInt($('.object_picture_active').css('left'))-parseInt($('.object_picture_active').css('width'))/2});
    $('#object_picture_container').css({left: $(window).width()/2-parseInt($('.object_picture_active').css('left'))-parseInt($('.object_picture_active').css('width'))/2});
    $('.object_details_background').width($('.hidden_container').width());
}


function setPreviewSize(divObject) {
    for(var i=0; i<object_images.length; i++) {
            if (object_images[i]['id']+'_picture'==$(divObject).attr('id'))
                $(divObject).width(object_images[i]['prev_width']);
                $(divObject).height(object_images[i]['prev_height']);
        }
}

var object_images = new Array();

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);
