var info = {
	show: function (divId, response)
	{
		$("#"+divId).empty();
		$.each(response, function(i, v) {
			$("#"+divId).append("<div class=\""+ v.type +"\">"+v.msg+"</div>");
			if(v.redirect != undefined) window.location = v.redirect;
			if(v.javascript != undefined) eval(v.javascript);
		});
	}
};

var page = {
	show: function (hash, id)
	{
		if(hash) {
			hash = hash.replace('#', '');
			$.ajax({
				url: hash,
				cache: true,
				beforeSend: function () {
					$(id).empty();
				},
				success: function(html) {
					$(id).html(html);
					$('.tipsy').remove();
					$('.tip').tipsy({gravity: 's'});
				}
			});
		}
	}
};

var brands = {
	vote: function (brand_id, rating)
	{
		$.ajax({
			url: "/brands/rate/"+brand_id,
			type: "POST",
			dataType: "html",
			data: ({rating: rating}),
			success: function(data){
				classs = 'bla';
				
				if(data < 0) {
					classs = 'minus';
				}
				
				$(".voting_"+brand_id).empty().append('<div class="rating '+classs+'">'+data+'%</div>');
			}
		});
		return false;
	}
};

$(document).ready(function () {

	$("#loader").ajaxStart(
		function(){
			$(this).show();
	}).ajaxComplete(
		function(){
			$(this).hide();
		}
	);
	
	$('.tip').tipsy({gravity: 's'});
	$('.tipr').tipsy({gravity: 'w'});
	$('.tipl').tipsy({gravity: 'e'});
	
	/*
	 * HISTORY
	 */
	id = '#ajaxContent';
	$.ui.history('enable', {
		id: id
	});
	
	
	$('table').colorize( {hover:'cross', click:'cross', oneClick:true} );
	$("table").floatHeader({ 
		fadeIn: 250,   
		fadeOut: 250  
	});
	 
	if($("#amount").length)
	{
		var select_amount = $("#amount");
		var slider_amount = $('<div id="slider-amount"></div>').insertAfter(select_amount).slider({
			min: 1,
			max: $("#amount option").length,
			range: "min",
			value: select_amount[0].selectedIndex + 1,
			slide: function(event, ui) {
				select_amount[0].selectedIndex = ui.value - 1;
			},
			stop: function(event, ui) {
				doSubmit($('#calculatorForm'));
			}
		});
		$("#amount").change(function() {
			slider_amount.slider("value", this.selectedIndex + 1);
			doSubmit($('#calculatorForm'));
		});
		
		var select_days = $("#days");
		var slider_days = $('<div id="slider-days"></div>').insertAfter(select_days).slider({
			min: 1,
			max: $("#days option").length,
			range: "min",
			value: select_days[0].selectedIndex + 1,
			slide: function(event, ui) {
				select_days[0].selectedIndex = ui.value - 1;
			},
			stop: function(event, ui) {
				doSubmit($('#calculatorForm'));
			}
		});
		$("#days").change(function() {
			slider_days.slider("value", this.selectedIndex + 1);
			doSubmit($('#calculatorForm'));
		});
	}
	
	$('#calculatorForm').submit(function() {
		doSubmit(this);
		return false;
	});
	
	$("#orderby_amount").change(function() {
		doSubmit($('#calculatorForm'));
	});
	
	$("#orderby_percent").change(function() {
		doSubmit($('#calculatorForm'));
	});
});

function doSubmit (obj) {
	$(obj).ajaxSubmit({
		beforeSubmit:  showRequest
	});
	return false; 
}

function showRequest(formData, jqForm, options) {
	var action = $(jqForm).attr("action");
	var hash = "/"+action+"?"+$.param(formData);
	window.location = '#'+hash;
	page.show(hash, "#ajaxContent");
	pageTracker._trackPageview();
    return false; 
} 
