jQuery.noConflict();

/* Rating-Workaround */
function tx_ratings_submit(id, rating, ajaxData, check) {
    jQuery('#tx-ratings-display-' + id).hide();
    jQuery('#tx-ratings-wait-' + id).show();
    jQuery.post('index.php?eID=tx_ratings_ajax', 
        {'ref': id, 'rating': rating, 'data': unescape(ajaxData), 'check': check },
        function(data) { jQuery('#tx-ratings-' + id).html(data); }
    );
}

jQuery(document).ready(function($) {
    /* set a class for script-specific styling */
    $("#esc__wrapper").addClass(WRAPPER_CLASS);
    
    /* initalize menu-expose */
    var menu_exp = $("#esc__mainmenu").expose({api: true, color: '#fff', opacity: 0.5, zIndex: 999, loadSpeed: 0, closeSpeed: 0 });
    $("#esc__mainmenu").hover( function(){ menu_exp.load(); }, function(){ menu_exp.close(); } );
    
    /* initalize overlay + overlay-expose <> */
    var ov_exp = $("#esc__overlay").expose({api: true, color: '#ddd', opacity: 0.5, closeOnClick: false });
    var ov = $("#esc__overlay").overlay({
        oneInstance: false,
        api: true,
        closeOnClick: false,
        onBeforeLoad: function() { ov_exp.load(); },
        onClose: function() {
            ov_exp.close();

            if (typeof OV_AVOID_PAGE_RELOAD !== 'undefined') {
                return;
            }

            var loc_url = window.location.href;
            var hasCommentsAnchor = window.location.href.indexOf('#esc__comments') != -1;

            // trim ts + anchor before reappending it
            if (hasCommentsAnchor)
                loc_url = loc_url.replace(/(\?|&)?(ts=\d+)?#esc__comments/, '');

            if ($("#esc__overlay").hasClass("smallOverlay") && (window.location.href.indexOf('#') == -1 || hasCommentsAnchor)) {
                var ts = (loc_url.indexOf('?') == -1 ? "?" : "&") + "ts=" + (new Date() * 1);
                window.location = loc_url + ts + "#esc__comments";
            } else {
                window.location = loc_url;
            }
        }
    });
    $("#esc__overlay div.header a.close").bind("click", function() { ov.close(); return false; });
    //ov.load();  /* for testing purpose */
    
    /* nav-hack for internet explorer */
    $("#esc__wrapper.explorer #esc__mainmenu li").hover(
        function() { $(this).addClass("hover"); },
        function() { $(this).removeClass("hover"); }
    );
    
    /* font-resizing */
    var font_size = 10;
    $("#esc__topmenu span.smaller a").live("click", function() {
        if ( font_size > 8 ) {
            font_size -= 1;
            $("#esc__content").css("font-size", font_size + "px");
        }
        return false;
    });
    $("#esc__topmenu span.normal a").live("click", function() {
        font_size = 10;
        $("#esc__content").css("font-size", font_size + "px");
        return false;
    });
    $("#esc__topmenu span.bigger a").live("click", function() {
        if ( font_size < 12 ) {
            font_size += 1;
            $("#esc__content").css("font-size", font_size + "px");
        }
        return false;
    });
    
    /* stage */
    var $stage_nav = $("#esc__stage div.viewport ul");
    var $stage_nav_elems = $stage_nav.find("li a");
    var $stage_line = $("#esc__stage div.stage_line");
    function navToViewport($active) {
        var move = false;
        
        if (!$stage_nav || !$stage_nav.length || !$active || !$active.length) {
            return;
        }
        
        /* abs. value of p */
        var p = $stage_nav.position().left * -1;
        /* width of stage_nav-viewport */
        var w = 621;
        /* rel. x-pos of current element */
        var x = $active.position().left;
        /* length (width) of current element */
        var l = $active.outerWidth();
        
        /* out of view left */
        if( p > x ) {
            move = -1 * x;
            
            /* if not first, show a bit of the prev-element */
            if ($active.get(0) != $stage_nav.find("li:first").get(0)) { move += 50; }
            
        /* out of view right */
        } else if( (p + w) < (x + l) ) {
            move = -1 * ( x + l - w );
            
            /* if not last, show a bit of the next-element */
            if ($active.get(0) != $stage_nav.find("li:last").get(0)) { move -= 50; }
        }
        
        /* move navigation to viewport */
        if ( move !== false ) {
            $stage_nav.animate({"left": move+"px"}, "slow");
        }
    }
    /* stage - initially mark first nav item as active */
    $stage_nav.find("li:first").addClass("active");
    $stage_nav_elems.bind("click", function() {
        resetStageRotation();
        navToViewport($(this).parent()); // align the viewport
        var no = $stage_nav_elems.index(this) * -970;
        $stage_line.animate({"left": no+"px"}, "slow");
        $stage_nav.find("li.active").removeClass("active");
        $(this).parent().addClass("active");
        return false;
    });
    /* stage - prev-button */
    $("#esc__stage div.stage_nav a.prev").bind("click", function() {
        resetStageRotation();
        var $cur = $stage_nav.find("li.active");
        if ( $cur.prev().length != 0 ) {
            $cur = $cur.removeClass("active").prev();
            navToViewport($cur); // align the viewport
            var no = $stage_nav_elems.index($cur.children()) * -970;
            $stage_line.animate({"left": no+"px"}, "slow");
            $cur.addClass("active");
        }
        return false;
    });
    /* stage - next-button */
    $("#esc__stage div.stage_nav a.next").bind("click", function() {
        resetStageRotation();
        var $cur = $stage_nav.find("li.active");
        if ( $cur.next().length != 0 ) {
            $cur = $cur.removeClass("active").next();
            navToViewport($cur); // align the viewport
            var no = $stage_nav_elems.index($cur.children()) * -970;
            $stage_line.animate({"left": no+"px"}, "slow");
            $cur.addClass("active");
        }
        return false;
    });

    /* automatic stage rotation */
    function startStageRotation() {
        stage_rotation = null;
        if (typeof(stage_rotation_interval) == 'undefined' || isNaN(stage_rotation_interval) || stage_rotation_interval < 1)
            return;
        stage_rotation = setInterval(function() {
            var $cur = $stage_nav.find("li.active");
            if ( $cur.next().length != 0 )
                $cur = $cur.removeClass("active").next();
            else
                $cur = $cur.removeClass("active").parent().find("li:first");
            navToViewport($cur); // align the viewport
            var no = $stage_nav_elems.index($cur.children()) * -970;
            $stage_line.animate({"left": no+"px"}, "slow");
            $cur.addClass("active");
        }, stage_rotation_interval * 1000);
    }
    function resetStageRotation() {
        clearInterval(stage_rotation);
        startStageRotation();
    }

    if ($('#esc__stage').length) {
        startStageRotation();
    }

    /* make whole shown stage content clickable to go to the article */
    $('#esc__stage div.stage').each(function() {
        var href = $(this).find('h3 a').attr('href');
        if (href) {
            $(this).click(function() {
                window.location = href;
            }).css('cursor', 'pointer');
        }
    });
    
    /* dashboard */
    $("#esc__dashboard a.target_dash").bind("click", function() {
        var url = $(this).attr("href");
        
        $("#esc__dashboard").load("dashboard/dash_subpage_login.php #esc__dashboard *");
        //$.get("",function(data){ $("#esc__dashboard").html(data); });
        return false;
    });
    $("a.target_overlay").bind("click", function() {
        var url = $(this).attr("href");
        
        $("#esc__overlay").removeClass("wideOverlay").removeClass("smallOverlay");
        if ( $(this).hasClass("wideOverlay") )
            $("#esc__overlay").addClass("wideOverlay");
        if ( $(this).hasClass("smallOverlay") )
            $("#esc__overlay").addClass("smallOverlay");
            
        $("#esc__overlay iframe").attr("src", url);
        ov.load();
        return false;
    });
    
    /* partner-fading */
    var partner_cur_li = $("#esc__footer #partners li:first");
    var partner_toggle = setInterval(function(){
        partner_cur_li.find("a.partner:first").appendTo(partner_cur_li.find("span.partner"));
	    partner_cur_li.find("span.partner a.partner:first").hide().prependTo(partner_cur_li).fadeIn("slow");
	    
        if ( partner_cur_li.next().length )
            partner_cur_li = partner_cur_li.next();
        else
            partner_cur_li = $("#esc__footer #partners li:first");
    }, 3000);
    //clearInterval(partner_toggle); /* for testing purpose */

    /* print link in service navigation */
    $("#print_link").live("click", function() {
        window.print();
        return false;
    });

    /* dashboard dropdown box */
    var $dashboard_table_co2 = $("#esc__dashboard_co2");
    var $dashboard_table_cost = $("#esc__dashboard_cost");
    $dashboard_table_cost.hide();
    $("#esc__dashboard form").show().submit(function() { return false });
    $("#esc__dashboard select").change(function() {
        if(this.value == "co2") {
            $dashboard_table_cost.hide();
            $dashboard_table_co2.show();
        } else {
            $dashboard_table_co2.hide();
            $dashboard_table_cost.show();
        }
    });
    
    /* comments */
    $("#esc__comments div.list_comments div").slice(3).hide();
    $("#esc__comments a.show_all").show();
    $("#esc__comments a.show_all").bind("click", function() {
        $("#esc__comments div.list_comments div").show();
        $("#esc__comments a.show_all").hide();
        return false;
    });
    
    $('span.shareThisLink a').click(function() {
        $(this).parent().parent().find('div.shareThisCode').toggleClass('hide');

        if (!$.browser.msie) // in ie6/7 the input text disappears when using focus+select, so we can't use it here
            $(this).parent().parent().find('div.shareThisCode input').focus(function() { this.select(); });

        return false;
    });
 
    /* statement ticker */
    if (typeof st_items_to_show !== 'undefined') {
        (function() {
            var $st_viewport       = $('#esc__content div.st_viewport');
            var $st_item_container = $st_viewport.find('ul:first');
            var $st_items          = $st_item_container.find('li');

            var st_item_count      = $st_items.length;

            var st_viewport_height = 0;
            var st_current_index   = 0;

            var st_busy = false;

            var st_autoscroll = true;
            var st_scroll_direction = '+';

            $st_items.slice(0, st_items_to_show).each(function() {
                st_viewport_height += $(this).outerHeight(true);
            });
            $st_viewport.css('height', st_viewport_height + 'px');

            $('#esc__content a.st_list_up, #esc__content a.st_list_down').click(function() {
                st_autoscroll = false;

                setTimeout(function() {
                    st_autoscroll = true;
                }, 10000);

                if (st_busy) {
                    return false;
                }

                var new_index = $(this).hasClass('st_list_down') ? st_current_index + 1 : st_current_index - 1;
                if (new_index < 0 || new_index > (st_item_count - st_items_to_show)) {
                    return false;
                }

                scrollTo(new_index);

                return false;
            });

            var scrollTo = function(index) {
                st_busy = true;
                var position = $st_items.eq(index).position();

                $st_item_container.animate({
                    top: -position.top
                }, 400, function() {
                    st_current_index = index;
                    st_busy = false;
                });
            };

            var autoScroll = function() {
                if (!st_autoscroll) return;

                if (st_scroll_direction == '+') {
                    if (st_current_index + 1 <= (st_item_count - st_items_to_show)) {
                        scrollTo(st_current_index + 1);
                    } else {
                        st_scroll_direction = '-';
                        scrollTo(st_current_index - 1);
                    }
                } else {
                    if (st_current_index - 1 >= 0) {
                        scrollTo(st_current_index - 1);
                    } else {
                        st_scroll_direction = '+';
                        scrollTo(st_current_index + 1);
                    }
                }
            }

            setInterval(autoScroll, 5000);

        })();
    }

});

