$(function() {
    $( "#tabs" ).tabs();

    $(document).ready( function() {
        $("#slider").slider({elementWidth:105,freezOnMouseOver:true,fluentMovement:true, fluentTickTime: 40, fluentMoveWidth: 1});
        $("div.newest_job_offers ul").slider({freezOnMouseOver:true,fluentMovement:true});
        
        $("#slider .slider-image").mouseenter( function(){
            $(this).children(".gray").hide();
            $(this).children(".color").show();
        }).mouseleave( function(){
            $(this).children(".gray").show();
            $(this).children(".color").hide();
        });

        if( $("#brands").length )
        {
            var current = 0;
            var maxNumber = $("#brands .brand-container").size() - 1;

            setInterval( function() {
                $("#brands #brand-" + current).fadeOut(400,function(){
                    current++;
                    if( current > maxNumber )
                    {
                        current = 0;
                    }
                    $("#brands #brand-" + current).fadeIn();
                });
            }, 5000 );
        }
    });
});

if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
    return -1;
  }
}

var sliderInterval = "";
var targetTiming = 15;
var targetSpeed = 5;
var startTiming = 56;
var startSpeed = 1;
var stepTiming = 2;
var stepSpeed = 0.2;
var currentSpeed = startSpeed;
var currentTiming = startTiming;

var moveSlider = function( direction )
{
    image = $("#sliding-planets img");
    image.css( "left", parseInt( image.css( "left" ) ) + direction*currentSpeed + "px" );

    if( direction > 0 )
    {
        if( parseInt( image.css( "left" ) ) > 0 )
        {
            image.css( "left", "0px" );
        }
    }
    else
    {
        parentWidth = image.parent().parent().innerWidth();
        imageWidth = image.outerWidth();
        if( ( -parseInt( image.css( "left" ) ) ) > (imageWidth - parentWidth) )
        {
            image.css( "left", (parentWidth - imageWidth) + "px" );
        }
    }

    if( currentSpeed < targetSpeed )
    {
        currentSpeed += stepSpeed;
    }

    if( currentTiming > targetTiming )
    {
        currentTiming -= stepTiming;
        clearInterval( sliderInterval );
        sliderInterval = setInterval( "moveSlider("+direction+")", currentTiming );
    }
}

var previousNews = function(number)
{
    var id = parseInt( $("div.gallery-full div.image-large-content.selected").attr("id").substr(20) );
    $("div.gallery-full div.image-large-content.selected").fadeOut().removeClass("selected");
    id--;
    if( id < 0 )
    {
        id = number - 1;
    }
    $("div.gallery-full div#image-large-content-"+id).fadeIn(400, function() {
        height = $("div.gallery-full div.image-large-content.selected").innerHeight();
        $("div.gallery-full div.image-large").animate({height: height+"px"});
        $("div.gallery-full div.image-caption").html( $("div.gallery-full div#image-caption-"+id).html() );
    }
    ).addClass("selected");
}

var nextNews = function(number)
{
    var id = parseInt( $("div.gallery-full div.image-large-content.selected").attr("id").substr(20) );

    $("div.gallery-full div.image-large-content.selected").fadeOut().removeClass("selected");
    id++;
    if( id > number - 1 )
    {
        id = 0;
    }
    $("div.gallery-full div#image-large-content-"+id).fadeIn(400, function() {
        height = $("div.gallery-full div.image-large-content.selected").innerHeight();
        $("div.gallery-full div.image-large").animate({height: height+"px"});
        $("div.gallery-full div.image-caption").html( $("div.gallery-full div#image-caption-"+id).html() );
    }
    ).addClass("selected");
}

var loadImage = function(linkid)
{
    var id = parseInt( $("div.gallery-full div.image-large-content.selected").attr("id").substr(20) );

    $("div.gallery-full div.image-large-content.selected").fadeOut().removeClass("selected");
    id = linkid;
    $("div.gallery-full div#image-large-content-"+id).fadeIn(400, function() {
        height = $("div.gallery-full div.image-large-content.selected").innerHeight();
        $("div.gallery-full div.image-large").animate({height: height+"px"});
        $("div.gallery-full div.image-caption").html( $("div.gallery-full div#image-caption-"+id).html() );
    }
    ).addClass("selected");
}

$(document).ready(function(){
    if( $("#carousel").length )
    {
        $("#carousel").carousel({objects: [
            [ 200,200,200,200 ],
            [ 150,150,0,150 ],
            [ 100,100,100,50 ],
            [ 60,60,220,0 ],
            [ 60,60,310,0 ],
            [ 100,100,400,50 ],
            [ 150,150,450,150 ]
        ], messageBoxPositionTop:0, messageBoxPositionLeft:-120, showMessageTime:400, animationTime: 300});

        $("body").delegate(".frame-body p, .frame-body h3", "click", function() {
            location.href = $("#carousel-message .frame-body .hidden-url").attr("href");
        });
    }

    if( $("#carousel-small").length )
    {
        $("#carousel-small").carousel({objects: [
            [ 172,172,139,106 ],
            [ 111,111,0,96 ],
            [ 63,63,77,49 ],
            [ 36,36,164,30 ],
            [ 36,36,249,30 ],
            [ 63,63,308,49 ],
            [ 111,111,337,96 ]
        ], messageBoxPositionTop:0, messageBoxPositionLeft:-120, showMessageTime:400, animationTime: 300});

        $("body").delegate(".frame-body p, .frame-body h3", "click", function() {
            location.href = $("#carousel-message .frame-body .hidden-url").attr("href");
        });
    }

    if( $("#sliding-planets") )
    {
        image = $("#sliding-planets img");
        parentWidth = image.parent().parent().innerWidth();
        imageWidth = image.outerWidth();

        image.css("left",((parentWidth - imageWidth)/2)+"px");
        
        $("#slideLeft").mouseenter( function() {
            sliderInterval = setInterval( "moveSlider(1)", startTiming );
        } ).mouseleave( function() {
            clearInterval( sliderInterval );
            currentSpeed = startSpeed;
            currentTiming = startTiming;
        } );

        $("#slideRight").mouseenter( function() {
            sliderInterval = setInterval( "moveSlider(-1)", startTiming );
        } ).mouseleave( function() {
            clearInterval( sliderInterval );
            currentSpeed = startSpeed;
            currentTiming = startTiming;
        } );
    }

    if( $("a.show-map").length )
    {
        $("a.show-map").click(function(){
            id = $(this).attr("id").substr(9);

            if( $("#map_" + id).parent().css("display") == "none" )
            {
                $("#map_" + id).parent().slideDown();
                $("h1#header-departments-"+id).removeClass('selected');
                $("div#container-departments-"+id).removeClass('selected');

                if (GBrowserIsCompatible() && map1.isLoaded() ) {
                    var startPoint = new GLatLng(52.4, 19.4515569);
                    map1.setCenter(startPoint);
                    map1.checkResize();
                    map1.setCenter(startPoint);
                    map1.addControl(new GSmallMapControl());
                }

                if (GBrowserIsCompatible() && map2.isLoaded() ) {
                    var startPoint = new GLatLng(52.6, 17.038538);
                    map2.setCenter(startPoint);
                    map2.checkResize();
                    map2.setCenter(startPoint);
                    map2.addControl(new GSmallMapControl());
                }
            }
            else
            {
                $("#map_" + id).parent().slideUp();
                $("h1#header-departments-"+id).addClass('selected');
                $("div#container-departments-"+id).addClass('selected');
            }

            return false;
        });
    }

    if( $("#company_name").length )
    {
        var group_a = [0,3,6,8,9,11];
        var group_b = [1,2,4,5,7,10,12];
        $(".service_type").change( function() {
            var has_a = false;
            var has_b = false;
            var selected = []

            $("input.service_type").each( function() {
                if( $(this).is(":checked") )
                {
                    selected.push( $(this).val() );
                }
            } )
            for( var i in selected )
            {
                if( group_a.indexOf( parseInt(selected[i],10) ) > -1 )
                {
                    has_a = true;
                }

                if( group_b.indexOf( parseInt(selected[i],10) ) > -1 )
                {
                    has_b = true;
                }
            }

            if( has_a && has_b )
            {
                $("#job-position").show();
                $("#job-position #phone_number").show();
                $("#job-position #email").show().css("float","right");
                $("#size-of-purchase").hide();
            }
            else if( has_a )
            {
                $("#job-position").show();
                $("#job-position #phone_number").show();
                $("#job-position #email").hide();
                
                $("#size-of-purchase").show();
                $("#size-of-purchase #phone_number").show();
                $("#size-of-purchase #email").hide();
            }
            else if( has_b )
            {
                $("#job-position").show();
                $("#job-position #phone_number").hide();
                $("#job-position #email").show().css("float","left");

                $("#size-of-purchase").hide();
            }
            else
            {
                $("#job-position").hide();
                $("#size-of-purchase").hide();
            }
            /*if( group_a.indexOf( parseInt($(this).val(),10) ) > -1 )
            {
                $("#job-position").show();
                $("#size-of-purchase").hide();
            }
            else
            {
                $("#job-position").hide();
                $("#size-of-purchase").show();
            }*/
        } )
    };

    $(".service_type").trigger("change");
    
});
