﻿/**
*	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.
*/

$(document).ready(function() {
    
	/* CALENDAR POPUP */
	
	$('.popupHolder').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;
		
		var pPos = $('.popup-link:first', $(parent)).position();

		$(popup).css({
			display: 'block',
			top: pPos.top - popup.height() - 35,
			left: pPos.left - 125
		}).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://api.twitter.com/1/statuses/user_timeline/LatitudeFest.json?callback=?", 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 class='tweet'>" + tweet + "</p><p class='tweetDetails'>" + tweetDate + " from " + data[0].source + "</p>");
            } else {
                $("#latestTweet").append("<p class='tweet'>no tweets to show...</p>");
            }
        });
    }
    if ($("#twitterFeed ul").length > 0) {
        $.getJSON("http://search.twitter.com/search.json?callback=?&rpp=5&q=%23latitudefestival", 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 + "'  class='by' title=''>" + item.from_user + ":</a> " + tweet + " <span class='tweetDetails'>" + tweetDate + " from " + tweetMethod + "</span></li>");
                });
            } else {
                $("#twitterFeed ul").append("<li>no tweets to show...</li>");
            }
        });
    }
});

/*** Slideshow plus minus buttons (Mariana)  **/

jQuery(document).ready(function() {
    $('div.showHide').click(function() {
        $(this).next().slideToggle('slow');
        var img = $(this).find("img"); //get a handle of the image tag inside title link
        var src = $(img).attr("src"); //get the source of the image
        //toggle the source of the image to show either plus/minus
        if (src.endsWith("plus.png"))
            src = src.replace('../_Resources/img/plus.png', '../_Resources/img/minus.png');
        else
            src = src.replace('../_Resources/img/minus.png', '../_Resources/img/plus.png');
        $(img).attr("src", src);
        return false;
    }).next().hide();
});
String.prototype.endsWith = function(str) {
    return this.lastIndexOf(str) == this.length - str.length;
}