jQuery(document).ready(function() {
	$("#abc3-games-window").dialog({ 
		autoOpen: false,
        modal: true,
        height: "auto",
        width: 900,
        draggable: false,
        resizable: false,
		position: 'top',
		bgiframe: true,
		beforeclose: onCloseDialogGame,

            overlay: {
                    opacity: 0.8, 
                    background: "black"
                }
            });
	 
	$(".dialog-open").click(function(){
		$.address.value($(this).attr('id'));
		return false;
	});
	
	 // Init and change handlers
    $.address.init(function(event) {
        
    }).change(function(event) {
        handleChange(event);
    });
	
});

function onCloseDialogGame(event, ui){
	$.address.value('');
	$("#abc3-games-window").empty();
}
var currentGame = {};
function launchGame(el){
	$("#abc3-games-window").empty();
	var title = jQuery(el).text();
	var url = $.address.value();
	currentGame.elementId = "#games-window-nav";
	currentGame.title = title;
	currentGame.id = '/abc3/games/#'+url;
	currentGame.format = 2;
	currentGame.thumb = "";
	
	currentPage = currentGame;
	dcsMultiTrack("DCS.dcsuri",currentGame.id,"WT.ti",currentGame.title);
	$("#abc3-games-window").load($(el).attr("href"),{},function(){
		$("iframe").css("z-index",-5);
	});
	updateUserOptions("#games-window-nav",currentGame.id,'2');
	var myDims = $(el).attr("rel").split('x');
	$.address.title(jQuery(el).text() + " - Games - ABC3");
	
	if(myDims){
		//$("#abc3-games-window").dialog("option", "width",myDims[0]+50);
	}
	
	$("#abc3-games-window").dialog("option", "dialogClass", "abc3-dialog-games-window");
	$("#abc3-games-window").dialog("option", "title", $(el).attr("title"));
	$("#abc3-games-window").dialog("open");
}

function handleChange(event) {
	if(event.path!="/"){
	var id=event.path;
	id.replace(/\//, '');
	str = id.substr(1,id.length-1);
	launchGame(jQuery("#"+str));
    }

}