﻿function winopen(url,winname,x,y){
    var host = 'http://hothardware.com';
    if(url.indexOf(host) == 0){
        var currentScript = $('#SiteScript').attr('src');
        url = currentScript.substring(0,currentScript.indexOf('/scripts/site.js')) + url.substring(host.length);
    }
    if(url.indexOf('image_popup') >= 0){
        x = 1024;
        y = 700;
    }
    var options = "toolbar=no,scrollbars=yes,resizable=yes,width=" + x + ",height=" + y;
    msgWindow=window.open(url,winname,options);
};

function navigateArticle(el) {
    window.location = $(el).val();
};

jQuery.easing.easeOutQuart = function(x, t, b, c, d) {
    return -c * ((t = t / d - 1) * t * t * t - 1) + b;
};

var forum = {
    fixImages : function(){
        var loc = window.location.toString();
        if(loc.indexOf('cs2007') >= 0){
            $('.ForumPostContentText p img[width]').removeAttr('width').removeAttr('height');
        }
    }
};

var site = {
    init: function() {
        //$('select.navigator').bind("change", site.navigate);
        $('input.with-hint').bind("focus", site.clearHint).bind("blur", site.showHint)
            .each(function(i, el) {
                $(el).attr('start-value', $(el).val()).parents('form').bind("submit", site.clearAllHints);
            });
        $('#Authentication .AuthenticationPassText').bind("focus", site.clearPassHint)
        .each(function(i, el) {
            $('#Authentication .AuthenticationPassword').hide();
            $(el).show();
        });
        $('#Authentication .AuthenticationPassword').bind("blur", site.showPassHint)
        $('.Tab a').bind("click", site.changeTab);
        $('#Comments .ThreadSubscription a').bind("click", site.toggleNewsSubscription);
        forum.fixImages();
        site.initGalleryStrip();
    },
    clearAllHints: function() {
        $(this).find('input.with-hint').each(function(i, el) {
            el = $(el);
            if (el.val() == el.attr('start-value')) el.val('');
        });
    },
    clearHint: function() {
        var el = $(this);
        if (el.val() == el.attr('start-value')) el.val('');
    },
    showHint: function() {
        var el = $(this);
        if (el.val() == '') el.val(el.attr('start-value'));
    },
    clearPassHint: function() {
        var el = $(this);
        var pass = $('#Authentication .AuthenticationPassword');
        el.hide();
        pass.show();
        window.setTimeout("$('#Authentication .AuthenticationPassword').focus();", 100);
    },
    showPassHint: function() {
        var el = $(this);
        var txt = $('#Authentication .AuthenticationPassText');
        if (el.val() == '') {
            el.hide();
            txt.show();
        }
    },
    navigate: function() {
        window.location = $(this).val();
    },
    changeTab: function() {
        var tab = $(this).parent();
        var strip = $(tab).parents('.TabStrip')[0];
        var sectionContainer = $('#' + $(strip).attr("rel"));
        if (!tab.hasClass("Active")) {
            $(strip).find(".Tab").each(function() {
                $(this).removeClass("Active");
            });
            tab.addClass("Active");
            $(sectionContainer).find(".TabSection").each(function() {
                $(this).addClass("Inactive");
            });
            $("#Section-" + tab.attr("id").replace("Tab-", "")).removeClass("Inactive");
        }
        return false;
    },
    toggleNewsSubscription: function() {

        $('#Comments .ThreadSubscription img').show();

        $.ajax({
            type: "POST",
            url: "/callback.aspx/ToggleSubscription",
            data: "{'NewsID':" + $('#Comments .ThreadSubscription a').attr("id").replace("SubscribeNews_", "") + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                $('#Comments .ThreadSubscription img').hide();
                if (msg.d) {
                    $('#Comments .ThreadSubscription span').text("You are currently subscribed to this conversation.");
                    $('#Comments .ThreadSubscription a').text("Unsubscribe");
                } else {
                    $('#Comments .ThreadSubscription span').text("");
                    $('#Comments .ThreadSubscription a').text("Subscribe to this conversation");
                }
            },
            error: function() {
                $('#Comments .ThreadSubscription img').hide();
            }
        });

        return false;
    },
    initGalleryStrip: function() {
        $('.Strip').each(
        function() {
            var s = 5; if ($(this).parents('#Featured').length > 0) s = 4;
            $(this).serialScroll({
                target: '.ScrollingPane',
                items: 'li',
                prev: 'a.prev',
                next: 'a.next',
                offset: -2,
                start: 0,
                step: s,
                duration: 1200,
                force: true,
                stop: true,
                lock: false,
                cycle: false, //don't pull back once you reach the end
                easing: 'easeOutQuart',
                onBefore: site.handleScroll
            });
        });
        site.initArrows();
    },
    handleScroll: function(e, elem, $pane, $items, pos) {
        site.enableDisableArrows($pane, $items, pos);
        site.loadThumbnails($items, pos);
    },
    loadThumbnails: function($items, pos) {
        var endpos = pos + 5;
        if (endpos > $items.length) endpos = $items.length;
        for (var i = pos; i < endpos; i++) {
            var img = $($items[i]).find("img")[0];
            if ($(img).attr("rsrc") != undefined) {
                $(img).attr("src", $(img).attr("rsrc")).removeAttr("rsrc");
            }
        }
    },
    initArrows: function() {
        $('.Strip').each(
        function() {
            var prev = $(this).find('a.prev img');
            var next = $(this).find('a.next img');
            var items = $(this).find('.ScrollingPane li');
            var s = 5; if ($(this).parents('#Featured').length > 0) s = 4;
            prev.fadeTo(100, 0.25);
            if (items.length <= s) { next.fadeTo(100, 0.25); }
        });
    },
    enableDisableArrows: function($pane, $items, pos) {
        var ribbon = $($pane).parents('.Strip')[0];
        var prev = $(ribbon).find('a.prev img');
        var next = $(ribbon).find('a.next img');
        var s = 5; if ($($pane).parents('#Featured').length > 0) s = 4;
        if ($items.length <= s) { prev.fadeTo(100, 0.25); next.fadeTo(100, 0.25); return; }
        if (pos + s > $items.length - 1) next.fadeTo(100, 0.25); else next.fadeTo(100, 1);
        if (pos == 0) prev.fadeTo(100, 0.25); else prev.fadeTo(100, 1);
    }
};