//TODO: decide a way to let the page decide the size of the popup
//		currently hard coded to 200 x 400 px...

var passThisObj;
var cp_alert_hide_timeout;

J(document).ready(function(){
	cp_ajax_pop_bind();
	J("a.cp_ajax_link").click(cp_ajax_link);
});

function cp_ajax_pop_bind(){
	J("a.cp_ajax_pop").unbind().click(function(){
		passThisObj = this;
		var popupHref=J(this).attr('href');
		if(popupHref.indexOf("?"))
			popupHref+="&ajax=true";
		else
			popupHref+="?ajax=true";
		var popupPos=J(this).offset();
		cp_popup(popupHref,200,400,popupPos.left,popupPos.top);
		return false;
	});
}

// param	dObj  Object of the link that is being clicked
//if used inside of ajax click function leave dObj blank and it will automatically use the containing object
// param	dObjReturn	string containing id of object for return data
//if passed dObjReturn it will load the target page into the object specified
//if left undefined it will load the target page as JS
function cp_ajax_link(dObj,dObjReturn){
	if(typeof(J(dObj).attr('href')) == 'undefined')
		dObj=this;
	passThisObj = dObj;
	cp_alert_hide();
	
	if(typeof(dObjReturn) == 'undefined')	
		J.getScript(J(dObj).attr('href')+".js");
	else
		J("#"+dObjReturn).load(J(dObj).attr('href')+"?ajax=true");
	return false;	
}

function cp_popup(href,height,width,x,y){
	J('div.cp_popup').remove();	
	J("body").append('<div class="cp_popup" style="height: '+height+'px; width: '+width+'px; top: '+y+'px; left: '+x+'px;">Loading...</div>');
	J("div.cp_popup").load(href,'',function(){
		J("div.cp_popup").fadeIn('normal');
	});
}
function cp_popup_hide(){
	J('div.cp_popup').remove();
}
//styles: cp_alert  cp_alert_good  cp_alert_ok  cp_alert_bad
function cp_alert(dMsg,dMsgType){
	J('div.cp_alert').remove();	
	J("body").append('<div class="cp_alert '+dMsgType+'">'+dMsg+'</div>');
	J("div.cp_alert").click(cp_alert_hide).animate({
		top: "100px"
	},500,function(){cp_alert_hide_timeout=setTimeout(cp_alert_hide,4000)});
}

function cp_alert_hide(){
	clearTimeout(cp_alert_hide_timeout);	
	J('div.cp_alert').animate({
		top: "-150px"
	},400,function(){
		J('div.cp_alert').remove();
	});
}