﻿$(document).ready(function(){
	checkListStatus();
	$("#rsfCommunities area").bind("click",function(){
		selectCommunity($(this).attr("class"),"large");
		return false;
	}).bind("mouseenter",function(){
		selectCommunity($(this).attr("class"),"small");
	}).bind("mouseleave",function(){
		// Do nothing
	});
	$("#listContainer a").bind("click",function(){
		selectCommunity($(this).attr("class"),"large");
		return false;
	});
	$("#mapImg").bind("mouseleave",function(){
		hideCommunities();
	});
	$("#listToggle p").bind("click",function(){
	    $("#listContainer").toggle();
	    $("#listToggle p").toggle();
	    var lStatus = $("#listContainer").css("display");
	    switch(lStatus){
	        case "none":
	            cVal = 0;
	            break;
	        default:
	            cVal = 1;
	            break;
	    }
	    createCookie("rsfList",cVal,1);
	});
});

function selectCommunity(comName,comSize){
	var idSize;
	var clSize;
	switch(comSize){
		case "small":
			idSize = "S";
			clSize = "small";
			break;
		case "large":
			idSize = "L";
			clSize = "large";
			break;
		default:
			idSize = "S";
			clSize = "small";
			break;
	}
	if(idSize == "L"){
		var itemHeight = $("#" + idSize + "COM" + comName).css("height");
		var itemWidth = $("#" + idSize + "COM" + comName).css("width");
		var itemTitle = $("#" + idSize + "COM" + comName + " a").children("." + idSize + "COM-title").text();
		tb_show(itemTitle,"#TB_inline?height=" + itemHeight.substring(0,itemHeight.length - 2) + "&width=" + itemWidth.substring(0,itemWidth.length - 2) + "&inlineId=" + idSize + "COM" + comName,"");
	}else{
		if($("#" + idSize + "COM" + comName).css("display") == "none"){hideCommunities();}
		$("#" + idSize + "COM" + comName).show("fast");
	}
}

function hideCommunities(){
	$(".item-small, .item-large").hide("fast");
}

function checkListStatus(){
    var cVal = readCookie("rsfList");
    switch(cVal){
        case "0":
            // Do nothing
            break;
	    case "1":
	        $("#listContainer").css("display","block");
	        $("#listToggle p").toggle();
	        break;
	    default:
	        $("#listContainer").css("display","block");
	        $("#listToggle p").toggle();
	        break;
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var 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);
}
