var baseUrl = location.protocol+'//'+location.host+'/public/';
var col_num, feat_num;
function addFeature(num) {
	if (num && !feat_num || num > feat_num) {feat_num = num;}
	var html = '';
	html += '<dt><label for="name'+feat_num+'" class="optional">Name</label></dt>';
	html += '<dd><input type="text" name="name'+feat_num+'" id="name'+feat_num+'" value="" class="floated" /></dd>';
	html += '<dt><label for="description'+feat_num+'" class="optional">Description</label></dt>';
	html += '<dd><textarea name="description'+feat_num+'" id="description'+feat_num+'" class="mceEditor simple"></textarea></dd>';
	$('#fieldset-f').append(html);
	replaceTextAreas();
	feat_num++;
	return false;
}
function alterDetails(str) {$('#details div').fadeOut('fast'); $('#'+str).fadeIn('fast');}
function authorizeComment(obj, comment_id) {jQuery.post('/comments/authorize', {'id':comment_id, 'value':obj.checked}, function(data){if (data == 'Successfully authorized this comment') {alert(data);} else {alert("I'm sorry something went wrong. Comment not authorized");}});}
function calculateLineTotal(obj) {
	var num = obj.id.match(/[0-9]{1,}/);
	var line_total = 0;
	line_total = (parseInt(obj.value)*parseFloat(document.getElementById('price_'+num).value));
	document.getElementById('linetot_'+num).value = number_format(line_total, 2, '.', '');
	updateTotals();
}
function changeSrc(obj, str) {
	var split = obj.id.match(/\d/g);
	for (var i=1; i<6; i++) {
		if (i<=split[1]) {
			$('#'+split[0]+'-'+i).attr('src', baseUrl+'images/icons/star_'+str+'.png');
		} else {
			$('#'+split[0]+'-'+i).attr('src', baseUrl+'images/icons/star_off.png');
		}
	}
}
function createCookie(name, value, days) {
	if (days) {var date = new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires = "; expires="+date.toGMTString();} else {var expires = "";}
	document.cookie = name+"="+value+expires+"; path=/";
}
function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, prec = decimals;
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep == "undefined") ? ',' : thousands_sep;
    var dec = (typeof dec_point == "undefined") ? '.' : dec_point;
    var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
    var abs = Math.abs(n).toFixed(prec);
    var _, i;
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {s = s.replace('.', dec);}
    return s;
}
function rateThis(obj) {
	var split = obj.id.match(/\d/g);
	$.post('/ratings/rateproduct', {'id': split[0], 'value': split[1]}, function(data) {alert(data); document.location = '/ratings/create';});
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {c = c.substring(1, c.length);}
		if (c.indexOf(nameEQ) == 0) {return c.substring(nameEQ.length, c.length);}
	}
	return null;
}
function removePadding() {
	var dts, next;
	dts = $('DT');
	for (var i=0; i<dts.length; i++) {
		if ($(dts[i]).html() != '&nbsp;') {continue;}
		if ($(dts[i]).next().html().match(/.*(type="submit").*/)) {continue;}
		$(dts[i]).remove();
	}
}
function replaceTextAreas() {
	var txtar = $("textarea");
	for (var i=0; i<txtar.length; i++) {
		var oFCKeditor = new FCKeditor(txtar[i].id);
		oFCKeditor.BasePath = baseUrl+"javascript/fckeditor/" ;
		oFCKeditor.Height = 300;
		oFCKeditor.Width = 600;
		if (txtar[i].className.match(/.*(mceEditor).*/)) {
			if (txtar[i].className.match(/.*(simple).*/)) {
				oFCKeditor.Height = txtar[i].style.height;
				oFCKeditor.ToolbarSet = 'Basic';
				oFCKeditor.Width = txtar[i].style.width;
			}
			oFCKeditor.ReplaceTextarea();
		}
	}
}
function showItem(elem_id) {
	var digit, pos;
	digit = elem_id.match(/\d{1,}/);
	pos = $("#"+elem_id).parent().parent().position();
	$('#description').html($('#'+digit).html());
	$('#description').css({
			'background-color': '#FFFFFF',
			'border': '1px dotted #959595',
			'left': pos.left,
			'padding': '5px',
			'position': 'absolute',
			'top': pos.top+3,
			'width': '930px',
			'z-index': 3
	});
	$('#description').fadeIn(2000);
}
function updateTotals() {
	var ln_tots = $('input[id^=linetot]');
	var sub = 0;
	for (var i=0; i<ln_tots.length; i++) {if ($(ln_tots[i]).val()) {sub += parseFloat($(ln_tots[i]).val());}}
	document.getElementById('subtotal').value = number_format(sub, 2, '.', '');
	document.getElementById('vat').value = number_format(0, 2, '.', '');
	document.getElementById('total').value = number_format(sub, 2, '.', '');
}
$(document).ready(function() {
		removePadding();
		replaceTextAreas();
		$("a[rel^='lightbox']").slimbox();
		$("ul.piped li:last-child").css({'border':'none'});
		$('input[type="image"]').bind('click', function(e) {return false;});
		$('input[id$="date"]').bind('click', function(e) {$(this).datepicker({dateFormat: 'yy-mm-dd'});});
		var req = $('.required');
		for (var i=0; i<req.length; i++) {$(req[i]).html($(req[i]).html()+' *');}
		$('.corn_all').corner();
		jQuery('.corn_all_big').corner('25px');
		var pageTracker = _gat._getTracker("UA-5113521-1");
		pageTracker._initData();
		pageTracker._trackPageview();
});
