var preu_ori = 0;

jQuery(function(){
    /*
    jQuery('div.option_1 .step').text('1');
    jQuery('div.option_10 .step').text('2');
    jQuery('div.option_13 .step').text('2');
    jQuery('div.option_14 .step').text('3');
    */

    jQuery('div.product-info div.options div.option_1 input[type=radio], div.product-info div.options div.option_17 input[type=radio]').bind('click',function(){
            jQuery('#tueleccion').html(jQuery(this).parent().text() + ' - ' + jQuery('h1.product_title').text());
            totals( this );
    });

    jQuery('div.product-info div.options div.option_14 input[type=checkbox]').bind('click',function(){
        totals( this );
    });

    jQuery('div.product-info div.cart input[name=quantity]').bind('change',function(){
        totals( jQuery('div.product-info div.options input[type=radio]:checked') );
    });
    
    jQuery('form#language_form').appendTo('div#powered');
    jQuery('form#currency_form').appendTo('div#powered');    

    //jQuery('form#language_form').remove();
    //jQuery('form#currency_form').remove();

    jQuery('div.peu_content').insertAfter('div#content');

    jQuery('div.option_price').each(function(){
        var changeComa = jQuery(this).html();
        changeComa = changeComa.replace(',','.');
        jQuery(this).html(changeComa);
    });

    preu_ori = (parseFloat(jQuery('span#price').text()) > 0) ? parseFloat(jQuery('span#price').text()) : 0;

});


function totals(){
    var preu = 0, tmp, moneda_f = '', moneda_l = '', str_preu;

    jQuery('div.option_price').each(function(){
        if (jQuery(this).parent().find('input').attr('checked') != undefined) {
            //alert(jQuery(this).text());
            str_preu = jQuery(this).text();
            tmp = getPreu( str_preu.replace('.','') );
            if (moneda_f == undefined){
                moneda_f = tmp.monedafirst;
                moneda_l = tmp.monedalast;
            }
            //alert(tmp.preu);
            preu += tmp.preu;
        }
    });

    preu = (preu_ori > 0) ? preu + preu_ori : preu;

    jQuery('span#price').text( moneda_f + ( preu.toFixed(2) * parseFloat(jQuery('input[name=quantity]').val()) ) + moneda_l );

}

function getPreu(cadena) {

	var preu_opcio = jQuery.trim(cadena),
	    moneda_f, moneda_l;

	if ( preu_opcio.charCodeAt(preu_opcio.length-1) > 47 && preu_opcio.charCodeAt(preu_opcio.length-1) < 58){
		moneda_f = preu_opcio.substr(0,1);
		moneda_l = '';
		preu_opcio = preu_opcio.substr(1);
	}else{
		moneda_f = '';
		moneda_l = preu_opcio.substr(preu_opcio.length-1,1);
		preu_opcio = preu_opcio.substr(0,preu_opcio.length-2);

	}
/*
        if (preu_opcio == ''){
		preu_opcio = 0;
		var preu = jQuery('span#price').text();
		if ( preu.charCodeAt(preu.length-1) > 47 && preu.charCodeAt(preu.length-1) < 58){
			moneda_f = preu.substr(0,1);
			moneda_l = '';
		}else{
			moneda_f = '';
			moneda_l = preu.substr(preu.length-1,1);
		}
	}
*/

        return {preu: parseFloat(preu_opcio), monedafirst: moneda_f, monedalast: moneda_l};
	//jQuery('span#price').text( moneda_f + ( parseFloat(preu_opcio) * parseFloat(jQuery('input[name=quantity]').val()) ) + moneda_l );
}

jQuery('#search input').keydown(function(e) {
	if (e.keyCode == 13) {
		moduleSearch();
	}
});

function moduleSearch() {
	url = 'index.php?route=product/search';

	var filter_keyword = jQuery('#filter_keyword').attr('value')

	if (filter_keyword) {
		url += '&filter_name=' + encodeURIComponent(filter_keyword);
	}

//http://cars-experience.com.mialias.net/index.php?route=product/search
//&filter_name=Ferrari&filter_category_id=20&filter_sub_category=true&filter_description=true

	var filter_category_id = jQuery('#filter_category_id').attr('value');

	if (filter_category_id) {
		url += '&filter_category_id=' + filter_category_id;
	}

	//webaporter
	//if (jQuery('#filter_description').attr('checked')) {
		url += '&filter_description=true';
	//}

	url += '&filter_subcategory=true';

	if (jQuery('#filter_model').attr('checked')) {
		url += '&filter_model=true';
	}
	// Fi webaporter

	location = url;
}

