/************************************************************************/
/* DOCEBO CORE - Framework												*/
/* ============================================							*/
/*																		*/
/* Copyright (c) 2006													*/
/* http://www.docebo.com												*/
/*																		*/
/* This program is free software. You can redistribute it and/or modify	*/
/* it under the terms of the GNU General Public License as published by	*/
/* the Free Software Foundation; either version 2 of the License.		*/
/************************************************************************/

var coursecatalogue_server_url = '';
var cart_summary = '';
var toggled_courses = new Array();
var _LANG = new Array();

var current_table_cell='';
var id_course;

function toggle_adv_search() {

	Effect.toggle('advanced_search', 'blind');
	if($('advanced_search_link').className == 'adv_link_exp') $('advanced_search_link').className = 'adv_link';
	else $('advanced_search_link').className = 'adv_link_exp';
	if($('do_search_up').style.display == 'none') $('do_search_up').style.display = 'inline';
	else $('do_search_up').style.display = 'none';
}


function setup_coursecatalogue(passed_server_url) {
	coursecatalogue_server_url = passed_server_url;

	var data = "op=getLang";
	var objAjax = new Ajax.Request(
        	coursecatalogue_server_url,
        	{method: 'post', parameters: data, onComplete: setup_coursecatalogue_lang}
    );
}

function setup_coursecatalogue_lang(ObjReq) {
	var langText = ObjReq.responseText;
	_LANG = langText.evalJSON(true);

	var data = "op=getCartSummary";
	var objAjax = new Ajax.Request(
        	coursecatalogue_server_url,
        	{method: 'post', parameters: data, onComplete: setup_coursecatalogue_complete}
    );
}

function setup_coursecatalogue_complete(ObjReq) {
	cart_summary=ObjReq.responseText;
}


function show_cart_preview() {
	
	var cart =  $('the_cart');
	
	if(cart != undefined) return ;
	cart = document.createElement('div');
	
	var obj = $('cart_link');
	var pos = Position.cumulativeOffset(obj)
	
	var y = pos[1];
	var x = pos[0];
	var x2 = x + parseInt(obj.offsetWidth);
	var y2 = y + parseInt(obj.offsetHeight);
	
	cart.id = 'the_cart';
	cart.style.zIndex = '6000';
	cart.style.position = 'absolute';
	cart.style.top = y2+'px';
	cart.style.left = x+'px';
	
	var str="";
	str += cart_summary ;
	str += '<form method="post" action="index.php?modname=coursecatalogue&op=go_cart">';
	str += '<input type="submit" value="'+_LANG._CART_POPUP_GO+'" /> ';
	str += '<input type="button" value="'+_LANG._CART_POPUP_CLOSE+'" onclick="document.body.removeChild($(\'the_cart\'));" />';
	str += '<input type="submit" name="empty_cart" id="empty_cart" value="'+_LANG._CART_POPUP_EMPTY+'" />';
	str += '</form>';
	
	cart.innerHTML = str;

	document.body.appendChild(cart);
}

function openWindowWithAction(id_c, action_to) {
	id_course=id_c;
	var data = "op=" + action_to + "&id_course=" + id_course;
	var objAjax = new Ajax.Request(
        	coursecatalogue_server_url,
        	{method: 'post', parameters: data, onComplete: openWindowWithAction_callback}
    );
}

function report_error(text) {
	
	var w = new Window();
	w.id 		= 'error';
	w.width 	= 600;
	w.title 	= 'Error reporting';
	w.content 	= text;
	w.show();	
}

function openComment(id_c) {
	id_course=id_c;
	var data = "op=comment_it&id_course=" + id_course;
	var objAjax = new Ajax.Request(
        	coursecatalogue_server_url,
        	{method: 'post', parameters: data, onComplete: openWindowWithAction_callback}
    );
}

function openComment_callback(objReq) {
	var parsed = objReq.responseText;
	//alert('stop here please');
	parsed = parsed.evalJSON(true);
	if(parsed == false) {
		report_error(objReq.responseText);
		return;
	}
	
	if (wObjList['course_action']!=null) {
		var w0=$('course_action');
		var el=document.getElementsByClassName("w_content",w0.parentNode);
		el[0].innerHTML=parsed.content;
	} else {	
		var name="course_action";
		var w=new Window();
		w.id 		= name;
		w.width 	= ( parsed.width != undefined ? parsed.width : 600 );
		w.title 	= parsed.title;
		w.content 	= parsed.content;
		w.show();
	}
}

function addajaxcomment() {
	
	var data = "op=addnewcomment" 
		+ "&id_course=" + $F('ajaxcomment_ext_key') 
		+ "&reply_to=" + $F('ajaxcomment_reply_to')
		+ "&text_of=" + $F('ajaxcomment_textof');
	var objAjax = new Ajax.Request(
        	coursecatalogue_server_url,
        	{method: 'post', parameters: data, onComplete: openComment_callback}
    );
}

function delComment(comment_id,id_c) {
	id_course=id_c;
	var data = "op=delcomment" 
		+ "&comment_id=" + comment_id
		+ "&id_course=" + id_course;
		
	var objAjax = new Ajax.Request(
        	coursecatalogue_server_url,
        	{method: 'post', parameters: data, onComplete: openComment_callback}
    );
}

function openWindowWithAction_callback(objReq) {
	
	var parsed = objReq.responseText;
	parsed = parsed.evalJSON(true);
	if(parsed == false) {
		report_error(objReq.responseText);
		return;
	}
	
	var name = "course_action";

	if (wObjList['course_action']!=null) {
		var w0=$('course_action');
		var el=document.getElementsByClassName("w_content",w0.parentNode);
		el[0].innerHTML=parsed.content;
	} else {
		var name="course_action";
		var w=new Window();
		
		
		var new_form = document.createElement('form');
		new_form.method = 'post';
		new_form.action = parsed.next_op;
		
		
		w.id 		= name;
		w.width 	= ( parsed.width != undefined ? parsed.width : 600 );
		
		w.form 		= new_form;
		w.title 	= parsed.title;
		w.content 	= parsed.content
		
		if(parsed.button != undefined) w.buttons = parsed.button;
		w.show();
	}


}

function course_vote(id_course, evaluation) {

	var data = "op=course_vote&id_course=" + id_course + "&evaluation=" + evaluation;
	var objAjax = new Ajax.Request(
        	coursecatalogue_server_url,
        	{method: 'post', parameters: data, onComplete: course_vote_callback}
    );
}

function course_vote_callback(objReq) {
	
	var parsed = objReq.responseText;
	parsed = parsed.evalJSON(true);
	
	$('course_score_' + parsed.id_course).innerHTML = parsed.new_score;
	switch(parsed.evaluation) {
		case "bad" : {
			$('score_image_bad_' + parsed.id_course).src 	= parsed.path_image + 'bad_grey.png';
			$('score_image_good_' + parsed.id_course).src 	= parsed.path_image + 'good.png';
		};break;
		case "good" : {
			$('score_image_bad_' + parsed.id_course).src 	= parsed.path_image + 'bad.png';
			$('score_image_good_' + parsed.id_course).src 	= parsed.path_image + 'good_grey.png';
		};break;
	}
}


function course_dash(link, id_course, elem_id, normal_subs) {
	
	if(link.className == 'show_details_more') link.className = 'show_details_less';
	else link.className = 'show_details_more';
	
	if($(elem_id).innerHTML != '') {
		Effect.toggle(elem_id, 'blind');
	} else {
	
		var data = "op=getdashcourse&id_course=" + id_course + "&elem_id=" + elem_id + "&normal_subs=" + (normal_subs ? 1 : 0);
		var objAjax = new Ajax.Request(
	        	coursecatalogue_server_url,
	        	{method: 'post', parameters: data, onComplete: course_dash_callback}
	    );
		$(elem_id).innerHTML = 'Loading ...';
	}
}

function course_dash_callback(objReq) {
	
	var parsed = objReq.responseText;
	parsed = parsed.evalJSON(true);
	$(parsed.elem_id).innerHTML = '';
	$(parsed.elem_id).innerHTML = parsed.content;
	$(parsed.elem_id).style.display = 'none';
	
	Effect.toggle(parsed.elem_id, 'blind');
	
	$('course_edition_' + parsed.id_course).style.display = 'none'; 
	$('course_edition_' + parsed.id_course + '_close').style.display = 'none';
	
}

