/**
* @author max
*/
$(function() {
    
    /****  first-level menu  ****/
    // activate (highlight) a menu item (takes care of first and last special cases)
    function activate (elem) {
        var parent = $(elem).parent();
            if( $(parent).hasClass("first") && !$(parent).hasClass("active") ){
                $(elem).removeClass("active_next").addClass("active_first");
                
                if (!$(parent).next().hasClass("active")) { // if next item is currently active, then make it 'active_next'
                    $(parent).next().find('a').addClass("active_next");
                }
            }
            else if( $(parent).hasClass("last") && !$(parent).hasClass("active") ) {
                $(elem).removeClass("active_next").addClass("active_last");
            }
            else if( !$(parent).hasClass("active") ) {
                $(elem).removeClass("active_next").addClass("active_mid");
                
                if( !$(parent).next().hasClass("active") ) { // if next is active, then make it 'active_next'
                    $(parent).next().find('a').addClass("active_next");
                }
            }
        };

    // deactivate (remove highlight) a menu item
    function deactivate (elem) {
        var parent = $(elem).parent();
            if( $(parent).hasClass("first") && !$(parent).hasClass("active") ){
                $(elem).removeClass("active_first");
                
                $(parent).next().find('a').removeClass("active_next");
            }
            else if( $(parent).hasClass("last") && !$(parent).hasClass("active") ) {
                $(elem).removeClass("active_last");
                if ($(parent).prev().hasClass("active")) { // if previous is active, then make this 'active_next'
                    $(elem).addClass("active_next");
                }
            }
            else if( !$(parent).hasClass("active") ) {
                $(elem).removeClass("active_mid");
                $(parent).next().find('a').removeClass("active_next");
                if ($(parent).prev().hasClass("active")) { // if previous is active, then make this 'active_next'
                    $(elem).addClass("active_next");
                }
            }
        };
    
    // make current menu item 'active' (the 'section' variable is set with PHP and should already be there)
    if (section && section != 'main') { // section is set
        //console.log('section is set: ',section);
        var item = $('div.pt_menu a[href$=' + section + ']').parent(); // find a link inside a div of class 'pt_menu' whose 'href' contains the current section
        $(item).addClass('active');
        if ($(item).hasClass("first")) {
            $(item).find('a').addClass("active_first");
            $(item).next().find('a').addClass("active_next");
        }
        else if ($(item).hasClass("last")) {
                $(item).find('a').addClass("active_last");
        }
        else {
            $(item).find('a').addClass("active_mid");
            $(item).next().find('a').addClass("active_next");
        }
    }
    else if (subsection) { // section is not set, but subsection is
        //console.log('section is not set');
        // figure out which first-level menu element to highlight by looking for second level menu's ID
        // next line: get a link with string=subsection contained in its 'href', then get ID of its parent
        var section_to_activate = $('div#top-menu a[href*=' + subsection + ']').parents('.top-menu-tabs').attr('id');
        // next line: get first-level menu item which has string=section_to_activate in its href
        var item = $('div.pt_menu a[href*=' + section_to_activate + ']').parent(); // find a link inside a div of class 'pt_menu' whose 'href' contains the current section
        // add class 'active' to this item..
        $(item).addClass('active');
        // ..and set its neighbours' classes
        if ($(item).hasClass("first")) {
            $(item).find('a').addClass("active_first");
            $(item).next().find('a').addClass("active_next");
        }
        else if ($(item).hasClass("last")) {
                $(item).find('a').addClass("active_last");
        }
        else {
            $(item).find('a').addClass("active_mid");
            $(item).next().find('a').addClass("active_next");
        }
    }
    
    // menu switch effect on hover for second level menu
    var flag = false; // when mousing over second-level menu, flag becomes 'true', when mousing out - 'false'
    var lastactive = "";
    var links = [];
    var menus = [];
    $('div.pt_menu a').each(function () {
        links.push(this);
        menus.push( $("#"+this.href.split('.com/')[1]).get(0) );
    });

    // figure out which secondary menu to show, if any
    var id_to_show = "";
    $(links).each(function () { // go through all menu links
        if ($(this).parent().hasClass('active')) { // if a link's parent (li) has class active 
            id_to_show = $(this).attr("href").split('/')[1]; // extract a piece from its 'href' attribute that follows '.com/'
        }
    });

    $(links).hoverIntent({ // on hover over first level menu..
        sensitivity: 6, // number = sensitivity threshold (must be 1 or higher)
        interval: 10, // number = milliseconds for onMouseOver polling interval
        over: function() { // what to do on mouseover
            
            if( !((this.href.split('.com/')[1]).split('&')[0] == id_to_show) ){ // do nothing if 'active' is already visible
                var id = this.href.split('.com/')[1];
                $(menus).hide().filter("#"+id).fadeIn();
            }

        },
        timeout: 10, // number = milliseconds delay before onMouseOut
        out: function(event) { // what to do on mouseout
            if (section == 'main') {
                if (!flag) { // if secondary menu was not yet moused over at this point, then..
                    $(menus).fadeOut(); // fade out all secondary menus
                }
            }
            else {
                if (!flag) {
                    if (!(this.href.split('.com/')[1] == id_to_show)) { // if this is not the current item
                        $(menus).fadeOut().filter("#" + id_to_show).fadeIn();
                    }
                }
            }
        }
    });

    // 'mouseover' tab colour change for first-level menu 
    $("div.pt_menu a").hover(function() {
    	lastactive = $(this); // remember last active menu item in order to keep it active when mouse goes over second-level menu
        if( !((this.href.split('.com/')[1]).split('&')[0] == id_to_show) ){ // do nothing if 'active' is already visible
            activate($(this));
        }
    }, function() {
    	deactivate($(this));
    });
    /**** eo first-level menu ****/

    /****  second-level menu  ****/
    $("#"+id_to_show).show(); // show an active menu item on an inner page

    var menuUls = $('#top-menu > ul'); // gather all lists inside element w/ id 'top-menu'
    $(menuUls).hover( 
        function() { // onmouseover action
            activate($(lastactive));
            flag = true;
        },
	    function() { // onmouseout action 
            deactivate($(lastactive));
            lastactive = ""; // to prevent lastActive highlighting again on mousing out and then on 2-nd level menu 
            flag = false;
            //console.log($(this).attr("id"));
            if (!($(this).attr("id") == id_to_show)) { // if current id is not the id of element to keep visible.. 
                $(this).fadeOut();
                $("#" + id_to_show).fadeIn();
            }
        }
    );
    
    // for each second-level list, get rid of the last element's background (the separator)
    $('div#top-menu ul').each(function(){
        $('li:last a', this).css("backgroundImage", "none");
    });

    // add a 'prev' class to a menu item before this page's corresponding menu itme
    //console.log('selecting second level menu: ',$('#'+id_to_show+' li a[href$='+subsection+']'));
    if (subsection) { // subsection should be set with PHP
        $('#'+id_to_show+' li a[href$='+subsection+']').addClass('current').parent().prev().find('a').addClass('active_prev');
    }
    
    // each link inside a list element that's anywhere inside a div with id 'top-menu'
    $('div#top-menu ul li a').hover(
        function(){
            $(this).parent().prev().find('a').addClass('active_prev'); // go to parent, find the parent's previous element (the previous list item), get inside, get its link and toggle the link's css class 'active_prev'
        },function(){
            if(!$(this).hasClass('current')){ // only remove previous element's 'prev' class if this item doesn't correspond to this page
                $(this).parent().prev().find('a').removeClass('active_prev'); // same on mouseout
            }
    });
    
    /**** eo second-level menu ****/
    
    /**** image 'popping-up' effect on hover for menu in content box ****/
    var links_sec = [];
    var messages = [];

    $('div#bottom-menu a').each(function () {
        links_sec.push(this);
        msg = $(this.hash).get(0)
        $(msg).wrapInner( "<div class='mid'></div>" ).prepend("<div class='top'></div>").append("<div class='bot'></div>");        
        messages.push(msg);
    });

    $(links_sec).hoverIntent({ // on hover over menu on bottom of content box..
        sensitivity: 12, // number = sensitivity threshold (must be 1 or higher)
        interval: 0, // number = milliseconds for onMouseOver polling interval
        over: function () {
            if ($(".inner", this).width() != 105) {
                $(".inner", links_sec).animate({  // reduce all images
                    "width":"68px",
                    "height":"52px"
                }, 10 );
                $(".inner", this).animate({ // expand this image
                    "width":"105px",
                    "height":"80px"
                }, 10 );
                
                $('#pitchFirst').hide();
                var msg = $(messages).hide().filter(this.hash);
                if ($.browser.msie) { // IE has problems with fading in PNG images
                    $(msg).show(); // show appropriate message on hover
                }
                else {
                    $(msg).fadeIn(); 
                }
            }
            
        },
        timeout: 0, // number = milliseconds delay before onMouseOut
        out: function () {} // do nothing
    });
    /**** eo image 'popping-up' effect ****/
   
    // create a box around the first, 'welcome', pitch
    $('#pitchFirst').wrapInner( "<div class='mid'></div>" ).prepend("<div class='top'></div>").append("<div class='bot'></div>");

    // create a box around the 'order now' container
    $('#order-box').wrapInner( "<div class='mid'></div>" ).prepend("<div class='top'></div>").append("<div class='bot'></div>");

    // 'our clients' slider initialization
    $(".pt_slider").slider({speed: "slow"});
});