/*--------------------------------- page ----------------------------------------*/
var page = {};

page.settings = {
	loader : '<img src="' + rootPath + '/img/site/ajax-loader.gif" style="margin:10px 0 0 10px"/> <span style="color:#616161;">Caricamento in corso... attendere.</span>',
	loaderWhite : '<img src="' + rootPath + '/img/site/ajax-loader-white.gif" style="margin:10px 0 0 10px"/>'
}

page.setTabCarousel = function(){

	var pageTabCarousel = new tabCarousel(); 

	$('#tabCarousel').jcarousel({
       scroll:1,
       start: 1,
       initCallback: pageTabCarousel.init,
       animation: 400
  	});	

}

page.setGallery = function(options){
	setTimeout(function(){
		var options = options || {thumbCol : 2, scroll : 2};
			
		pageGallery = new gallery($('#gallery a'),options); 
	    
		$('#gallery').jcarousel({
	        vertical: true,
	        scroll: options.scroll,    
	        initCallback: pageGallery.init,
	        buttonNextCallback : function(a,b){},
	        buttonPrevCallback : function(a){}
	    });		
	}, 500);
}

page.setSearchActions = function(){
	
	$('.search-colors a').click(function(e){
		e.preventDefault();
		var href = $(this).attr('href');
		var monitor = $('#dialogcolors');
		monitor.html(page.settings.loader);
		monitor.dialog({
			bgiframe: true,
			height: 550,
			width: 500,
			modal: true,
			close: function(){
				monitor.dialog('destroy');
			}
		});		
		$.ajax({
			url : href,
			cache:false,
			dataType : 'html',
			success : function (data, textStatus) {
				monitor.html(data);
			}
		});
		
	})
	
	$('.search-texture a').click(function(e){
		e.preventDefault();
		var href = $(this).attr('href');
		
		var dialogID = $(this).attr('dialog');
		var monitor = $('#'+dialogID);
		monitor.html(page.settings.loader);
		monitor.dialog({
			bgiframe: true,
			height: 550,
			width: 500,
			modal: true,
			close: function(){
				monitor.dialog('destroy');
			}
		});
		
		$.ajax({
			url : href,
			cache:false,
			dataType : 'html',
			success : function (data, textStatus) {
				monitor.html(data);
			}
		});
	})

}

page.setSuperficiDialog = function(){

		$('a.supColori').click(function(e){
		e.preventDefault();
		var href = $(this).attr('href');
		var monitor = $('#dialog');
		monitor.dialog({
			bgiframe: true,
			height: 550,
			width: 500,
			modal: true,
			close: function(){
				monitor.dialog('destroy');
			}
		});		
		
	})

	
}

page.setPercorsiInCeramica = function(){

	$('#percorsiInCeramica').change(function(){
		var content = $(this).val();
		var node = $(this).attr('rel');
		var href = rootPath + '/index.cfm/12,html'
		var monitor = $('#ajaxMonitor');
		monitor.html(page.settings.loaderWhite);
		if (content != 0) {
			$.ajax({
				url: href,
				data: {
					nxt: 'contentBody',
					content: content,
					node : node
				},
				cache: false,
				dataType: 'html',
				success: function(data, textStatus){
					monitor.html(data);
				}
			})
			
		}
	})
}

page.setBaloons = function(){
	$(document.body).click(function(){
		$('.baloon').hide();
		$('.baloon').parent('li.selected').removeClass('selected');
	});
	$('.baloon-handle').click(function(){
		var el = $(this);
		var id = el.attr('id').replace('for-', '');
		setTimeout(function(){
			$('#'+id).show();
			el.parent('li').addClass('selected');
		}, 10);
	});

}	
function fractionsInches(intero,frazione,pollici){
	var yeah=false;
	function gdc(a,b){
		while(1){
			a = a % b;
			if(a==0){
				return b;	
			}
			b = b% a;
			if(b==0){
				return a;
			}
		}
	}
	
	function nextNearest(value, number) {
	  var ceil = Math.ceil(value);
	  var remainder = value % number;
	  if (remainder > 0)
	    value = value - remainder + number;
	  return value;
	}
	
	function prevNearest(value, number) {
	  var ceil = Math.ceil(value);
	  var remainder = value % number;
	  if (remainder > 0)
	    value = value - remainder ;
	  return value;
	}
	
	function findDec(support,frazione){
		if (support != 0) {
			for (i = 1; i <= support; i++) {
				if ((i/support) == frazione) {
					return i/gdc(i,support) + "/" + support/gdc(i,support);
					yeah = true;
				}
			}
			if (yeah != true) {
				findDec(support/2, frazione);
			}
		}
	}
	try{
		var risultato = findDec(16,frazione);
		if (risultato==undefined){
			throw(err);
		}
		document.write(intero + " " + risultato);
	}
	catch(err){
		var near1 = Math.abs(pollici - nextNearest(pollici,.25));
		var near2 = Math.abs(pollici - prevNearest(pollici,.25));
		var uberNear=Math.min(near1, near2);
		
		if(uberNear==near1){
			var scarto = Math.abs(intero - nextNearest(pollici,.25));
			
			if(scarto != 0 && scarto != 1){
				document.write(intero + " " + findDec(16,scarto));
			}else{
				document.write(Math.round(pollici));
			}
		}else{
			var scarto = Math.abs(prevNearest(pollici,.25)-intero);
			if(scarto != 0 && scarto != 1){
				document.write(intero + " " + findDec(16,scarto));
			}else{
				document.write(Math.round(pollici));
			}
		}
	}
}

