/* AJAX FUNCTIONS */

function displayLoader(el){
	el.html('<img style="margin:50px auto;" src="/images/divers/ajax-loader.gif" />');
}

function loadAjax(page, histo, el){
	if(histo!=true)
		histo = false;
	
	var el = el || $(".corps");
	
	displayLoader(el);

	$.post(""+page,
		function(html){
			el.html(html);
		}
	);
	
}

$(".ajax_link").live("click", function(){
	loadAjax($(this).attr("href"));
	return false;
});

/* POPUP FUNCTIONS */

function showModalOverlay(){
	$("#modal_overlay").remove();
	
	var overlay = $("<div>");
	overlay.css({
		position: "fixed",
		width:"100%",
		height:"100%",
		backgroundColor:"#000",
		opacity:0.6,
		zIndex:1000001
	});
	overlay.attr("id", "modal_overlay");
	$("body").prepend(overlay);
}


function popup(html, title, width, modal, func){
	var title = title || '';
	var width = width || 200;
	var modal = modal || false;
	var func = func || false;
	
	if(html == undefined)
		return false;
		
	var l = {"title" : title, "width":width}
	var popup = createPopup(l, func);
		
	popup.append(html);
	popup.css({
		zIndex:1000002,
		width: width+"px"
	});
	
	
	if(modal){
		showModalOverlay();
	}
	$(document.body).prepend(popup);
	
}

function showPopup(element_id, modal, func){
	var modal = modal || false;
	var func = func || false;
	var el = $("#"+element_id);
	
	var title = el.attr("title");
	if(title == undefined) title = '';
	
	var width = parseInt(el.css("width"));
	if(isNaN(width))
		width = 200;
	
	var l = {"title" : title, "width":width}
	var popup = createPopup(l, func);
	popup.append(el);
	
	
	$(document.body).prepend(popup);
	el.show();
	popup.show();
	
	if(modal){
		showModalOverlay();
	}
	
}



function createPopup(l, func){
	$("#general_popup").remove();
	var popup = $("<div>");
	popup.attr("id", "general_popup");
	popup.addClass("popup");

	
	var title_bar = createTitleBar(popup, l.title, func);
	popup.append(title_bar);
	
	
	
	var marginLeft = (document.body.clientWidth-l.width)/2;
	popup.css({
		zIndex: "1000001",
		marginLeft: marginLeft+"px",
		marginTop: (120+document.documentElement.scrollTop)+"px"
	});

	return popup;
}

function createTitleBar(parent, title, func){
	var func = func || false;
	var a = $("<div>");
	a.attr("id", "popup_title_bar");
	a.html(title);
	
	
	b = $("<a>");
	b.attr("href", "#");
	b.attr("id", "close_popup_button");
	a.prepend(b);
	
	a.bind("mousedown", function(e){
		var mouse_posX = e.pageX;
		var mouse_posY = e.pageY;
		
		var popup_initX = parseInt(parent.css("marginLeft"));
		var popup_initY = parseInt(parent.css("marginTop"));
		
		$(document).bind("mousemove", function(e){
			var new_left = popup_initX - (mouse_posX - e.pageX);
			var new_top = popup_initY - (mouse_posY - e.pageY);
			
			var width = parseInt(parent.css("width"));
			var height = parseInt(parent.css("height"))+12;
			
			if(new_left<0)
				new_left = 0;		
			else if(new_left>document.body.clientWidth-width)
				new_left = document.body.clientWidth-width;
			
			
			
			if(new_top<0)
				new_top = 0;		
			else if(new_top>document.body.clientHeight-height)
				new_top = document.body.clientHeight-height;
			
			parent.css({
				marginLeft: new_left+"px",
				marginTop: new_top+"px"
			});
			
		});
		
		$(document).bind("mouseup", function(){
			$(document).unbind("mousemove");
			$(document).unbind("mouseup");
		});
		
	});

	b.bind("click", function(){
		if(func!==false)
			func.call();
		removePopup();
		a.unbind("mousedown");
		parent.hide();
		$(document.body).prepend(parent);
		return false;
	});
	
	
	return a;
}



function removePopup(){
	$(".popup").hide();
	$("#modal_overlay").remove();
}



function requestPublishStream(perms, func){
	FB.login(function(response) {
		console.log(response);
		if (response.session) {
			if (response.perms) {
				func.call();
			// user is logged in and granted some permissions.
			// perms is a comma separated list of granted permissions
			} else {
			// user is logged in, but did not grant any permissions
			}
		} else {
			top.location.href = "<?php echo $facebook->getLoginUrl(); ?>";
		}
	}, {perms: perms});

}


/* Preload images */

function preloadImage(images){
	
	document.preloader = new Array();
	var p = document.preloader;
	
	for(i in images){
		p[i] = new Image();
		p[i].src = images[i];
	}
	
}
$(document).ready(function(){
	var el = new Array();
	el.push('/images/popup/marine/title_bar.png');
	el.push('/images/popup/marine/cross.png');
	el.push('/images/popup/marine/cross_hover.png');
	el.push('/images/form/radio_bg.png');
	
	preloadImage(el);
});
