// This class is responsible for providing easy access to the ajax functions
// available in the fredhopper_ajax_data_provider.php script.

function _AjaxDataProvider() {}

_AjaxDataProvider.prototype.getAllBrandsForMainCategory = function(categoryId, webroot) {
	if(typeof mainCatMenu.cachingCategories == 'undefined')mainCatMenu.cachingCategories = new Array();
	
	var cachingCategory;
	for(var i=0; i<mainCatMenu.cachingCategories.length; i++){
		if(mainCatMenu.cachingCategories[i].catId == categoryId){
			cachingCategory = mainCatMenu.cachingCategories[i];
			break;
		}
	}
	
	if(cachingCategory){
		mainCatMenu.extendBrands(categoryId);
	}else{
		if(typeof mainCatMenu != 'undefined'){
			mainCatMenu.currentCategoryId = categoryId;
			var url = webroot + "/datamodules/datasources/external/fredhopper/fredhopper_ajax_data_provider.php";
			var params = {ajax_function: "get_all_brands_for_main_category", categoryId: mainCatMenu.currentCategoryId, webroot: webroot};
			$.post(url, params, this.getAllBrandsForMainCategoryCallback);		
		}
	}
}

_AjaxDataProvider.prototype.getAllBrandsForMainCategoryCallback = function(data, textStatus) {
	var object = eval(data);
	if(typeof mainCatMenu != "undefined"){
		var new_content;
		new_content = "<h2>Merken</h2>";
		for (var i=0; i<object.brands.length; i++) {
			new_content += "<a href=\"" + object.brands[i].link + "\" onclick=\"" + object.brands[i].tracking + "\">";
			new_content += 		object.brands[i].name;
			new_content += "</a>";
		}
		
		var div					= $('#c_'+ mainCatMenu.currentCategoryId);
		var list 				= div.children("div.list");
		var rc 					= list.children("div.fredhopperdata").children("div.firstRightColumn");
		var orig_html 			= rc.html();
		var orig_list_properties= {width:list.width(), height:list.height()};	
		var orig_rc_properties	= {width:rc.width(), height:rc.height(), innerWidth:rc.innerWidth()};
		var new_html 			= new_content;
		
		var cat = {catId:mainCatMenu.currentCategoryId, div:div, list:list, rc:rc, orig_html:orig_html, orig_list_properties:orig_list_properties, new_html:new_html, orig_rc_properties:orig_rc_properties};
		mainCatMenu.cachingCategories.push(cat);
		mainCatMenu.extendBrands(cat.catId);
	}
}

_AjaxDataProvider.prototype.getAllBrandsForSelection = function(urlParams, webroot, popup_id) {
	this.popup_id = "#" + popup_id.toString();
	this.popup_list = "#brands_popup_list_" + (popup_id.replace(/[^\d]/gi, "")).toString();
	
	//do post only when content of list isn't set yet	
	//if ($(this.popup_list).html() == "") {
		var url = webroot+'/datamodules/datasources/external/fredhopper/fredhopper_ajax_data_provider.php';
		var params = {ajax_function: 'get_all_brands_for_current_selection', urlParams: urlParams, webroot: webroot};
		$.post(url, params, this.getAllBrandsForSelectionCallback);
	//}else{
		//$(this.popup_id).show();
	//}
}

var offset;
var new_height;

_AjaxDataProvider.prototype.getAllBrandsForSelectionCallback = function(data, textStatus) {
	var popup_id = AjaxDataProvider.popup_id;
	var popup_list = AjaxDataProvider.popup_list;
	var title = $(popup_id).children("div:first").children("h4:first").children("span.brands_popup_title");
	
	var object = eval(data);
	var list_content = "";
	var first_part;
	var second_part;
	list_content += "<ul><div class=\"brand-popup-firstCol\">";
	
	first_part = Math.ceil((object.brands.length / 3) - 1);
	second_part = Math.floor(((object.brands.length - parseInt(first_part)) / 2) + first_part);
	
	for(var i=0; i<object.brands.length; i++){
		list_content += "<li class=\"fl\">";
		list_content += 	"<a onclick=\"addToMultiForm(this,'" + object.brands[i].section + "','" + object.brands[i].brandUrl + "','set_fh_multiselect_form_" + object.brands[i].section + "'); " + object.brands[i].tracking + "\"";
		if( object.brands[i].selected ) {
			list_content += "class='active'";	
		}
		list_content += ">";
		list_content += 		object.brands[i].name;
		list_content += 	"</a>";
		list_content += "</li>";
		if(i == first_part) list_content += "</div><div class=\"brand-popup-secondCol\">";
		if(i == second_part) list_content += "</div><div class=\"brand-popup-thirthCol\">";
	}
	list_content += "</div></ul>";
	
	for(var i=0; i<object.brands.length; i++){
		if( object.brands[i].selected ) {
			$('#multi_brands_form').append("<input type='hidden' name='args[" + object.brands[i].section + "][]' value='" + object.brands[i].brandUrl + "' />");
		}
	}
	
	$(title).html(object.title);
	$(popup_list).html(list_content);
	$(popup_id).show();
	offset = $(popup_id).offset();
	if( $('#page_background_gradient').height() <= $(popup_id).outerHeight() + Math.round(offset.top) ) {
		new_height = parseInt( $(popup_id).outerHeight() ) + Math.round(offset.top);
		$('#page_background_gradient').css('cssText', 'height: '+new_height+'px !important').css('min-height', new_height+'px');
	}
}

// Create an instance of the class.
var AjaxDataProvider = new _AjaxDataProvider();

function addToMultiForm(elm, section, brand_url, form) {
	var currentform = $('#multi_brands_form');
	if(!empty(form)) {
		currentform = $('#'+form);
	}
	var removed = false;
	$(elm).removeClass().addClass('active');
	$.each( $('#multi_brands_form input'), function(i,e) {
		if( $(e).val() == brand_url ) {
			$(e).remove();
			$(elm).removeClass();
			removed = true;
		}
	});
	if( removed != true ) {
		currentform.append("<input type='hidden' name='args[" + section + "][]' value='"+brand_url+"' />");
	}
}

function closeAjaxPopup(elm) {
	$(elm).hide();
	$('#page_background_gradient').css('cssText', 'height: auto !important').css('min-height', '100%');
}

function showAllBrands(popup_id_noajax) {
	$('#' + popup_id_noajax).show();
	offset = $('#' + popup_id_noajax).offset();
	if( $('#page_background_gradient').height() <= $('#' + popup_id_noajax).outerHeight() + Math.round(offset.top) ) {
		new_height = parseInt( $('#' + popup_id_noajax).outerHeight() ) + Math.round(offset.top);
		$('#page_background_gradient').css('cssText', 'height: '+new_height+'px !important').css('min-height', new_height+'px');
	}
}

