$(document).ready(function() {
	//"The poppy thing"	
	$("#SnapABug_bImg").mouseover(function() { console.log("asd"); $("#SnapABug_bImg").animate({
		marginLeft: "5px" }, 250 );
	}).mouseout(function(){$("#SnapABug_bImg").animate({marginLeft: "0px" }, 250 );});
	
	$(".handle").mouseover(function() { console.log("asd"); $(".handle").animate({
		marginLeft: "-5px" }, 250 );
	}).mouseout(function(){$(".handle").animate({marginLeft: "0px" }, 250 );});
	
	if ($("#mySwf").length > 0)
	{ //if it's there
		var flashvars = {};
		var params = {
			wmode: "transparent",
		  allowfullscreen : "true"
		};
		var attributes = {};
		swfobject.embedSWF("/info/infographic.swf", "mySwf", "100%", "360", "9.0.0","/info/expressInstall.swf", flashvars, params, attributes);
	}
	
	if ($("#cercord").length > 0){ //if it's there
		$('#cercord').liteAccordion({autoPlay : true, pauseOnHover :true});
	 }

	var timer = setInterval(function() { getTwitter(); },60000);	
	
	var twitterContent = "";
	var updatedTwitterContent = "";
	var afterName = "";
	var twitterCount = 0;
	twindex = 0;
	var items = [];
	var count = 0;
	function getTwitter()
	{
		jQuery.support.cors = true;
		$.getJSON('http://pipes.yahoo.com/pipes/pipe.run?_id=6072307398295f2eeca7fdbf06a0c961&_render=json', function(data) 
		{
			items = []; items.length = 0;
			count = 0;
			$.each(data, function(key, val) 
			{
				if(key == "count") twitterCount = val;
				if(key == "value") $.each(val,function(key2, val2) 
				{
					if(key2 == "items") $.each(val2,function(key3, val3) 
					{
						if((key3 >= twindex) && (key3 < (twindex+4)))
						{
							items.push("<div class='clear' style='padding-bottom:10px;'>");
							
							switch(val3['author'])
							{
								case 'CernamKaren':
									items.push( '<a class="twitter" href="http://twitter.com/#!/CernamKaren"><img class="twitterThumb" alt="twitterAvatar" src="http://a2.twimg.com/profile_images/1407601897/karen_fb_wal_reasonably_small.jpg"/></a>');
									twit = '<a class="twitter" href="http://twitter.com/#!/CernamKaren">CernamKaren</a>';
									break;
								case 'CernamOwen':
									items.push( '<a class="twitter" href="http://twitter.com/#!/CernamOwen"><img class="twitterThumb" alt="twitterAvatar" src="http://a0.twimg.com/profile_images/1222128191/Owen4_normal.png"/></a>');
									twit = '<a class="twitter" href="http://twitter.com/#!/CernamOwen">CernamOwen</a>';
									break;
								default:
									items.push( '<a class="twitter" href="http://twitter.com/#!/CernamOE"><img class="twitterThumb" alt="twitterAvatar" src="http://a1.twimg.com/profile_images/1214496810/LogoNoText__2__normal.jpg"/></a>');
									twit = '<a class="twitter" href="http://twitter.com/#!/CernamOE">CernamOE</a>';
									break;
							}
							afterName = val3['description'].split(':');
							afterName = val3['description'].substring(afterName[0].length+1);
							
							items.push(afterName.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1">$1</a>') + " ");
							items.push(twit);
							items.push("</div>");
						}
						count++;
						
					});
				});
			});

			updatedTwitterContent = items.join('');
			if(updatedTwitterContent != twitterContent)
			{
				$('#twitstream').fadeOut(1000, function(){$(this).empty().html(updatedTwitterContent).fadeIn(1000)});
				twitterContent = updatedTwitterContent;
			}
			
		
		})
		.error(function(XMLHttpRequest, textStatus, errorThrown) { console.log('Error ' + textStatus); console.log('Error Thrown ' + errorThrown); });

	}
	

	$.ajaxTransport("+*", function( options, originalOptions, jqXHR ) {
    
    if(jQuery.browser.msie && window.XDomainRequest) {
        
        var xdr;    
        return {
            send: function( headers, completeCallback ) {
                // Use Microsoft XDR
                xdr = new XDomainRequest();
                
                xdr.open("get", options.url);
                
                xdr.onload = function() {
                    
                    if(this.contentType.match(/\/xml/)){
                        
                        var dom = new ActiveXObject("Microsoft.XMLDOM");
                        dom.async = false;
                        dom.loadXML(this.responseText);
                        completeCallback(200, "success", [dom]);
                        
                    }else{
                        completeCallback(200, "success", [this.responseText]);
                    }

                };
                
                xdr.ontimeout = function(){
                    completeCallback(408, "error", ["The request timed out."]);
                };
                
                xdr.onerror = function(){
                    completeCallback(404, "error", ["The requested resource could not be found."]);
                };
                
                xdr.send();
          },
          abort: function() {
              if(xdr)xdr.abort();
          }
        };
      }
    });
	

	getTwitter();
	
	$('#more').click(function() 
	{ 
		if(twindex<(twitterCount-3)) 
			{ twindex=twindex+3; getTwitter();}
		else $('#more').fadeOut(1000);
								
	});
	

});
