﻿// common page load event items here
$(function() {
    wireupnav();
    wireupShare();
});

function wireupShare() {
    $(".link_share").click(function() {
        $(this).fadeOut('fast', function() { $(this).next().fadeIn('fast'); });
        return false;
    });
}


function wireupnav() {
    $(".nav_channels").hover(
        function() { $("#channels_expanded").stop(false, true).slideDown('fast'); $($(".nav_channels")[0]).addClass("active"); },
        function() { $("#channels_expanded").stop(false, true).slideUp('fast'); $($(".nav_channels")[0]).removeClass("active"); }
    );

    $("#channels_expanded").hover(
        function() { $("#channels_expanded").stop(false, true).show(); $($(".nav_channels")[0]).addClass("active"); },
        function() {$("#channels_expanded").stop(false, true).slideUp(); $($(".nav_channels")[0]).removeClass("active"); 
    });
}

function homepage_init(conversation_nav) {

    // wire up conversation carousel
        $("#homepage_carousel").jCarouselLite({
            visible: 1,
            btnNext: ".button_CarouselNext",
            btnPrev: ".button_CarouselPrev",
            btnGo: conversation_nav,
            auto: 7000,
            speed: 1000,
            afterEnd: function(a) {
                    $("#nav_carousel .active").removeClass('active'); // clear previous
                    $("#nav_carousel .nav_page" + $(a).attr('class').replace('slide', '')).addClass('active');
                }
            });
        
        //fyi: whats buzzing module wires itself up in template, depending on # of items

        // wire up infographic
        $(".lightbox").lightBox({ imageBtnClose: themeroot + '/images/button_lightbox_close.gif' });

        // init channel images
        $(".channel_image img:nth-child(1)").fadeIn();
        //setInterval(rotateChannelImages, 12000);

       
}

function changeCommentType(t) 
{
    $(".addconnection").toggle();
    $(".addcomment").toggle();
    $("#commentType").val(t);
    return false;
}

function wireup_itemlist(el, num_show) {
    num_show = num_show || 3;
    // wire up nav
    $(el + " .items").jCarouselLite({
        btnNext: el + " .nav_next",
        btnPrev: el + " .nav_prev",
        visible: 1,
        scroll: 1,
        /*vertical: true,*/
        afterEnd: function(a) {
            p = $(a).attr('class').replace('slide', '');
            p = (p == 0) ? "1" : p / num_show + 1;
            $(el + " .currentpage").html(p);
        }
    });
}

function mychannel_init() {

    // watch selection clicks, add to form
    $(".formchoice").click(function () {
        if ($(this).attr('checked')) {
            addMyChannelItem(this);
        }
        else {
            // find matching form item, remove
            id = "#yourselections #formitem_" + $(this).attr('id');
            p = $(id).parent(); // get parent <li>, remove
            $(p).fadeOut(333, function () { $(p).remove(); });
        }
    });

    // wire up remove button using .live due to dynamic creation of button
    $(".remove").live('click', function () {
        pid = $(this).attr('id').replace('formitem_', '');  // get parent id
        $("#" + pid).attr('checked', false);  // uncheck
        p = $(this).parent();   // get parent <li> in form
        $(p).fadeOut(333, function () { $(p).remove(); });  // remove <li> from form
        return false;
    });


    //$("#txtTag").autocomplete({ source: tagsJSON });

}

function SubmitMyChannels() {

    $("body").css("cursor", "progress");

    // validate
    em = $("#email").val();

    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(em) == false) {
        alert('ERROR: Invalid Email Address');
        $("body").css("cursor", "default");
        return false;
    }

    // store email in form as hidden field
    $("#emailaddress").val(em);
    
    $("#subscribe").submit();
    
    return false;
}

function ShowSubscribe() {
    $('#recentactivity').fadeOut(333);
    
    $('#panel_mychannels').fadeOut(333, function() {
        $('#panel_subscribe').fadeIn(333);
        $('#yourselections').removeClass('mychannels'); 
    });
    
    scroll(0, 0); 
    return false;
}


function addMyChannelItem(el) {
    id = $(el).attr('id'); // base id for new item from parent
    c = $(el).attr('class').replace('formchoice', '');// get item type from parent classname
    c = $.trim(c);  // $.trim() needed for IE8
    v = $(el).val();
    text = $(el).next().text();
    n = $(el).attr('name');
    // create new form entry, insert into form
    data = "<li><input name='" + n + "' type='hidden' value='" + v + "' id='formitem_" + id + "' /><span>" + text + "</span>";
    data +="<a id='formitem_" + id +"' href='#' class='remove'></a></li>";
    $(".selected_" + c).append(data);
}



function SearchGo() {
    if ($("#s").val())
        $("#searchform").submit();

    return false;
}

function CheckComment() {

    if ($("#commentType").val() == "connection") {
        var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	    if (regexp.test($("#connectionURL").val()) == false){
            alert('ERROR: Invalid Connection URL');
            return false;
        }

        if ($("#connectionTitle").val() == "") {
            alert("ERROR: please enter a Connection Title");
            return false;
        }
 
    }
    return true;
}

function checkEmail() {
    em = $("#EMAIL").val();

    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(em) == false) {
        alert('ERROR: Invalid Email Address');
        $("body").css("cursor", "default");
        return false;
    }

    return true;
}

var currentChannelImage = 1;

function rotateChannelImages() {

    jQuery(".channel_image img:nth-child(" + (currentChannelImage) + ")").fadeOut('fast');
    currentChannelImage = (currentChannelImage + 1 > 3) ? 1 : currentChannelImage + 1;
    jQuery(".channel_image img:nth-child(" + (currentChannelImage) + ")").fadeIn('fast');
}
