﻿/**
*	Javascript Tools based on jquery
*
*	@date		2009-11-30
*	@copyright	Live Nation (Music) UK
*   @require    jquery
*   @jquery     jquery-1.3.1.min.js
*
*   This is main file with javascript outside class.
*/
// TWITEER
$(document).ready(function() {
    
	/* CALENDAR POPUP */
	
	$('.eventViewCalendar .event-link').each(function() {

		var link = $('.popup-link:first', $(this));
		var popup = $('.popup:first', $(this)).css('opacity', 0);

		link.mouseover(popupLinkMouseOver).mouseout(popupLinkMouseOut);
		popup.mouseover(popupLinkMouseOver).mouseout(popupLinkMouseOut);
	});
	function popupLinkMouseOver()
	{
		var parent = $(this).parent().get(0);
		var popup = $('.popup', parent);

		if (parent.hideDelayTimer)
			clearTimeout(parent.hideDelayTimer);

		// if its acutally animating than dont animate again
		if( $('.popup:animated', parent).size() )
			return false;
		
		// if its showen than dont show again
		if( $('.popup:visible', parent).size() )
			return false;
		
		$(popup).css({
			display: 'block',
			top: - (popup.height() + 20)
		}).animate({
			top: '-=10px',
			opacity: 1
		}, 200);
		
		return false;
	}
	function popupLinkMouseOut()
	{
		var parent = $(this).parent().get(0);;
		var popup = $('.popup', parent);

		if (parent.hideDelayTimer)
			clearTimeout(parent.hideDelayTimer);

		var actualIndex = $('.eventViewCalendar .event-link .popup').index(popup);

		// hide all other popups what
		// :animated - are not actualy animating (means they are hiding)
		$('.eventViewCalendar .event-link .popup:not(:eq(' + actualIndex + '))').css('opacity', 0);

		parent.hideDelayTimer = setTimeout(function () {
			popup.animate({
				top: '-=10px',
				opacity: 0
			}, 200, function () {
				popup.css('display', 'none');
			});
		}, 300);

		return false;
	}
});

/**
* 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 ($("#latestTweet").length > 0) {
        $.getJSON("http://twitter.com/statuses/user_timeline/wirelessfest.json?callback=?&count=1", function(data) {
            $("#latestTweet").html("");
            if (data.length > 0) {
                tweet = encodeTweet(data[0].text);
                var values = data[0].created_at.split(" ");
                time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
                tweetDate = relative_time(time_value);
                $("#latestTweet").append("<p>" + tweet + "</p><p class='tweetDetails'>" + tweetDate + " from " + data[0].source + "</p>");
            } else {
                $("#latestTweet").append("<p>no tweets to show...</p>");
            }
        });
    }
    if ($("#twitterFeed ul").length > 0) {
        $.getJSON("http://search.twitter.com/search.json?callback=?&rpp=5&q=%23wf2010", 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>");
            }
        });
    }
});


/**
* Hero transition
**/
function swapPanel(panelToShow) {
    // show new content
    $(panels[currentPanel]).stop().animate({ marginLeft: "-330px" }, 500);
    $(panelLinks[currentPanel]).css("visibility", "hidden");
    if (currentPanel != panelToShow) { $(bgImages[currentPanel]).stop().animate({ opacity: 0 }, 1000).css("z-index", "0"); }

    // hide old content
    $(panels[panelToShow]).stop().animate({ marginLeft: "0" }, 700);
    $(panelLinks[panelToShow]).css("visibility", "visible");
    $(bgImages[panelToShow]).stop().animate({ opacity: 1 }, 700).css("z-index", "10");
}
function setTimer() {
    t = setTimeout("rotatePanels()", 7000);
}
function rotatePanels() {

    if (currentPanel < panels.length - 1) {
        panelToShow = currentPanel + 1;
    }
    else 
    { panelToShow = 0; }

    swapPanel(panelToShow);
    currentPanel = panelToShow;
    setTimer();
}

$(document).ready(function() {
    currentPanel = 0;
    panels = $("#hero li");
    bgImages = $("#hero img");
    panelLinks = $("#hero a");

    bgImages.css("opacity", 0);

    // only runs if the hero is present
    if (panels.length > 0) {
        panels.each(function(i) {
            $(this).mouseover(function() {
                swapPanel(i);
                currentPanel = i;
            });
        });

        $("#hero").mouseover(function() { clearTimeout(t); }).mouseout(function() { setTimer(); });

        swapPanel(currentPanel);
        setTimer();
    }
});