var App = {
    rotate_time: 3000
};

App.Accordion = function(el){
    var sections = $(el).children('div');
    sections.first().addClass('active');
    sections.click(function(event){
        event.preventDefault();
        if($(event.currentTarget).hasClass('active'))
            return false;
        var prev = sections.filter('.active'),
            curr = $(event.currentTarget);
        prev.children('div').slideUp(400,function(){prev.removeClass('active');});
        curr.addClass('active').children('div').slideDown('slow');
    });
};

App.Tabs = function(el){
    var buttons = $('.tabs .left a'),
        content = $('.tabs .right .content .tab');
    $(content).first().addClass('active'); 
    $(buttons).first().addClass('active'); 
    buttons.click(function(event){
        event.preventDefault();
        var prev_content = content.filter('.active'),
            curr_content = content.filter('#tab_'+event.currentTarget.rel),
            prev_button = buttons.filter('.active'),
            curr_button = $(event.currentTarget);
        prev_content.fadeOut(400,function(){
            prev_content.removeClass('active');
            curr_content.fadeIn(400).addClass('active');
        });
        curr_button.animate({'color':'#ffffff'},400,'',function(){curr_button.addClass('active');});
        prev_button.animate({'color':'#d0e389'}).removeClass('active');
    });
};

App.Form = function(el){
    $(el).validate({
        errorClass: 'invalid',
        validClass: 'valid',
        success: 'valid',
        messages: {
            name: " ",
            company: " ",
            title: " ",
            email: {
                required: " ",
                email: " "
            }
        }
    });
};

App.LightBox = function(el){
    var vzaar = '<div class="vzaar_media_player"><object id="video" width="462" height="350" type="application/x-shockwave-flash" data="http://view.vzaar.com/658222.flashplayer"><param name="movie" value="http://view.vzaar.com/658222.flashplayer">	<param name="allowScriptAccess" value="always"><param name="allowFullScreen" value="true"><param name="wmode" value="transparent"><param name="flashvars" value="endLink=www.kabel-x.eu&brandText=Kabel-X&brandLink=www.kabel-x.eu&endText=Kabel-X%0A%0AKabel-X+delivers+a+cutting+edge+solution+for+cost-effective+extraction+of+out-dated+copper+cables+from+their+outer-jacket+without+having+to+dig+the+cables+out+of+the+ground+first.+"><embed src="http://view.vzaar.com/658222.flashplayer" type="application/x-shockwave-flash" wmode="transparent" width="462" height="350" allowScriptAccess="always" allowFullScreen="true" flashvars="endLink=www.kabel-x.eu&brandText=Kabel-X&brandLink=www.kabel-x.eu&endText=Kabel-X%0A%0AKabel-X+delivers+a+cutting+edge+solution+for+cost-effective+extraction+of+out-dated+copper+cables+from+their+outer-jacket+without+having+to+dig+the+cables+out+of+the+ground+first.+"></embed><video width="462" height="350" src="http://view.vzaar.com/658222.mobile" poster="http://view.vzaar.com/658222.image" controls onclick="this.play();"></video></object></div>';
    $(el).colorbox({html: vzaar, transition:'fade', speed:500, scrolling: false})
};

App.Menu = function(el){
    $('div#header div.b ul li a').each(function(i,el){
        if(el.href == window.location.href){
            $(el).addClass('active');
            $('<div class="arrow"></div>').appendTo(el);
        }else{
            $(el).hover(function(event){
                $(event.target).animate({'color':'#d1e188'},400);
            },function(event){
                $(event.target).animate({'color':'#74b643'},400);
            });
        }
    });
    $('div#header div.a ul li a').each(function(i,el){
        if(el.href == window.location.href){
            $(el).parent().addClass('active');
            $('<div class="arrow"></div>').appendTo(el);
        }else{
            $(el).hover(function(event){
                $(event.target).animate({'background-color':'#e6e7e9'},400)
            },function(event){
                $(event.target).animate({'background-color':'#d2d3d5'},400);
            });
        }
    });
};

App.Rotate = function(el){
    var texts = el.children('div.text'),
        href = $('div#banner a#banner-link'),
        i = 0;
    href.attr('href',el.children(':nth-child('+(i)+')').attr('rel'));
    setInterval(function(){
        el.animate({'margin-top': -(i*254)},{duration: 800});
        href.attr('href',el.children(':nth-child('+(i+1)+')').attr('rel'));
        i = (i>=texts.length-1 ? 0 : i+1);
    },App.rotate_time);
};

App.Slideshow = function(el){
    var panes = $('div.slideshow div.panes'),
        links = $('div.slideshow div.nav a'),
        descs = $('div.slideshow div.description div');
    links.click(function(e){
        e.preventDefault();
        links.filter('.active').removeClass('active');
        $(e.target).addClass('active');
        panes.animate({'margin-top': -((e.target.innerHTML-1)*225)},{duration: 800});
        descs.filter('.active').slideUp().removeClass('active');
        descs.filter('.n'+e.target.innerHTML).slideDown().addClass('active');
    });
};

$(document).ready(function(){
    var accordion = $('div.accordion'),
        tabs = $('div.tabs'),
        form = document.forms[0],
        lightbox = $('a.light-box'),
        rotate = $('div.rotate'),
        slideshow = $('div.slideshow');
    accordion.length && new App.Accordion(accordion[0]);
    tabs.length && new App.Tabs(tabs);
    form && new App.Form(document.forms[0]);
    lightbox.length && new App.LightBox(lightbox);
    rotate.length && new App.Rotate(rotate);
    slideshow.length && new App.Slideshow(slideshow);
    new App.Menu($('div#header div.b ul'));
});
