var debug = false;

$(document).ready(function() {
	debugLog("common.document.ready()");
	initDocument();
});

function initDocument() {
	debugLog("common.initDocument()");
		$('a.popup, #menu_popup_login_htm a').click(function() {
		debugLog("common.a.click()");
		launchPopup($(this).attr('href'));
		return false;
	});

	$('a.videoplayer').addClass('iframe');

	$('a.videoplayer').fancybox({
		'hideOnContentClick': false,
		'showCloseButton': true,
		'width': 650,
		'height': 420
	});
	if (typeof(initPage)=="function") initPage();
}

var popup_width = 0;
var popup_title = "";
var popup_buttons = {};
var popup_id = 0;
var popup_loading = false;

function launchPopup(url,id,data) {
	debugLog("common.launchPopup('"+url+"','"+id+"') popup_loading=["+popup_loading+"]");

	if (!popup_loading) {

		popup_loading = true;

		if (!id) {
			id = popup_id++;
		
			$('body').append('<div id="popup_'+id+'" style="display:hidden"><'+'/div>');
		
			popup_width = 600;
			popup_title = "";
			popup_buttons = {};
		}
	
		if (!data) {
			data = "popup_id="+id;
		} else {
			data += "&popup_id="+id;
		}
		
		$.ajax({
			url: url,
			type: "get",
			data: data,
			cache: false,
			dataType: "html",
			success: function(data, textStatus, XMLHttpRequest){
				debugLog("common.launchPopup: ajax success");
				$('#popup_'+id).html(data).dialog({
					title: popup_title,
					modal: true,
					resizable: false,
					autoOpen: true,
					width: popup_width,
					buttons: popup_buttons,
					close: function(event, ui) {
						//alert('close');
						$('#popup_'+id).remove();
					}
				});
				popup_loading = false;
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				debugLog("common.launchPopup: ajax error");
				alert('error\n XMLHttpRequest['+XMLHttpRequest+']\n textStatus['+textStatus+']\n errorThrown['+errorThrown+']');
				popup_loading = false;
			}
		});
	}
	return false;
}

/* ==================== */

function createCookie(name,value,days) {
	var expires="", date=new Date();
	if (days) {
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires="; expires="+date.toGMTString();
	}
	document.cookie=name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ=name+"=", ca=document.cookie.split(';'), c="";
	for(var i=0;i < ca.length;i++) {
		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);}
