/**
 * This file has been created manually and with love for Swallow Studio.
 * Author of this file is Pavel Ptacek, copyright is hereby transferred to
 * Swallow Studio spol. s.r.o. in Czech Republic.
 * Swallow Studio might work with this file without any limitations.
 * You might not copy, re-use or otherwise publish contents of this file,
 * or its parts.
 *
 * @author Pavel Ptacek <birdie at animalgroup dot cz>
 * @copyright Swallow Studio spol. s r.o.
 */

/**
 * Works class -> handles scrolling
 */
var works = {
    // holds the timer
    timer : null,

    // maximum padding; 0 = disabled
    max : 0,

    /**
     * Binds the scroller
     */
    bind : function() {
        // Bind scrolling events
        $('#arrow-up').hover(
            function() {works.timer = setInterval(works.scrollUp, 15);},
            function() {clearInterval(works.timer)}
        );
        $('#arrow-down').hover(
            function() {works.timer = setInterval(works.scrollDown, 15);},
            function() {clearInterval(works.timer)}
        );
        $("#other-works").mousewheel(works.mouseScroll);

        var height = parseInt($('#other-works-cover').height());
        var one = parseInt($('#other-works li').height());
        var total = one * $('#other-works li').length;

        if(total <= height) {
            return;
        }

        works.max = height - total;
        
        // Check the param
        var split = window.location.href.split('#');
        if(split[1]) {
            $('#other-works').css('margin-top', split[1] + 'px');
            $('#other-works a').each(function() {
                var href = $(this).attr('href');
                var sp = href.split('#');
                $(this).attr('href', sp[0] + '#' + split[1]);
            });
        }
    },

    /**
     * Scrollup
     */
    scrollUp : function() {
        var curr = parseInt($('#other-works').css('margin-top'));
        if(curr >= 0) {
            return;
        }

        curr += 3;
        $('#other-works').css('margin-top', curr + 'px');
        $('#other-works a').each(function() {
            var href = $(this).attr('href');
            var split = href.split('#');
            $(this).attr('href', split[0] + '#' + curr);
        });
    },

    /**
     * Scrolldown
     */
    scrollDown : function() {
        var curr = parseInt($('#other-works').css('margin-top'));
        if(curr <= works.max) {
            return;
        }

        curr -= 3;
        $('#other-works').css('margin-top', curr + 'px');
        $('#other-works a').each(function() {
            var href = $(this).attr('href');
            var split = href.split('#');
            $(this).attr('href', split[0] + '#' + curr);
        });
    },
    
    /**
     * Handles mouse scroll
     */
    mouseScroll : function(event, delta) {
        var original = parseInt($('#other-works').css('margin-top'));
        var curr = original + (delta * 30);
        
        // Correct mousewheel scroll
        if(curr >= 0 && original < 0) {
            curr = 0;
        }
        if(curr <= works.max && original > works.max) {
            curr = works.max;
        }
        
        if(curr < works.max || curr > 0) {
            return;
        }
        
        $('#other-works').css('margin-top', curr + 'px');
        $('#other-works a').each(function() {
            var href = $(this).attr('href');
            var split = href.split('#');
            $(this).attr('href', split[0] + '#' + curr);
        });

        event.preventDefault();
    }
};

/**
 * Works img class -> handles the images within works
 */
var worksImg = {
    current : 0,
    imgs : [],
    offsetX : 0,
    
    /**
     * Inits the class
     */
    init : function() {
        if(!$('body').hasClass('works')) {
            return;
        }
        
        // Walk through and populate the imgs array
        $('.left .second img, .left .second > .flashContent').each(function() {
            worksImg.imgs.push($(this));
            $(this).hide();
        });
        
        // Store offsets
        worksImg.offsetX = $('.left .second').offset().left;
        
        // Bind events
        $('body.works .carousel a').click(worksImg.click);
        
        // Show first image
        worksImg.imgs[0].show();
        $('body.works .carousel a[rel="0"]').addClass('active');
        
        // And resize content
//        var h = worksImg.imgs[0].height();
//        $('.left .second').animate({height: (h+20) + 'px'}, 'slow');        
    },
    
    /**
     * Used for resizing content
     */
    resizeContent : function() {
        if(!$('body').hasClass('works')) {
            return;
        }
        
        // Get the biggest height
        var height = 0;
        for(i in worksImg.imgs) {
            if(worksImg.imgs[i].height() > height) {
                height = worksImg.imgs[i].height();
            }
        }
        
        // Increment for the carousel
        height += 20;
        
        // Set it to correct elements
        $('.left .second').css('height', height + 'px');
        
        // And set the height of currently displayed image
        height = worksImg.imgs[worksImg.current].height();
        $('#works-prev').css('height', height + 'px');
        $('#works-next').css('height', height + 'px');
    },
    
    /**
     * Click to next image
     */
    next : function(e) {
        e.preventDefault();
        
        // Get next one
        var nextIndex = worksImg.current + 1;
        if(nextIndex >= worksImg.imgs.length) {
            nextIndex = 0;
        }
        
        var current = worksImg.imgs[worksImg.current];
        var next = worksImg.imgs[nextIndex];

        // Change index & init fading
        current.css('z-index', '1');
        next.css('z-index', '2');

        // Resize!
        var h = worksImg.imgs[nextIndex].height();
        $('#works-prev').animate({height: h + 'px'}, 'slow');
        $('#works-next').animate({height: h + 'px'}, 'slow');
        $('.left .second').animate({height: h + 'px'}, 'slow');

        // Fade!
        next.fadeIn('slow', function() {
            current.hide();
        });
        current.fadeOut('fast');
        worksImg.current = nextIndex;
    },
    
    /**
     * Click to previous image
     */
    prev : function(e) {
        e.preventDefault();
        
        // Get next one
        var prevIndex = worksImg.current + 1;
        if(prevIndex >= worksImg.imgs.length) {
            prevIndex = 0;
        }
        
        var current = worksImg.imgs[worksImg.current];
        var prev = worksImg.imgs[prevIndex];

        // Change index & init fading
        current.css('z-index', '1');
        prev.css('z-index', '2');
        
        // Resize!
        var h = worksImg.imgs[prevIndex].height();
        $('#works-prev').animate({height: h + 'px'}, 'slow');
        $('#works-next').animate({height: h + 'px'}, 'slow');
        $('.left .second').animate({height: h + 'px'}, 'slow');

        // Fade!
        prev.fadeIn('slow', function() {
            current.hide();
        });
        current.fadeOut('fast');
        worksImg.current = prevIndex;
    },
    
    /**
     * Called on mouse move
     */
    move : function(e) {
        var x = e.pageX - worksImg.offsetX;
        
        if(x <= 100 && $('#works-prev').not(':visible')) {
            $('#works-prev').show();
        }
        if(x > 100 && $('#works-prev').is(':visible')) {
            $('#works-prev').hide();
        }
        
        if(x >= 350 && $('#works-next').not(':visible')) {
            $('#works-next').show();
        }
        if(x < 350 && $('#works-next').is(':visible')) {
            $('#works-next').hide();
        }
    },
    
    /**
     * Called on mouse out
     */
    out : function() {
        $('#works-prev').hide();
        $('#works-next').hide();
    },
    
    /**
     * Ultimate clicker!
     */
    click : function(e) {
        e.preventDefault();
        
        // Get the one we are supposed to set
        var nextIndex = $(this).attr('rel');
        if(nextIndex == worksImg.current) {
            return;
        }
        
        // Get elements
        var current = worksImg.imgs[worksImg.current];
        var next = worksImg.imgs[nextIndex];

        // Change index & init fading
        current.css('z-index', '1');
        next.css('z-index', '2');

        // Resize!
        var h = worksImg.imgs[nextIndex].height();
        $('.left .second').animate({height: (h+20) + 'px'}, 'slow');
        
        // Classes!
        $('.left .carousel a').removeClass('active');
        $(this).addClass('active');

        // Fade!
        next.fadeIn('slow', function() {
            current.hide();
        });
        current.fadeOut('fast');
        worksImg.current = nextIndex;        
    }
};

/**
 * Homepage promo
 */
var promo = {
    /**
     * Last position of the slider
     */
    last : 0,
    
    /**
     * The image counter used for appending new images
     */
    imageCounter : 0,
    
    /**
     * The global offset
     */
    globalOffset : 0,
    
    // center of the screen
    middle : 0,
    
    // current section
    current : 0,
    
    animating : false,
    timer : null,
    
    /**
     * Image class which handles the individual moving images
     */
    image : function(elm, rate, center, totalLeft, travelDistance) {
        this.elm = elm;
        this.rate = rate;
        this.center = center;
        this.totalLeft = totalLeft;
        this.travelDistance = travelDistance;
    },
    
    /**
     * Array of images
     */
    images : [],
    
    /**
     * Appends new image into array
     */
    append : function(sect, img, left, top, rate) {
        // If there's no 'center' then we just append the img & forget about it
        if(!rate) {
            $('#sect-' + (sect+1)).append('<img src="/images/website/promo/' + img + '" style="top: ' + top + 'px; left: ' + left + 'px;" />')
            return;
        }
        
        // If the section is the first one, then we use different method
        if(sect == 0) {
            $('#sect-' + (sect+1)).append('<img src="/images/website/promo/' + img + '" id="sect-img-' + promo.imageCounter + '" style="top: ' + top + 'px; left: ' + left + 'px;" />')
            promo.images[sect].push(new promo.image($('#sect-img-' + promo.imageCounter), rate, left));
            promo.imageCounter++;
            return;
        }
        
        // Calculate the space remaining on the right
        var remains = 900 - left;
        var offset = remains * rate;
        var totalLeft = left + offset;
        $('#sect-' + (sect+1)).append('<img src="/images/website/promo/' + img + '" id="sect-img-' + promo.imageCounter + '" style="top: ' + top + 'px; left: ' + totalLeft + 'px;" />')
        promo.images[sect].push(new promo.image($('#sect-img-' + promo.imageCounter), rate, left, totalLeft, offset));
        promo.imageCounter++;
    },
    
    /**
     * Move to the next position
     */
    next : function(e) {
        e.preventDefault();
        
        if(promo.current > 3) {
            return;
        }
        
        if(promo.animating != false) {
            return;
        }
        
        if(promo.current == 3) {
            $('#slider-next').fadeOut('slow');
        }
        if(promo.current == 0) {
            $('#slider-prev').fadeIn('slow').css('display', 'block');
        }
        
        promo.animating = true;
        promo.timer = setInterval(promo.nextMove, 10);
    },

    /* actually moves the slider */
    nextMove : function() {
        if(promo.current == 0 && promo.last > 650) {
            clearInterval(promo.timer);
            promo.animating = false;
            promo.current = 1;
            return;
        }
        
        if(promo.current == 1 && promo.last > 1300) {
            clearInterval(promo.timer);
            promo.animating = false;
            promo.current = 2;
            return;
        }
        
        if(promo.current == 2 && promo.last > 1950) {
            clearInterval(promo.timer);
            promo.animating = false;
            promo.current = 3;
            return;
        }
        
        if(promo.current == 3 && promo.last >= 2600) {
            clearInterval(promo.timer);
            promo.animating = false;
            promo.current = 4;
            return;
        }
        

        $('#slider').slider('value', promo.last + 6);
    },
    
    /**
     * Move to the previous position
     */
    prev : function(e) {
        e.preventDefault();
        
        if(promo.current <= 0) {
            return;
        }
        if(promo.animating != false) {
            return;
        }
        if(promo.current == 1) {
            $('#slider-prev').fadeOut('slow');
        }
        if(promo.current == 4) {
            $('#slider-next').fadeIn('slow').css('display', 'block');
        }
        
        promo.animating = true;
        promo.timer = setInterval(promo.prevMove, 10);
    },

    /* actually moves the slider */
    prevMove : function() {
        if(promo.current == 4 && promo.last <= 1950) {
            clearInterval(promo.timer);
            promo.animating = false;
            promo.current = 3;
            return;
        }
        
        if(promo.current == 3 && promo.last <= 1300) {
            clearInterval(promo.timer);
            promo.animating = false;
            promo.current = 2;
            return;
        }
        
        if(promo.current == 2 && promo.last <= 650) {
            clearInterval(promo.timer);
            promo.animating = false;
            promo.current = 1;
            return;
        } 
        
        if(promo.current == 1 && promo.last <= 0) {
            clearInterval(promo.timer);
            promo.animating = false;
            promo.current = 0;
            return;
        }

        $('#slider').slider('value', promo.last - 6);
    },
    
    /**
     * Bind the events
     */
    init : function() {
        $('#slider').slider({
            orientation: "horizontal",
            range: "min",
            max: 2600,
            value: 0,
            slide: promo.move,
            change: promo.move
        }).hide();
        $('#slider-next').click(promo.next);
        $('#slider-prev').click(promo.prev);
        
        // Set the width of promo box & the offset & margins of the slider
        $('#promo').width($('body').width());
        promo.globalOffset = $('body').width() + 100;
        var margin = ($('body').width() - $('#slider').width()) / 2 - 50;
        $('#slider').css('margin-left', margin + 'px');
        
        // Calculate the middle
        var w = $('body').width() - 1080;
        if(w > 0) {
            promo.middle = (w/2);
        }
        
        // The promo (last section) actions
        $('#promo #sect-5 .calendar').datepicker({
            firstDay: 1,
            dateFormat: 'dd. MM yy',
            navigationAsDateFormat: true,
            prevText: 'MM',
            nextText: 'MM',
            currentText: 'MM',
            onSelect: function(dateText, inst) {$('#datepicker-value').val(dateText);}
        });
        $('#promo #sect-5 #form-link').click(function() {$('#sect-5 .calendar').show('slow');$('#sect-5 form').show('slow');$('#promo #sect-5 .calendar .ui-datepicker-header').width($('#promo #sect-5 .calendar table').width());return false;});
        $('#promo #sect-5 input').focus(function() {$(this).parent().find('span:visible').hide('slow');});
        $('#promo #sect-5 input').blur(function() {if($(this).val().length < 1) {$(this).parent().find('span.title').show('slow');}});

        // Prepare the array
        promo.images.push(new Array());
        promo.images.push(new Array());
        promo.images.push(new Array());
        promo.images.push(new Array());
        promo.images.push(new Array());
        
        // Append first section
        promo.append(0, '01-codes.png', 160, 80, 0.2);
        promo.append(0, '01-vodochody.png', 340, 60, 1.2);
        promo.append(0, '01-monitor.png', 240, 120, 0.2);
        
        // Append second section
        promo.append(1, 'plus.png', 0, 220, 0.2);
        promo.append(1, '02-youtube.png', 380, 140, 1.5);
        promo.append(1, '02-linkedin.png', 380, 320, 2);
        promo.append(1, '02-at.png', 280, 240, 0.4);
        promo.append(1, '02-facebook.png', 180, 300, 8);
        promo.append(1, '02-twitter.png', 220, 120, 6);

        // Append third section
        promo.append(2, 'plus.png', 0, 220, 0.1);
        promo.append(2, '03-ipad.png', 320, 120, 2);
        promo.append(2, '03-iphone.png', 220, 160, 0.4);
        
        // Third section apps -> X axis - from 40px to 220px; Y axis - from 120px to 380px
        promo.append(2, '03-app-01.png', 140, 120,   1);
        promo.append(2, '03-app-02.png', 143.5, 320, 23);
        promo.append(2, '03-app-03.png', 190, 150,   3);
        promo.append(2, '03-app-04.png', 250, 150,  4);
        promo.append(2, '03-app-05.png', 320, 380,  5);
        promo.append(2, '03-app-06.png', 300, 170,  6);
        promo.append(2, '03-app-07.png', 170, 240,   7);
        promo.append(2, '03-app-08.png', 290, 350,  8);
        promo.append(2, '03-app-09.png', 320, 120,  9);
        promo.append(2, '03-app-10.png', 210, 265,  10);
        promo.append(2, '03-app-11.png', 110, 190,   11);
        promo.append(2, '03-app-12.png', 318, 218,  12);
        promo.append(2, '03-app-13.png', 140, 380,   13);
        promo.append(2, '03-app-14.png', 250, 100,  14);
        promo.append(2, '03-app-15.png', 212, 365,  15);
        promo.append(2, '03-app-16.png', 340, 280,  16);
        promo.append(2, '03-app-17.png', 364, 354,  17);
        promo.append(2, '03-app-18.png', 220, 200,  18);
        promo.append(2, '03-app-19.png', 173, 345,   19);
        promo.append(2, '03-app-20.png', 367, 178,  20);
        promo.append(2, '03-app-21.png', 314, 390,  21);
        promo.append(2, '03-app-22.png', 126, 174,   22);
        
        // Append fourth section
        promo.append(3, 'plus.png', 0, 220, 0.2);
//        promo.append(3, '04-cd.png',  340, 240, 1);
        promo.append(3, '04-bag.png', 200, 0,  4);
        promo.append(3, '04-cd-cd.png',  280, 240, 1.08);
        promo.append(3, '04-cd-obal.png',  340, 240, 1);
        promo.append(3, '04-car.png', 100, 100, 0.5);
        promo.append(3, '04-bc2.png', 60,  310, 2);
        promo.append(3, '04-bc1.png', 60,  270, 7);
        
        // Append fifth section (the calendar thing)
        promo.append(4, 'equals.png', 0, 220, 0.4);

        // Position the boxes
        $('#sect-1').css('left', promo.middle + 'px');
        $('#sect-2').css('left', promo.globalOffset + 'px');
        $('#sect-3').css('left', promo.globalOffset + 'px');
        $('#sect-4').css('left', promo.globalOffset + 'px');
        $('#sect-5').css('left', promo.globalOffset + 'px');
        
        // Z-indexes!
        $('#sect-1').css('z-index', '54');
        $('#sect-2').css('z-index', '53');
        $('#sect-3').css('z-index', '52');
        $('#sect-4').css('z-index', '51');
        $('#sect-5').css('z-index', '50');
        
        // temporary
//        $('#slider').slider('value', 2600);
    },
    
    /**
     * Moves the canvas
     */
    move : function(event, ui) {
        // Skip?
        if(promo.last == ui.value) {
            return;
        }
        
        var newLeft = 0, current = null, frame = 0;
        
        // Move images within first section --> always
        if(ui.value < 200) {
            for(i in promo.images[0]) {
                current = promo.images[0][i];
                newLeft = current.center - ui.value * current.rate;
                current.elm.css('left', newLeft + 'px');
            }
        }
        newLeft = 0 - $.easing.easeInCubic(null, ui.value, promo.middle*-1, 1000, 200);
        $('#sect-1').css('left', newLeft + 'px');
        
        // Second section -> flying in
        if(ui.value <= 650) {
            frame = ui.value;
            newLeft = promo.globalOffset - $.easing.easeOutCubic(null, frame, promo.middle*-1, promo.globalOffset, 650) + 100; 
            $('#sect-2').css('left', newLeft + 'px');
            
            if(frame < 650) {
                for(i in promo.images[1]) {
                    current = promo.images[1][i];
                    newLeft = current.totalLeft - $.easing.easeOutCubic(null, frame, 0, current.travelDistance, 650);
                    current.elm.css('left', newLeft + 'px');
                }
            }
        }
        if(ui.value > 650) {
            frame = ui.value - 650;
            newLeft = 100 - $.easing.easeInCubic(null, frame, promo.middle*-1, 1500, 200);
            $('#sect-2').css('left', newLeft + 'px');

            if(frame < 200) { 
                for(i in promo.images[1]) {
                    current = promo.images[1][i];
                    newLeft = current.center - $.easing.easeInCubic(null, frame, 0, current.travelDistance, 200);
                    current.elm.css('left', newLeft + 'px');
                }
            } 
        }

        // Third section -> flying in
        if(ui.value <= 1300) {
            frame = ui.value - 650;
            newLeft = promo.globalOffset - $.easing.easeOutCubic(null, frame, promo.middle*-1, promo.globalOffset, 650) + 100;
            $('#sect-3').css('left', newLeft + 'px');
            
            if(frame < 650) {
                for(i in promo.images[2]) {
                    current = promo.images[2][i];
                    newLeft = current.totalLeft - $.easing.easeOutCubic(null, frame, 0, current.travelDistance, 650);
                    current.elm.css('left', newLeft + 'px');
                }
            }
        }
        if(ui.value > 1300) {
            frame = ui.value - 1300;
            newLeft = 100 - $.easing.easeInCubic(null, frame, promo.middle*-1, 1000, 200);
            $('#sect-3').css('left', newLeft + 'px');
            
            if(frame < 200) {
                for(i in promo.images[2]) {
                    current = promo.images[2][i];
                    newLeft = current.center - $.easing.easeInCubic(null, frame, 0, current.travelDistance, 200);
                    current.elm.css('left', newLeft + 'px');
                }
            }
        }
        
        // Fourth section -> flying in
        if(ui.value <= 1950) {
            frame = ui.value - 1300;
            newLeft = promo.globalOffset - $.easing.easeOutCubic(null, frame, promo.middle*-1, promo.globalOffset, 650) + 100;
            $('#sect-4').css('left', newLeft + 'px');
            
            if(frame < 650) {
                for(i in promo.images[3]) {
                    current = promo.images[3][i];
                    newLeft = current.totalLeft - $.easing.easeOutCubic(null, frame, 0, current.travelDistance, 650);
                    current.elm.css('left', newLeft + 'px');
                }
            }
        }
        if(ui.value > 1950) {
            frame = ui.value - 1950;
            newLeft = 100 - $.easing.easeInCubic(null, frame, promo.middle*-1, 1000, 200);
            $('#sect-4').css('left', newLeft + 'px');
            
            if(frame < 200) {
                for(i in promo.images[3]) {
                    current = promo.images[3][i];
                    newLeft = current.center - $.easing.easeInCubic(null, frame, 0, current.travelDistance, 200);
                    current.elm.css('left', newLeft + 'px');
                }
            }
        }
        
        // Fifth section -> flying in
        if(ui.value <= 2600) {
            frame = ui.value - 1950;
            newLeft = promo.globalOffset - $.easing.easeOutCubic(null, frame, promo.middle*-1, promo.globalOffset, 650) + 100;
            $('#sect-5').css('left', newLeft + 'px');
            
            for(i in promo.images[4]) {
                current = promo.images[4][i];
                newLeft = current.totalLeft - $.easing.easeOutCubic(null, frame, 0, current.travelDistance, 650);
                current.elm.css('left', newLeft + 'px');
            }
        }        
        
        // And store the last position
        promo.last = ui.value;
    }
};


/**
 * Career func
 */
var career = career || {};
career.openPos = null;
career.showPos = function() {
    if(career.openPos != null) {
        $(career.openPos).hide('slow');
    }

    var id = '#' + $(this).attr('data-open');

    if(career.openPos == id) {
        career.openPos = null;
        return false;
    }
    career.openPos = id;
    $(id).show('slow');
    return false;
}

/**
 * Client freakin carousel
 */
var clca = {
    current : 1,
    bind : function() {
        $('#clients-carousel-next').click(clca.next);
    },
    
    next : function(e) {
        e.preventDefault();
        
        if(clca.current == 1) {
            $('body.homepage .right .clients .first').animate({left: '-500px'}, 800, 'easeInOutCubic');
            $('body.homepage .right .clients .second').animate({left: '0px'}, 800, 'easeInOutCubic');
            $('body.homepage .right .clients .third').css('left', '500px');
            $('body.homepage .right .clients .fourth').css('left', '500px');
            $('body.homepage .right .clients .fifth').css('left', '500px');
            $('body.homepage .right .clients .sixth').css('left', '500px');
            clca.current = 2;
            return false;
        }
        if(clca.current == 2) {
            $('body.homepage .right .clients .first').css('left', '500px');
            $('body.homepage .right .clients .second').animate({left: '-500px'}, 800, 'easeInOutCubic');
            $('body.homepage .right .clients .third').animate({left: '0px'}, 800, 'easeInOutCubic');
            $('body.homepage .right .clients .fourth').css('left', '500px');
            $('body.homepage .right .clients .fifth').css('left', '500px');
            $('body.homepage .right .clients .sixth').css('left', '500px');
            clca.current = 3;
            return false;
        }
        if(clca.current == 3) {
            $('body.homepage .right .clients .first').css('left', '500px');
            $('body.homepage .right .clients .second').css('left', '500px');
            $('body.homepage .right .clients .third').animate({left: '-500px'}, 800, 'easeInOutCubic');
            $('body.homepage .right .clients .fourth').animate({left: '0px'}, 800, 'easeInOutCubic');
            $('body.homepage .right .clients .fifth').css('left', '500px');
            $('body.homepage .right .clients .sixth').css('left', '500px');
            clca.current = 4;
            return false;
        }
        if(clca.current == 4) {
            $('body.homepage .right .clients .first').css('left', '500px');
            $('body.homepage .right .clients .second').css('left', '500px');
            $('body.homepage .right .clients .third').css('left', '500px');
            $('body.homepage .right .clients .fourth').animate({left: '-500px'}, 800, 'easeInOutCubic');
            $('body.homepage .right .clients .fifth').animate({left: '0px'}, 800, 'easeInOutCubic');
            $('body.homepage .right .clients .sixth').css('left', '500px');
            clca.current = 5;
            return false;
        }
        if(clca.current == 5) {
            $('body.homepage .right .clients .first').css('left', '500px');
            $('body.homepage .right .clients .second').css('left', '500px');
            $('body.homepage .right .clients .third').css('left', '500px');
            $('body.homepage .right .clients .fourth').css('left', '500px');
            $('body.homepage .right .clients .fifth').animate({left: '-500px'}, 800, 'easeInOutCubic');
            $('body.homepage .right .clients .sixth').animate({left: '0px'}, 800, 'easeInOutCubic');
            clca.current = 6;
            return false;
        }
        if(clca.current == 6) {
            $('body.homepage .right .clients .second').css('left', '500px');
            $('body.homepage .right .clients .third').css('left', '500px');
            $('body.homepage .right .clients .fourth').css('left', '500px');
            $('body.homepage .right .clients .fifth').css('left', '500px');
            $('body.homepage .right .clients .sixth').animate({left: '-500px'}, 800, 'easeInOutCubic');
            $('body.homepage .right .clients .first').animate({left: '0px'}, 800, 'easeInOutCubic');
            clca.current = 1;
            return false;
        }
    }
};

/**
 * validator
 */
function validMail(mail) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = mail;
   if(reg.test(address) == false) {
      return false;
   }
   return true;
}
function checkFormContact() {
    if($('input[name="question"]:checked').length < 1) {
        $('.error[data-field="question"]').show('slow');
    }
    else {
        $('.error[data-field="question"]').hide();
    }
    
    if($('input[name="name"]').val().length < 1) {
        $('.error[data-field="name"]').show('slow');
    }
    else {
        $('.error[data-field="name"]').hide();
    }
    
    if(!validMail($('input[name="email"]').val())) {
        $('.error[data-field="email"]').show('slow');
    }
    else {
        $('.error[data-field="email"]').hide();
    }
    
    if($('input[name="phone"]').val().length < 1) {
        $('.error[data-field="phone"]').show('slow');
    }
    else {
        $('.error[data-field="phone"]').hide();
    }
    
    if($('.error:visible').length > 0) {
        return false;
    }
    
    return true;
}

/**
 * Homepage form validator
 */
function checkFormHomepage() {
    if($('input[name="name"]').val().length < 1) {
        $('.error[data-field="name"]').show('slow');
    }
    
    if($('input[name="email"]').val().length < 1) {
        $('.error[data-field="email"]').show('slow');
    }
    
    if($('input[name="phone"]').val().length < 1) {
        $('.error[data-field="phone"]').show('slow');
    }
    
    if($('input[name="company"]').val().length < 1) {
        $('.error[data-field="company"]').show('slow');
    }
    
    if($('.error:visible').length > 0) {
        $('#sect-5 span.title').hide();
        return false;
    }
    
    return true;
}

/**
 * Function for validating the career
 */
function checkFormCareer() {
    var id = $(this).attr('id');
    if($('#' + id + ' input.f').val().length < 1) {
        $('#' + id + ' .error.f').show('slow');
    }
    if(!validMail($('#' + id + ' input.e').val())) {
        $('#' + id + ' .error.e').show('slow');
    }
    
    if($('.error:visible').length > 0) {
        return false;
    }
    
    return true;
}

/**
 * Init the website
 */
$(document).ready(function() {
    if($('body').hasClass('works')) {
        works.bind();
        worksImg.init();
    }

    if($('body').hasClass('career')) {
        $('a.career-link').click(career.showPos);
        $('form:not(.news)').submit(checkFormCareer);
        
        // Open the position based on hash
        var split = document.location.href.split('#ref-');
        if(split[1]) {
            $('a.career-link[data-open="' + split[1] + '"]').click();
        }
    }
    
    if($('body').hasClass('homepage')) {
        promo.init();
        clca.bind();
    }
    
    // align the languages
    var center = $('body').width() / 2;
    center -= 400;
    $('#lang').css('right', center + 'px');
    
    // Form sent?
    if($('#form-centerer.hidelong').length > 0) {
        setTimeout(function() {
            $('#form-centerer').fadeOut('slow');
        }, 15000);
    }
    else if($('#form-centerer').length > 0) {
        setTimeout(function() {
            $('#form-centerer').fadeOut('slow');
        }, 3000);
    }
    
    // contact form
    if($('body.contact').length > 0 && !$('body.contact.career').length) {
        $('body.contact #contform').submit(checkFormContact);
    }
    
    // Homepage form
    if($('body.homepage form').length > 0) {
        $('body.homepage #sect-5 form').submit(checkFormHomepage);
    }
    
    // Map in contact
    if($('#map').length > 0) {
        $(function() {
            /*
            http://maps.google.com/maps?q=K%C5%99i%C5%BE%C3%ADkova+34,+Praha-Praha+8,+%C4%8Cesk%C3%A1+republika&hl=en&ie=UTF8&ll=50.092049,14.450616&spn=0.003717,0.009645&sll=37.0625,-95.677068&sspn=37.546691,79.013672&z=17
             *
             **/

                var myOptions = {
                        zoom: 14,
                        center: new google.maps.LatLng(50.092049,14.450616),
                        mapTypeId: google.maps.MapTypeId.ROADMAP,
                        streetViewControl: true
                }
                var map = new google.maps.Map(document.getElementById("map"), myOptions);
                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(50.092049,14.450616),
                    map: map,
                    icon: '/images/website/map.png'
                });
                
                // Now let's see how many website will copycat the code bellow :-)
                // http://maps.google.com/maps?q=k%C5%99i%C5%BE%C3%ADkova+34&hl=en&ie=UTF8&ll=50.092451,14.450834&spn=0,0.008256&sll=37.0625,-95.677068&sspn=62.61328,135.263672&radius=15000&z=18&layer=c&cbll=50.092451,14.450834&panoid=-0SYeWPCD3x2fcvoim6www&cbp=12,212.7,,0,-10.12
                var fenway = new google.maps.LatLng(50.092451,14.450834);
                var panorama = map.getStreetView();
                panorama.setPosition(fenway);
                panorama.setPov({
                heading: 173.7,
                    zoom:0.68,
                    pitch:20
                });
                panorama.setVisible(true);
        });    
    }
    
    // Submitter b'coz of canvas
    $('a.button').click(function(e) {
        e.preventDefault();
        return $('form#' + $(this).attr('rel')).submit();
    });
});
