/**
 * X3 CMS - A smart Content Management System
 *
 * @author		Paolo Certo
 * @copyright	(c) 2010-2011 CBlu.net di Paolo Certo
 * @license		http://www.gnu.org/licenses/agpl.htm
 * @package		X3CMS
 */
 
// external links
$(function() {
    $('a[href^=http]').not('.no_target').click( function() {
        window.open(this.href);
        return false;
    });
});


$(function() {
    $('a.blank').click( function() {
        window.open(this.href);
        return false;
    });
});

// preload images
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}

$.fn.rating = function() {
	this.each(function(){
		$(this).rateit({max: 5, step: 1});
	});
}


var c = 0;

$(document).ready(function() {
	
	$('#antispam').remove();
    $('a[href^=http]').not('.no_target').addClass('blank');
	$('<img/>').preload();
	
	rate();
	recaptcha();
	sliding();
	fixedMenu();
	
});

// reload captcha
var recaptcha = function() {
		$('#reload_captcha').click(function() {
			c = c + 1;
			var src = $('#reload_captcha').attr('href');
			d = new Date();
			$("#captcha_img").attr("src", src + '/' + c + '?' + d.getTime());
			return false;
		});
	},
	sliding = function() {
		if(!$("#invoice").attr("checked")) {
			$("#invoicing").fadeOut('slow');
		}
		$("#invoice").change(function(){
			$("#invoicing").slideToggle(500)
		});
	},
	rate = function() {
		$('.rating').rating();
		$('.rating').bind('rated', function (e) {
			var ri = $(this);
			
			//if the use pressed reset, it will get value: 0 (to be compatible with the HTML range control), we could check if e.type == 'reset', and then set the value to  null .
			var value = ri.rateit('value');
			var id = ri.data('productid'); // if the product id was in some hidden field: ri.closest('li').find('input[name="productid"]').val()
			
			//maybe we want to disable voting?
			ri.rateit('readonly', true);
			
			// load
			$('#poll_' + id).load(root + '/call_plugin/x3pro/2/rate/' + value + '/' + id, 
				function() {
					rate();
				}
			);
			return false;
		});
	},
	fixedMenu = function(){
		$('#nav ul').css({display: 'none'}); // Opera Fix
		$('#nav li').hover(
			function(){
				$(this).find('ul:first').css({visibility: 'visible',display: 'none'}).show(400);
			},function(){
				$(this).find('ul:first').css({visibility: 'hidden'});
			}
		);
	}

