﻿/// <reference path="libs/jquery-1.3.2-vsdoc.js" />

/**
*	Javascript Tools based on jquery
*
*	@date		2009-02-12
*	@author		Michal Gondar
*	@copyright	Live Nation (Music) UK
*   @require    jquery
*   @jquery     jquery-1.3.1.min.js
*
*   This is main file with javascript outside class.
*/


/*
 *  check all input[type=text] elements,
 *  remember default value
 *  make onFocus, onBlur functions
 *  // make focus on first found input
 */
$(document).ready(function() {
    $("input[type=text]").focus(function() {
        if( this.defVal == null ) {
            this.defVal = $(this).val();
        }
        if( this.value == this.defVal ) {
            this.value = "";
        }
    });
    $("input[type=text]").blur(function() {
        if( this.value == "" ) {
            this.value = this.defVal;
        }
    });

    /* select first input box */
    /*
    if( $("input[type=text]")[0] ) {
        $("input[type=text]")[0].focus();
    }
    */
});


/* A-Z bands select */
/* makes option clickable */
$(document).ready(function() {
    $("#azbands select").change(function () {
        if( this.value ) {
            window.location = "/lineup/artist.aspx?aid=" + this.value;
        }
    });
});


/**
* Twitter ajax
**/
function encodeTweet(text) {
    // finds all @'s, hastags and links and converts to markup
    tweet = text.replace(/http:\/\/\S+/g, '<a href="$&" target="_blank">$&</a>')
        .replace(/(@)(\w+)/g, ' <a href="http://twitter.com/$2" target="_blank">@$2</a>')
        .replace(/(#)(\w+)/g, ' <a href="http://search.twitter.com/search?q=%23$2" target="_blank" class="by">#$2</a>');
    return tweet;
}
function relative_time(time_value) {
    // sets relative time from tweet post
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);

    if (delta < 60) {
        return 'less than a minute ago';
    } else if (delta < 120) {
        return 'about a minute ago';
    } else if (delta < (60 * 60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if (delta < (120 * 60)) {
        return 'about an hour ago';
    } else if (delta < (24 * 60 * 60)) {
        return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if (delta < (48 * 60 * 60)) {
        return '1 day ago';
    } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
    }
}

$(document).ready(function() {
    // only runs if the twitter column is available
    if ($("#twitterFeed ul").length > 0) {
        $.getJSON("http://search.twitter.com/search.json?callback=?&rpp=3&q=%23playaway", function(data) {
            $("#twitterFeed ul").html("");
            if (data.results.length > 0) {
                $.each(data.results, function(i, item) {
                    tweet = encodeTweet(item.text);
                    var values = item.created_at.split(" ");
                    time_value = values[2] + " " + values[1] + ", " + values[3] + " " + values[4];
                    tweetDate = relative_time(time_value);
                    tweetMethod = item.source.replace(/(&quot;)/g, "\"").replace(/(&lt;)/g, "<").replace(/(&gt;)/g, ">");
                    $("#twitterFeed ul").append("<li><a href='http://www.twitter.com/" + item.from_user + "' title=''>" + item.from_user + ":</a> " + tweet + " <span class='tweetDetails'>" + tweetDate + " from " + tweetMethod + "</span></li>");
                });
            } else {
                $("#twitterFeed ul").append("<li>no tweets to show...</li>");
            }
        });
    }
});

/** Follow us tab **/
$(document).ready(function() {
    // build links
    socialLinks = $("<ul></ul>");
    socialLinks.append("<li class='followTwitter'><a href='http://twitter.com/playawayfest' title=''>Follow us on twitter</a></li>");
    socialLinks.append("<li class='followFacebook'><a href='http://www.facebook.com/playawayfestival' title=''>Follow us on facebook</a></li>");
    socialLinks.append("<li class='followMyspace'><a href='http://www.myspace.com/playawayfestival' title=''>Follow us on MySpace</a></li>");

    // add to follow section
    $(".followTab").append(socialLinks);

    // kill click
    $(".followTab #nav9").click(function() { return false; });

    // set up fade
    $(".followTab ul").css("display", "none");

    $(".followTab").mouseover(function() {
        $(".followTab ul").css("display", "block");
    }).mouseout(function() {
        $(".followTab ul").css("display", "none");
    });
});
