
/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 3.2
 *	LISENSE: FL8XC0FSM5WF - http://www.flynax.com/license-agreement.html
 *	PRODUCT: General Classifieds Software
 *	DOMAIN: oglasi-oglasnik.si
 *	FILE: CURRENCYCONVERTER.INC.PHP
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2011
 *	http://www.flynax.com/
 *
 ******************************************************************************/

/* short form listing grid */
var curConv_sf_dom = '<div class="value curConv">{price}</div>';
var curConv_sf_relation = 'td div.value';

/* listing details page */
var curConv_ld_dom = '<div class="value curConv">{price}</div>';
var curConv_ld_relation = 'td:eq(2)';

/* featured block */
var curConv_fb_dom = '<div class="value curConv">{price}</div>';
var curConv_fb_relation = 'td:eq(2)';

var curConv_img = '<img class="curConv" style="vertical-align: sub;" src="'+curConv_configs['flags_url']+'{code}.gif" alt="" /> ';

$(document).ready(function(){
	curConv_handle();
});

var curConv_handle = function( reset ){
	if ( reset )
	{
		$('.curConv').remove();
	}
	
	if ( !curConv_rates[curConv_currency] )
	{
		/* return if user currency does not exist in imported rates */
		return;
	}
	
	/* browse listings */
	$('#listings fieldset tr:regex(id, sf_field_([0-9]*)_'+curConv_configs['field']+')').each(function(){
		var curLine = curConv_decode_price( $(this).find('div.value').html() );
		
		if ( curLine.currency !== false && curLine.currency != curConv_currency && curLine.price )
		{
			var curRate = curConv_rates[curLine.currency];
			var curDollar = curLine.price/curRate;
			var findRate = curConv_rates[curConv_currency];
			var showRate = curDollar*findRate;
			var out = curConv_encode_price(showRate, curConv_currency) + '<spab class="grey_small" style="font-family: Verdana;font-size: 10px;">'+curConv_lang['converted']+'</span>';
			
			if ( curConv_configs['position'] == 'after' )
			{
				$(this).find(curConv_sf_relation).append(curConv_sf_dom.replace('{price}', out));
			}
			else if ( curConv_configs['position'] == 'before' )
			{
				$(this).find(curConv_sf_relation).prepend(curConv_sf_dom.replace('{price}', out));
			}
			else if ( curConv_configs['position'] == 'right' )
			{
				var right_content = $(this).find(curConv_sf_relation).html();
				$(this).find(curConv_sf_relation).html(right_content+' - '+out);
			}	
		}
	});
	
	/* listing details */
	$('tr#df_field_'+curConv_configs['field']).each(function(){
		var curLine = curConv_decode_price( $(this).find('div.value,div.value_first').html() );
		
		if ( curLine.currency !== false && curLine.currency != curConv_currency )
		{
			var curRate = curConv_rates[curLine.currency];
			var curDollar = curLine.price/curRate;
			var findRate = curConv_rates[curConv_currency];
			var showRate = curDollar*findRate;
			var out = curConv_encode_price(showRate, curConv_currency) + '<span class="grey_small" style="font-family: Verdana;font-size: 10px;">'+curConv_lang['converted']+'</span>';
			
			if ( curConv_configs['position'] == 'after' )
			{
				$(this).find(curConv_ld_relation).append(curConv_ld_dom.replace('{price}', out));
			}
			else if ( curConv_configs['position'] == 'before' )
			{
				$(this).find(curConv_ld_relation).prepend(curConv_ld_dom.replace('{price}', out));
			}
			else if ( curConv_configs['position'] == 'right' )
			{
				var right_content = $(this).find(curConv_ld_relation+' div').html();
				$(this).find(curConv_ld_relation+' div').html(right_content+' - '+out);
			}
			
			if ( curConv_configs['show_flags'] )
			{
				$(this).find('div:not(.curConv).value').html(curConv_img.replace('{code}', curLine.currency.toLowerCase()) + $(this).find('div:not(.curConv).value').html())
			}
		}
	});

	/* random block */
	if ( curConv_configs['page'] == 'home' )
	{
		$('table#listings td div.value').each(function(){
			var curLine = curConv_decode_price( $(this).html() );
	
			if ( curLine.currency !== false && curLine.currency != curConv_currency )
			{
				var curRate = curConv_rates[curLine.currency];
				var curDollar = curLine.price/curRate;
				var findRate = curConv_rates[curConv_currency];
				var showRate = curDollar*findRate;
				var out = curConv_encode_price(showRate, curConv_currency);
				
				if ( curConv_configs['position'] == 'after' || curConv_configs['position'] == 'right' )
				{
					$(this).after(curConv_ld_dom.replace('{price}', out));
				}
				else if ( curConv_configs['position'] == 'before' )
				{
					$(this).find(curConv_ld_relation).prepend(curConv_ld_dom.replace('{price}', out));
				}
				
				if ( curConv_configs['show_flags'] )
				{
					$(this).html(curConv_img.replace('{code}', curLine.currency.toLowerCase()) + $(this).html())
				}
			}
		});
	}
}

var curConv_encode_price = function( str, currency ){
	currency = curConv_configs['show_flags'] ? curConv_img.replace('{code}', currency.toLowerCase()) + currency : '<b>'+currency+'</b>';
	
	str = str.toFixed(2);

	/* convert price to string */
	eval("var converted = '"+ str +"'");
	
	var index = converted.indexOf('.');
	var rest = curConv_configs['show_cents'] ? '.00' : '';
	
	if (index >= 0)
	{
		rest = converted.substring(index);
		
		if ( rest == '.00' && !curConv_configs['show_cents'] )
		{
			rest = '';
		}
		
		converted = converted.substring(0, index);
	}
	
	/* revers string */
	var res = '';
	converted = converted.reverse();
	for(var i = 0; i < converted.length; i++)
	{
		var char = converted.charAt(i);
		res += char;
		var j = i+1;
		if ( j % 3 == 0 && j != converted.length )
		{
			res += curConv_configs['price_delimiter'];
		}
	}
	converted = res.reverse();
	converted = currency+' '+converted+rest;
	
	return converted;
}

var curConv_decode_price = function( str ){
	var out = new Array();
	out['currency'] = false;
	
	str = trim(str);
	
	/* parse currency */
	var pattern = '^([A-Z]{3})?\\s?([0-9\.\,\'\:\;\"]+)\\s?([A-Z]{3})?$';
	var matches = str.match(pattern);
	
	if ( !matches )
	{
		out['currency'] = false;
		return out;
	}
	
	if (matches[1] || matches[3])
	{
		out['currency'] = matches[1] ? matches[1] : matches[3];
		str = matches[2];
	}
	
	var s = new RegExp("\\"+curConv_configs['price_delimiter'], 'gi');
	str = str.replace(s, '');
	str = parseInt(str);
	
	out['price'] = str;
	
	return out;
}

/* custom methods */

if ( typeof(trim) != 'function' )
{
	function trim(str, chars) {
		return ltrim(rtrim(str, chars), chars);
	}
}
 
if ( typeof(ltrim) != 'function' )
{
	function ltrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	}
}

if ( typeof(rtrim) != 'function' )
{
	function rtrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}
}

jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}

String.prototype.reverse = function() {
    var s = "";
    var i = this.length;
    while (i>0) {
        s += this.substring(i-1,i);
        i--;
    }
    return s;
}

