﻿/**
*	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.
*/
/**
*  Loader function
*  Add a function to window.onload event
*/
function addLoadEvent(func, args) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = function() {
            func(args);
        }
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func(args);
        }
    }
}


$(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/hardrockcalling.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=%23hrc2010", 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>");
            }
        });
    }
});
/**
*  News page Functions
*/
var News = {

    newsId: null,               // Block ID containing ALL News headers & details
    newsDetailsClass: null,     // Block Class containing Specific News details
    newsHeaderClass: null,      // Block Class containing Specific News header

    newsItems: new Array(),
    newsHeaders: new Array(),

    init: function(args) {

        this.newsId = args[0];
        this.newsDetailsClass = args[1];
        this.newsHeaderClass = args[2];

        // Build News blocks objects
        var block = $(this.newsId);
        var el = block.getElementsByTagName('div');
        var el_length = el.length;
        for (i = 0; i < el_length; i++) {
            if (el[i].className == this.newsDetailsClass) {
                var tmp = el[i];
                News.setParam(tmp, 'newsId', el[i].id);
                News.newsItems.push(tmp);
            } else if (el[i].className == this.newsHeaderClass) {
                var aTags = el[i].getElementsByTagName('a');
                var aL = aTags.length;
                for (j = 0; j < aL; j++) {
                    News.setParam(aTags[j], 'newsId', aTags[j].id);
                    aTags[j].onclick = function() {
                        News.showInfo(this);
                        //return false;
                    }
                    News.newsHeaders.push(aTags[j]);
                }
            }
        }

        // Show correct block function of query string
        var qs = location.href.split('?')[1];
        if (qs != 'undefined' && qs != null) {
            qs = qs.split('#')[0];
            var aid = qs.split('=')[1];
            var o = new Object;
            News.setParam(o, 'newsId', aid);
            News.showInfo(o);
        } else {
            // Default
            News.showInfo(News.newsItems[0]);
        }

    },

    showInfo: function(el) {
        var l = News.newsItems.length;
        if (typeof (el) == 'object') { // Action triggered by a click event
            for (i = 0; i < l; i++) {
                if (News.newsItems[i].newsId == el.newsId) {
                    News.newsItems[i].style.display = 'block';
                } else {
                    News.newsItems[i].style.display = 'none';
                }
            }
        } else {
            for (i = 0; i < l; i++) {
                if (News.newsItems[i].newsId == el) {
                    News.newsItems[i].style.display = 'block';
                } else {
                    News.newsItems[i].style.display = 'none';
                }
            }
        }
        var loader = $('loader');
        if (loader != null) loader.style.display = 'none';
    },

    setParam: function(e, param, value) {
        e[param] = value;
    },

    getParam: function(e, param) {
        return e[param];
    }

};


/**
* 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();
    }
});