//facebook feed
var items = 0;
var current = 5;

function fbFetch(mobgenpic,more){
	//Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
	if(more){
		items = items + 5;
		var func = "'/$!gid;/images/mobgen_placeholder_logo.png','" + items +"'";
		$('more').html("<h2 id='more' onClick='fbFetch(" + func + ")'>VIEW MORE</h2>");
	}else{
		items = current;
	}

	var accessToken = "209854849052265|3ff4cebfa8dd1e25d61c8426.1-100001409350136|cR7bCgsXXefU7mz4ajQiPbEiQ_o";
	var url = "https://graph.facebook.com/343740997218/feed?limit=" + items + "&callback=?&access_token="+accessToken;
	var application = "";
		//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
		$.getJSON(url,function(json){
		
			var html = "";
			
			//loop through and within data array's retrieve the message variable.
			$.each(json.data,function(i,fb){
			
				if(fb.application){
					application = fb.application.name;
				}else{
					application = "Facebook";
				}

				if(fb.message ==undefined){
					var message = "";
				}else{
					var urlmatch = fb.message.match(/https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/g);
					var urlfix = "<a href='" + urlmatch + "' target='_blank'>" + urlmatch + "</a>";
					var message = fb.message.replace(/https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/,urlfix);
				}
				
				if(fb.picture){
					var picture = "<img src='" + fb.picture + "' class='blogimgfacebook'/>";
				}else {
					var picture = "<img src='" + mobgenpic + "' class='blogimg'/>";
				}
				
				var fbname = "Mobile Generation";
				if(fb.from.name==fbname){
					html += "<div class='blogitem'>";
					html += picture;
					html += "<p class='blogtext'><strong>MobGen &nbsp;";
					html += "</strong></br>" + message + "<br><strong>" + fuzzyFacebookTime(fb.created_time.replace(/-/g,'/')) + "&nbsp;Via: " + application + "</strong></p>";
					html += "<div class='blogline'></div>";
					html += "</div>";
				}
			});

			//A little animation once fetched
			$('.facebookfeed').animate({opacity:0}, 500, function(){
 
				$('.facebookfeed').html(html);
																  });
			$('.facebookfeed').animate({opacity:1}, 500);
		});
};
	var currentPicture = Math.floor(Math.random()*6);
	function changePicture(){
		$('#background'+currentPicture).fadeTo(1000, 0);
		currentPicture++;
		if(currentPicture == 6){
			currentPicture = 0;
		}
		
		blackTimer();	
	}
	function blackTimer(){
		alertTimerId = setTimeout ( "fromBlack()", 2000 );
	}
	
	function fromBlack(){
		$('#background'+currentPicture).fadeTo(1000, 1);
		backgroundTimer();
	}
	
	function backgroundTimer(){
		 alertTimerId = setTimeout ( "changePicture()", 12000 );
	}

$(function() {

	backgroundTimer();
	fromBlack();
/* Organic Tabs */

    $.organicTabs = function(el, options) {
    
        var base = this;
        base.$el = $(el);
        base.$nav = base.$el.find(".nav");
                
        base.init = function() {
        
            base.options = $.extend({},$.organicTabs.defaultOptions, options);
            
            // Accessible hiding fix
            $(".hide").css({
                "position": "relative",
                "top": 0,
                "left": 0,
                "display": "none"
            }); 
            
            base.$nav.delegate("li > a#navPage", "click", function() {
            
                // Figure out current list via CSS class
                var curList = base.$el.find("a.current").attr("href").substring(1),
                
                // List moving to
                    $newList = $(this),
                    
                // Figure out ID of new list
                    listID = $newList.attr("href").substring(1),
                
                // Set outer wrapper height to (static) height of current inner list
                    $allListWrap = base.$el.find(".list-wrap"),
                    curListHeight = $allListWrap.height();
            	    $allListWrap.height(curListHeight);
                                        
                if ((listID != curList) && ( base.$el.find(":animated").length == 0)) {
                                            
                    // Fade out current list
                    base.$el.find("#"+curList).fadeOut(base.options.speed, function() {
                        
                        // Fade in new list on callback
                        base.$el.find("#"+listID).fadeIn(base.options.speed);
                        
                        // Adjust outer wrapper to fit new list snuggly
                        var newHeight = base.$el.find("#"+listID).height();
						newHeight += 12;
						
                        $allListWrap.animate({
                            height: newHeight
                        });
                        
						
						
                        // Remove highlighting - Add to just-clicked tab
                        base.$el.find(".nav li a").removeClass("current");
                        $newList.addClass("current");
                            
                    });
                    
                }   
                // $allListWrap({ autoHeight: false })
                // Don't behave like a regular link
                // Stop propegation and bubbling
                return false;
            });
            
        };
        base.init();
    };
    
    $.organicTabs.defaultOptions = {
        "speed": 300
    };
    
    $.fn.organicTabs = function(options) {
        return this.each(function() {
            (new $.organicTabs(this, options));
        });
    };


/* Overlay */

	var buffer = 40,
	speed  = 175;

	$("#page-wrap>a>div")
			.find("span")
			.css({
					left: -200
			})
			.end()
			.mouseenter(function(e) {
			
					var $el       = $(this),
						pos       = $el.position(),
						boxLeft   = pos.left,
						boxRight  = pos.left + $el.width(),
						boxTop    = pos.top,
						boxBottom = pos.top + $el.height(),
						
						$innerBox = $el.find("span");
					
					if ((e.pageX - boxLeft) < buffer) {
							
							$innerBox.removeClass().removeAttr("style").addClass("outLeft").animate({
									left: 0
							}, speed);
							
					} else if ((boxRight - e.pageX) < buffer) {
							
							$innerBox.removeClass().removeAttr("style").addClass("outRight").animate({
									left: 0
							}, speed);
							
					} else if ((e.pageY - boxTop) < buffer) {
							
							$innerBox.removeClass().removeAttr("style").addClass("outTop").animate({
									top: 0
							}, speed);
							
					} else if ((boxBottom - e.pageY) < buffer) {
					
							$innerBox.removeClass().removeAttr("style").addClass("outBottom").animate({
									top: 0
							}, speed);
							
					}
			
			})
			.mouseleave(function(e) {
			
					var $el       = $(this),
						pos       = $el.position(),
						boxLeft   = pos.left,
						boxRight  = pos.left + $el.width(),
						boxTop    = pos.top,
						boxBottom = pos.top + $el.height(),
						
						$innerBox = $el.find("span");
					
					if ((e.pageX - boxLeft) < buffer) {
							
							$innerBox.removeClass().removeAttr("style").animate({
									left: -200
							}, speed);
							
					} else if ((boxRight - e.pageX) < buffer) {
							
							$innerBox.removeClass().removeAttr("style").animate({
									left: 200
							}, speed);
							
					} else if ((e.pageY - boxTop) < buffer) {
							
							$innerBox.removeClass().removeAttr("style").animate({
									top: -200
							}, speed);
							
					} else if ((boxBottom - e.pageY) < buffer) {
					
							$innerBox.removeClass().removeAttr("style").animate({
									top: 200
							}, speed);
							
					}
			
			});
});			
