/**
 * Uhlsport productfinder.js
 * XML Generator für Flash
 * -------------------------------------------------------------------------
 * @author Weitclick
 * @copyright Copyright(C) 2009, Weitcklick
 * @version $Id: productfinder.js 22 2009-02-23 13:52:40Z ma $
 * @revision $LastChangedRevision:0 $
 **/

var who=0;
var type=0;
var color=0;
var when=0;
var where=0;
var loading=false;

function InitSub() {
	$('img').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false, 
	    showBody: " - ", 
	    fade: 250 
	});
					
	
	$('#nav_buttons>a>button>div').css('line-height', '16px');
	if(lang == 'fr' || lang == 'es') {
		$('#nav_buttons>a>button>div').css('width', '180px');
	} else {
		$('#nav_buttons>a>button>div').css('width', '107px');
	}
}

function Loader() {
	// Firefox u.a. lädt Bilder auch wenn das Element leer gemacht wird, IE führt nach einem Stop nichts mehr aus
	if($.browser.name != "msie") { 
		stop(); 
	}
	$('#productfinder-loader>strong').html(text_loading);
	$('#productfinder-products').html('');
	if(!loading) {
		loading = true;
	
		var attributes_get = '&who=' + who + '&type=' + type + '&color=' + color + '&when=' + when + '&where=' + where + '&lang=' + lang;
		var url = '/fileadmin/uhlsport/php/productfinder_ajaxLoader.php?' + attributes_get;
		
		$.getJSON(url, function(json){
			$('#productfinder-loader>strong').html(json.products.count+' <span id="text_ajaxLoading">'+text_ajaxLoading+'</span>');
			
			$.each(json.attributes, function(index, attribute){
				$('#att' + attribute.element).attr('src', $('#att' + attribute.element).attr('src').replace(/(norm|disabled|active)/i, attribute.status));
				$('#att' + attribute.element).attr('class', attribute.status);
			});
			
			RenderProducts(json.products, 0, 0);

			loading = false;
		});
	}
}

function RenderProducts(array, offset, limit) {
	if(!offset) {
		offset = 0;
	}
	
	if(!limit) {
		limit = array.count;
	} else if(limit > offset + array.count) {
		limit = array.count;
	}
	
	var size = array.size;
	if(!size) {
		size = 40;
	}
	
	for(var x=offset; x < limit; x++) {
		var product = array[x];
		$('#productfinder-products').append('<img src="/fileadmin/templates/media/products/'+size+'x'+size+'/'+product.im+'" width="'+size+'" height="'+size+'" title="'+product.na+'" onclick="G('+product.it+');" />');
	}
	
	$('img').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false, 
	    showBody: " - ", 
	    fade: 250 
	});
}

function G(item) {
	window.location.href = '/go-product/?itemnum='+item+'&country='+lang;
}

function SetShowall() {
	who = 0;
	type = 0;
	color = 0;
	when = 0;
	where = 0;
	Loader();
}

function SetAttribute(name, code, element) {
	if($('#'+element).attr('class') != 'disabled') {
		if(eval(name+'=='+code)) {
			eval(name+'=0');	
		} else {
			eval(name+'='+code);
		}
		
		Loader();
	}
}
