/*
 * Requers global constants
 */

/*max size*/
var MAX_LIST = 3;
/*global object list*/
var list = [];
var link = '';

/* **Constants** below is required */
var URL_FILE_PAGE_FRANCHISE_PROFILE;
var URL_FILE_PLUGIN_STUB;
var cart_item_1;
var cart_item_2;
var cart_item_3;


function compare_add(obj, _no_redraw){
	if (obj.checked){
		/*add to list*/
		if (list.length < MAX_LIST){
			/* add uniq only */
			for ( var i in list ){
				if (list[i].name == obj.name){
					return;
				}
			}

			list[list.length] = obj;

			/*check object...*/
			HighlightItemByID(obj.name, true);
		} else {
			obj.checked = false;
			HighlightItemByID(obj.name, false);
		}
	}else{
		/*remove from list*/
		/*find and remove*/
		for ( var i in list ){
			if (list[i].name == obj.name){
				list.splice(i,1);
			}
		}
		/*find and remove*/

		/*unchek it to!*/
		HighlightItemByID(obj.name, false);
	}

	if (!_no_redraw){
		redraw();
	}
}


function redraw(){
	var str = "";
	var tmp_char = "?";

	link = "";

	/*text list*/
	for ( var i in list )
	{
		/*apply partten*/
		// The link below works only with output filter enabled
		str += '<img src="images/remove.jpg" name="'+ i +'" value="'+ list[i].value +'" onclick="compare_remove('+ i +')" />\
						<a href="' + URL_FILE_PAGE_FRANCHISE_PROFILE + list[i].name + '.html"><span class="txt_newsletter">'+ list[i].value +'</span><br /></a>';
		/*apply partten*/

		/* create get lik */
		link += tmp_char + 'page_compare[' + i + ']=' + list[i].name;
		tmp_char = "&";
		/* create get lik */

	}
	/*out everithing*/

	/*show or hide floating window*/
	document.getElementById('select_out_div_count').innerHTML= '<strong>Selected: </strong>'+ list.length +' of '+ MAX_LIST;
	if (list.length == 0){
		$("#floatdiv").slideUp("slow");
	} else {
		$("#floatdiv").slideDown("slow");
	}

	/*show checks*/
	document.getElementById('select_out_div').innerHTML = str;

	/* request for info - behavior change One Item ajastment */
	if (list.length > 1){
		link_info = URL_FILE_PLUGIN_STUB + link + "&go_form_fri";
	}

	/*anable/disable button*/
	if (list.length > 1){
		$("a[name=floatdiv_go_button]").show("slow");
	}else{
		$("a[name=floatdiv_go_button]").hide("slow");
	}

	/* Send Ajax - str only */
	$.post(
	URL_FILE_PLUGIN_STUB,
	{
		cart_item_1: (list[0] == undefined)? '': list[0].name,
		cart_item_2: (list[1] == undefined)? '': list[1].name,
		cart_item_3: (list[2] == undefined)? '': list[2].name
	}
);

}


function compare_remove(lockal_id){
	list[lockal_id].checked = false;
	HighlightItemByID(list[lockal_id].name, false);

	list.splice(lockal_id,1);
	redraw();
}


function HighlightItemByID(id, check){

	if (check) {
		$("#" + "table_" + id).attr('class', "logolist selectedtocompare");
		$("#checkbox_"+id).attr('checked', "checked");
		//document.getElementById(id).className = "selected_box";
	} else {
		$("#" + "table_" + id).attr('class', "logolist");
		$("#checkbox_"+id).attr('checked', "");
		//document.getElementById(id).className = "unselected_box";
	}
}


/**
 * On start-up
 */
$(document).ready(function(){
	if (cart_item_1)
		compare_add( {name: cart_item_1, value: cart_item_1, checked: true}, true); // No redraw flag
	if (cart_item_2)
		compare_add( {name: cart_item_2, value: cart_item_2, checked: true}, true);
	if (cart_item_3)
		compare_add( {name: cart_item_3, value: cart_item_3, checked: true}, true); 
	redraw();
});


