var content;
var familyRoomAvailable;
var performaceRoomAvailable;

function createCookie(name,value,days) {
	var expires;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	else{
		expires = "";
	} 
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)===' ')
		{
			 c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) === 0){
		 return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function getBlogContent(){
	return content;	
}

//allow access to the living room and jamies room
function activateFamilyRoom(){
	$("#jamieLink > img").attr("src", "imgs/Jaimies_Room_On.png");
	$("#familyLink > img").attr("src", "imgs/LivingRoom_On.png");		

	$("#jamieLink").click(function(){
			return true;
	});
	$("#familyLink").click(function(){
			return true;
	});
	
	createCookie("familyRoomAvailable", true, 100);
}

//allow direct access to the performance room
function activatePerformRoom(){
	$("#performLink").click(function(){
			return false;
	});
	$("#performLink > img").attr("src", "imgs/Live_On.png");			
	createCookie("performaceRoomAvailable", true, 100);
}

$(function(){
	
    var starTime = 0;
    var endTime = 0;

	$("#bandwidth_modal").show().dialog({
		modal:true,
		autoOpen: false,
		resizable: false,
		draggable: false,
		buttons: 
			{"Leave": function(){
				window.location = "http://www.uuvvwwz.com/low.html";					
			},
			"Stay": function(){
				$("#bandwidth_modal").dialog("close");							
			}
		}			
	});	

    $.ajax({
        url: "bandwidth_test.txt",
        beforeSend: function(){
            startTime = (new Date()).getTime();
        },
        success: function(data){
            endTime = (new Date()).getTime();
            var difference = (endTime-startTime)/1000;
            //use the size of the file and the time to download to calculate speed
            var speed = 43824/difference;
            var kbps = speed/1024;
			if(kbps < 5){
				$("#bandwidth_modal").dialog("open");
			}
        }
    });
    
	$.ajax({
		url: "blogData.php",		
		data: "alt=rss",
		type: "GET",
		success: function(msg){
			content = msg;
		}
	});
	
	//onload see if these rooms have been made available
	familyRoomAvailable = readCookie("familyRoomAvailable");
	performaceRoomAvailable = readCookie("performaceRoomAvailable");
	
	//if not block the links, if so change the image
	if(familyRoomAvailable === null){
		$("#jamieLink").click(function(){
			return false;
		});
		$("#familyLink").click(function(){
			return false;
		});
	}else{
		$("#jamieLink >img").attr("src", "imgs/Jaimies_Room_On.png");
		$("#familyLink > img").attr("src", "imgs/LivingRoom_On.png");		
	}
	
	if(performaceRoomAvailable === null){
		$("#performLink").click(function(){
				return false;
		});
	}else{
		$("#performLink > img").attr("src", "imgs/Live_On.png");			
	}
	
});

