/**
 * Browser requirements 
 * @type Array
 */
var requirements =  new Array();
/* needed browser versions */
requirements['browserVersion'] =  new Array();
requirements['browserVersion']['webkit'] = 500;
requirements['browserVersion']['mozilla'] = 1.8;
requirements['browserVersion']['msie'] = 9;
requirements['browserVersion']['opera'] = 10;

var maxTweets = 10;

// tweet cache
var tweetsCache;

// creating a namespace
(function(){

// check borwser requirements
	var passedBrowserCheck = (jQuery.browser.webkit == true && parseFloat(jQuery.browser.version) >= requirements['browserVersion']['webkit']) ? true : 
							 (jQuery.browser.mozilla == true && parseFloat(jQuery.browser.version) >= requirements['browserVersion']['mozilla']) ? true : 
							 (jQuery.browser.msie == true && parseFloat(jQuery.browser.version) >= requirements['browserVersion']['msie']) ? true :
							 (jQuery.browser.opera == true && parseFloat(jQuery.browser.version) >= requirements['browserVersion']['opera']) ? true :  
							 false;
	// not passed browser test!
	if(!passedBrowserCheck)
	{
		jQuery("div#notSupportedBrowser").show();
		jQuery("div#body").hide();
        return false;
	}
	
	(function(){
	  // if firefox 3.5+, hide content till load (or 3 seconds) to prevent FOUT
	  var d = document, e = d.documentElement, s = d.createElement('style');
	  if (e.style.MozTransform === ''){ // gecko 1.9.1 inference
	    s.textContent = 'h1{visibility:hidden}';
	    e.firstChild.appendChild(s);
	    function f(){ s.parentNode && s.parentNode.removeChild(s); }
	    //addEventListener('load',f,false);
	    setTimeout(f,1200); 
	  }
	})();
	
	jQuery(document).ready(main);
	
	function main()
	{	
				
		var indexPage = "home";
		
		// set the anhor
		var uri = window.location + ''; //making a string
        uri = uri.replace("%23","#"); // replacing encoding
        
        //  bookmarked ? 
        if(uri.indexOf('#') != -1 && uri.substr(uri.indexOf("#")+1).length > 0)
        	indexPage = uri.substr(uri.indexOf("#")+1);
		
		//turn all navigation links into ajaxInterface requests
		jQuery("a.ajax").live("click",(function(){
			
			window.location = baseUrl() + "#" + jQuery(this).attr("href");

			jQuery.ajaxInterface({
			   url: "./sites/" + jQuery(this).attr("href") + ".xml",
               type: 'GET',
			   async: true
			 });
			 
			return false;	
		}));
		
		// load the initail content
		jQuery.ajaxInterface({
		   url: "./sites/" + indexPage + ".xml",
           type: 'GET',
		   async: true
		 });	
	}
})();


function twitterJSONcallback(pTweets)
{
    // load tweets 
	if(typeof tweetsCache == "undefined")
        jQuery('#loadingTweets').fadeOut(500, function(){
            jQuery('#loadingTweets').remove();
            parseTweet(0, pTweets, 0);
        });	
    // display from cache
    else
        jQuery('#loadingTweets').fadeOut(1, function(){
            jQuery('#loadingTweets').remove();
            jQuery('div#tweets').fadeOut(1).append(tweetsCache.html()).fadeIn(750);
        });	
        
        
    
}

function parseTweet(pI, pTweets, pPrinted)
{
    if(pTweets.length == (pI))
	    return false;
      
    if(maxTweets === pPrinted)
        return false;

    if(pTweets[pI].in_reply_to_status_id !== null)
    {
        parseTweet((pI+1), pTweets, pPrinted)
        return false;
    }

    var tweetDate = new Date(Date.parse(pTweets[pI].created_at));
    var tweetHTML = jQuery('<article class="tweet">');
        tweetHTML.append('<h3>' + tweetDate.toLocaleDateString() + '</h3><span class="dblQoutes">&#147;</span>');
        tweetHTML.append('<p>' + replaceURLWithHTMLLinks(pTweets[pI].text) + '</p>');
			
        jQuery('div#tweets').append(tweetHTML);
        tweetsCache = jQuery('div#tweets').clone();
        
        tweetHTML.fadeIn(500,function(){
            parseTweet((pI+1), pTweets, (pPrinted+1))
        })
       
}

function replaceURLWithHTMLLinks(text) {
  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  return text.replace(exp,"<a href='$1' target='_blank'>$1</a>"); 
}

function baseUrl()
{
	return ((window.location+'').indexOf("#") != -1) ? (window.location+'').substr(0,(window.location+'').indexOf("#")) : window.location;
}


