function openFeed(){
    var params = getParams();
    if ( params.get( "section" ) ){
        var section = params.get("section");
        toggleFeedOptions( section );

        if ( params.get("name") ){
            var feedName = unescape( params.get('name') ); 
            var feeds = $( section + "_feeds" ).getElementsByTagName("a");
            for ( var i = 0; i < feeds.length; i++ ){
                if ( feeds[i].innerHTML == feedName){
                    feeds[i].onclick.call( feeds[i] );
                }
            }
        }
    }
}

function toggleFeedOptions( id ){
    urchinTracker( 'widgets_auto.html/toggle_feed/' + id );
    var display = $( id + "_feeds").style.display;
    if ( display == "none" ){
        addClass( $( id + "_feeds_title" ), "sl_auto_feeds_site_sel");
        $( id + "_arrow" ).src = "../images/feeds_twarrow_dwn.gif";
    }
    else {
        removeClass($( id + "_feeds_title" ), "sl_auto_feeds_site_sel");
        $( id + "_arrow" ).src = "../images/feeds_twarrow_rt.gif";
    }
    $( id + "_feeds").style.display = display == 'none' ? 'block' : 'none';
}

var selectedLink;
function toggleSelectedLink( thisLink ){
    if ( selectedLink ){
        selectedLink.style.color = "#0099CC";
    }
    thisLink.style.color = "#333333";
    selectedLink = thisLink;
}

function showFeed( thisLink, xsl, feedUrl, title, type, feedTitleLink, titleLink, sort ){
    urchinTracker( 'widgets_auto.html/show_feed/' + ( title ? title : feedUrl ) );
    if ( thisLink ){
        toggleSelectedLink( thisLink );
    }
    for ( var i = 0; i < LOGIN_SITE_IDS.length; i++ ){
        $( LOGIN_SITE_IDS[i] + "_login" ).style.display = "none";
    }
    $( "sl_auto_feeds_getstarted" ).style.display = "none";
    $( "sl_auto_feeds_preview" ).style.display = "block";

    var params = "xsl=" + xsl + "&feedUrl=" + escape(feedUrl);
    if ( title ){
        params += "&title=" + escape( title );
    }
    if ( type ){
        params += "&type=" + type;
    }
    if ( feedTitleLink ){
        params += "&feedTitleLink=" + feedTitleLink;
    }
    if ( titleLink ){
        params += "&titleLink=" + titleLink;
    }
    if ( sort ){
        params += "&sort=" + sort;
    }

    var paramMap = getParams();
    var paramKeys = paramMap.getKeys();
    for ( var i = 0; i < paramKeys.length; i++ ){
        params += "&" + paramKeys[i] + "=" + paramMap.get(paramKeys[i]); 
    }

    $("feed-iframe").src =  "http://" + domain + "/users/ShowAutoFeedPreview.php?" + params;
    return false;
}

var LOGIN_SITE_IDS = [ "amz", "amz_listmania", "bb", "lst", "nfx" ];

function showLogin( thisLink, id, type ){
    toggleSelectedLink( thisLink );
    for ( var i = 0; i < LOGIN_SITE_IDS.length; i++ ){
        $( LOGIN_SITE_IDS[i] + "_login" ).style.display = ( LOGIN_SITE_IDS[i] == id ) ? "block" : "none";
    }
    $( "sl_auto_feeds_getstarted" ).style.display = "none";
    $( "sl_auto_feeds_preview" ).style.display = "none";
    if ( type && $( id + "_type" ) ) {
        $( id + "_type" ).innerHTML = type;        
    }
    return false;
}

function showFeedURLWidget( xsl, urlId, title ){
    showFeed( null, xsl, $(urlId).value, title );
    deactivateInput( "Feed URL", $(urlId) );
    return false;
}

function showLastFMWidget(){
    var value = $("lst_type").innerHTML;
    var title = "My Last.fm ";
    if ( value.indexOf("Top Albums") == 0 ){
        params = "topalbums.xml";
        title += "Top Albums";
    }
    else if ( value.indexOf("Top Artists") == 0 ){
        params = "topartists.xml";
        title += "Top Artists";
    }
    if ( value.indexOf("(3 Mos.)") != -1 ){
        params += "?type=3month";
        title += ": Past 3 Months";
    }
    var feed = "http://ws.audioscrobbler.com/1.0/user/" + $("lst_username").value + "/" + params;
    showFeed( null, "lastfm.xsl", feed, title );
    deactivateInput( "Username", $("lst_username") );
    return false;
}

function showAmazonWidgetByEmail( type ){
    setValidationMsg( "amz_login_msg", "Looking up your info, please wait..." );
    var url = "http://" + domain + "/users/GetPageContent.php?url=http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService" +
              "&AWSAccessKeyId=07GG2103RY5KFCY09GG2&Operation=ListSearch&ListType=" + type + "&Email=" + $("amz_email").value;
    var ajax = new AJAXInteraction( url, function(req){
            var txt = req.responseText;
            if ( txt.indexOf( "Error" ) == -1 ){
                setValidationMsg( "amz_login_msg" );
                var listId = txt.substring( txt.indexOf("<ListId>") + "<ListId>".length, txt.indexOf("</ListId") );
                showFeed( null, "amazon-list.xsl", "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=07GG2103RY5KFCY09GG2&Operation=ListLookup&ListType=WishList&ListId="
                        + listId + "&ResponseGroup=ItemAttributes,Images,EditorialReview", "My Amazon Wish List",
                        null, "http://www.amazon.com/gp/registry/wishlist/" + listId );
            }
            else {
                setValidationMsg( "amz_login_msg", "Could not find any matches for this email address.", true );
            }
        });
    ajax.doGet();
    return false;
}

function showAmazonWidgetByUrl( type ){

    var wishlistUrl, validationId;
    if(type == "Listmania") {
        wishlistUrl = $("amz_listmania_url").value;
        validationId = "amz_listmania_msg";
    }
    else {
        wishlistUrl =  $("amz_url").value;
        validationId = "amz_url_msg";
    }

    var listId = getAmazonListIDFromURL(wishlistUrl, type);

    if(listId != "") {
        var listLink = "";
        var title = "";
        if(type == "Listmania") {
            listLink = "http://www.amazon.com/gp/lm/" + listId;
            title = "Listmania List";
        }
        else {
            listLink = "http://www.amazon.com/gp/registry/wishlist/" + listId;
            title = "My Amazon Wish List";
        }

        showFeed( null, "amazon-list.xsl", "http://ecs.amazonaws." + getAmazonSuffix(wishlistUrl) + "/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=07GG2103RY5KFCY09GG2&Operation=ListLookup&ListType=" + type + "&ListId="
                + listId + "&ResponseGroup=ItemAttributes,Images,EditorialReview", title,
                null, listLink );

    }
    else {
        setValidationMsg( validationId, "Could not get your wishlist from this link.", true );
    }

    return false;
}

function getAmazonListIDFromURL( url, type ) {
    var listId = "";

    var idStart = url.indexOf("/wishlist/");
    var idEnd = -1;
    if(type == "WishList" && idStart > -1) {
        idStart += 10;
        idEnd = url.indexOf("/", idStart);
    }
    else if(type == "WishList" && url.indexOf("registry.html") > -1) {
        idStart = url.indexOf("id=") + 3;
        idEnd = url.indexOf("&", idStart);
    }
    else if(type == "Listmania" && url.indexOf("/lm/") > -1) {
        idStart = url.indexOf("/lm/") + 4;
        idEnd = url.indexOf("/", idStart);
    }

    if(idEnd == -1) {
        idEnd = url.length;
    }

    if(idStart > -1) {
        listId = url.substring(idStart, idEnd);
    }

    return listId;
}

function getAmazonSuffix( url ) {
    var suffix = "com";
    var suffixStart = url.indexOf("amazon.");
    if(suffixStart > -1) {
        suffixStart += 7;
        var suffixEnd = url.indexOf("/", suffixStart);
        if(suffixEnd == -1) {
            suffixEnd = url.length;
        }
        suffix = url.substring(suffixStart, suffixEnd);
    }

    return suffix;
}

function enterAmazonURL() {
    $('amz-email-form').style.display = "none";
    $('amz-wishlist-url-form').style.display = "block";

    return false;
}

function enterAmazonEmail() {
    $('amz-email-form').style.display = "block";
    $('amz-wishlist-url-form').style.display = "none";

    return false;
}

function activateInput( initalText, input ) {
    if ( input.value == initalText ) {
        input.value = "";
        input.style.color = "#333333";
    }
    input.focus();
    return false;
}

function deactivateInput( initialText, input ){
    if ( input.value == "" ){
        input.value = initialText;
        input.style.color = "#999999";
    }
    return false;
}

