/*
(function($){
	$.extend({ dequeue : function(elem, effect) {
		$(elem).dequeue(effect);
	}
	});
})(jQuery);
*/
var selectingAllRates = false;
var disableAllLinks = false;

$(document).ready(function() {
	//*
	var ok = true;
	var bVer = parseInt($.browser.version);
	if($.browser.msie) {
		//if(bVer < 7) {
		 ok = false;
		//}
	}
	if(ok) {
	//*
		$(".icon_box img").hover(function() {
			var e = this;
		    $(e).stop().animate({ marginTop: "-7px" }, 250, function() {
		    	$(e).stop().animate({ marginTop: "-5px" }, 250);
		    });
		},function(){
			var e = this;
		    $(e).stop().animate({ marginTop: "0px" }, 250, function() {
		    	$(e).stop().animate({ marginTop: "0px" }, 250);
		    });
		});
	//*/
	}
	
	
	$("img.off_icon").hover(function() {
		this.src = this.src.replace('/off/', '/'+on_base_dir+'/');
	}, function() {
		if($(this).hasClass('off_icon')) {
			this.src = this.src.replace('/'+on_base_dir+'/', '/off/');
		}
	}).click(highlight_icon);

	$("img.off_icon_ex").hover(function() {
		this.src = this.src.replace('/off/', '/'+on_base_dir+'/');
	}, function() {
		if($(this).hasClass('off_icon_ex')) {
			this.src = this.src.replace('/'+on_base_dir+'/', '/off/');
		}
	}).click(select_icon);
	
	$('.tooltipIcon').hover(function() {
		var tt = $(this).parent().find('.bigTooltip');
		if($.browser.msie) {
			tt.show();
			if($.browser.version <= 6) {
				var c = $('<div class="bigTooltip blueTooltip">'+tt.html()+'</div>');
				var o = tt.offset();
				tt.hide();
				//alert(o.left+" - "+o.top);
				c.css({
					position: 'absolute',
					left: o.left,
					top: o.top - 40
				});
				$(document.body).append(c);
				c.show();
				tt[0].cloneTT = c;
				//c.pngFix();
			} else {
			}
		} else {
			tt.show();
			//tt.fadeIn();
		}
	}, function() {
		var tt = $(this).parent().find('.bigTooltip');
		if($.browser.msie) {
			if($.browser.version <= 6) {
				tt[0].cloneTT.hide();
				tt[0].cloneTT = null;
			} else {
				tt.hide();
			}
		} else {
			tt.hide();
			//tt.fadeOut();
		}
	});
	
	//*/
	
	$('.sosicon').hover(show_tooltip, hide_tooltip);
	
	//*
	$('tr.result').each(function() {
		try {
			if(this.onclick != null) {
				var goto_link = this.onclick;
				$(this).find('td:not(:first)').click(goto_link);
				this.onclick = null;
			}
		} catch(e) {}
	});
	//*/
	
	//*
	$('.result input[type=checkbox]').click(function() {
		var tr = $(this).parent().parent();
		if(this.checked) {
			if(tr.hasClass('odd') || tr.hasClass('even')) {
				tr.addClass('selected-row');
				tr.next().addClass('selected-row');
			} else if(tr.hasClass('odd2') || tr.hasClass('even2')) {
				tr.addClass('selected-row2');
				tr.next().addClass('selected-row2');
			} else if(tr.hasClass('odd4') || tr.hasClass('even4')) {
					tr.addClass('selected-row4');
					tr.next().addClass('selected-row4');
			}
		} else {
			tr.removeClass('selected-row');
			tr.next().removeClass('selected-row');
			tr.removeClass('selected-row2');
			tr.next().removeClass('selected-row2');
			tr.removeClass('selected-row4');
			tr.next().removeClass('selected-row4');
		}
	});
	//*/
	
	$('.result').hover(function() {
		//$(this).css('background-color', '#F3FFF1');
	}, function() {
		//$(this).css('background-color', '');
	});
	
	try {
		$('input[type=checkbox],input[type=radio]').prettyCheckboxes();
	} catch(e) {}
	
	
	//IE6 rows hover
	/*
	$('.result').hover(function() {
		$(this).css('background-color', '#F3FFF1');
	}, function() {
		$(this).css('background-color', '');
	});
	//*/
	
	$('.rate_details_link').click(function() {
		disableAllLinks = true;
	});
});

function highlight_icon() {
		this.src = this.src.replace('/off/', '/'+on_base_dir+'/');
		$(this).removeClass('off_icon');
		$(this).addClass('on_icon');
		$(this).unbind('click');
		$(this).bind('click', function() {
			this.src = this.src.replace('/'+on_base_dir+'/', '/off/');
			$(this).removeClass('on_icon');
			$(this).addClass('off_icon');
			$(this).unbind('click');
			$(this).bind('click', highlight_icon);
			//return false;
		});
		//return false;
}

function select_icon() {
	var href = $(this).parent().parent().attr('href');
	if(href == null || href.length() <= 0) return;
	unselect_all($(this).parent().parent().parent());
	
	this.src = this.src.replace('/off/', '/'+on_base_dir+'/');
	$(this).removeClass('off_icon_ex');
	$(this).addClass('on_icon');
	$(this).unbind('click');
	$(this).bind('click', function() { return unselect_icon(this); });
	//return false;
}

function unselect_icon(elem) {
	//alert(elem);
	elem.src = elem.src.replace('/'+on_base_dir+'/', '/off/');
	$(elem).removeClass('on_icon');
	$(elem).addClass('off_icon_ex');
	$(elem).unbind('click');
	$(elem).bind('click', select_icon);
	//return false;
}

function unselect_all(elem) {
	elem.find('.icon').each(function() {
		unselect_icon(this);
	});
}

function show_tooltip() {
	var text = $(this).parent().parent().find('.icon_label').text();
	var tt = $('<div class="tooltip">'+text+'</div>');
	$(this).parent().parent().append(tt);
}

function hide_tooltip() {
	$(this).parent().parent().find('.tooltip').remove();
}

function startADSLCheck() {
	var ndCell = $('.disp_cell');
	ndCell.each(function() {
		var cell = this;
		$(cell).html('<img src="/images/ajax-loader.gif" alt="Caricamento..." />');
		var cid = cell.id.replace('disp_cell_', '');
		var num = $('#numtel').val();
		var pre = $('#prefix').val();
		//*
		$.get('grab.php', { t: cid, n: num, p: pre }, function(res) {
			res = parseInt(res);
			var t = null;
			switch (res) {
				case -1://ND
					t = "N.D.";
					break;
				case 0:
					t = '<span style="color: #ff0000">NO</span>';
					break;
				case 1:
					t = '<span style="color: #008800">SI</span>';
					break;
			}
			$(cell).html(t);
		});
		//*/
	});
}

function openRate(url) {
	if(!selectingAllRates) {
		location.href=url;
	}
}

function openRefer(url) {
	if(!selectingAllRates && !disableAllLinks) {
		window.open(url);
	} else {
		disableAllLinks = false;
	}
}

function selectAllRates(val) {
	selectingAllRates = true;
	$('input[type=checkbox].compareCheckbox:not(#c0)').each(function() {
		var oldVal = this.checked;
		this.checked = val;
		if(oldVal != val) {
			$('label[for='+this.id+']').trigger('click');
		}
	});
	selectingAllRates = false;
}

function checkComparedRates() {
	var tot_rates = 0;
	$('input[type=checkbox].compareCheckbox:not(#c0)').each(function() {
		if(this.checked) tot_rates++;
	});
	if(tot_rates < 2) {
		alert('Seleziona almeno 2 tariffe da confrontare');
		return false;
	}
	return true;
}