$(document).ready(function(){
	
	$("#ContentRight").append('<a href="http://www.ermesagricoltura.it/" target="_blank"><img src="/images/sapori-valori.jpg" width="280" /></a>');
	
	var $MenuMediaPlaceholder = $('#MenuMediaPlaceholder');
	var $MenuMedia = $('#MenuMedia');
	
	if( $MenuMediaPlaceholder.length ){
		$MenuMediaPlaceholder.append($MenuMedia);
	}
	
	var GrdCesarinesEvents = $("#GrdCesarinesEvents");
	$("#TabBooking").append(GrdCesarinesEvents);


	$("select[name='rg'].service").change(function(e){
		var rg = $(this).val();
		var pr = $("select[name='pr'].service");
		$.ajax({
			type: "GET",
			url: "service.php",
			data: "cmd=provinces.get_by_region&rg="+rg,
			success: function(html){
				var sel = pr.val();
				pr.parent().html(html);
				var prn = $("select[name='pr'].service");
				prn.val(sel);
			}
		});
	});
	$("select[name='rg'].service").trigger('change');
	

	// CONTROLS WIDTH //////////////////////////////////////
	
	var controlw = 280;
	var buttonw = 120;
	$(".Form input[type!='checkbox'][type!='radio']").width(controlw);
	$(".Form input[type='submit']").width(buttonw);
	$(".Form input[type='submit']").addClass('Button');
	$(".Form select").width(controlw);
	$(".Form textarea").width(controlw);
	$(".Row:odd").addClass('Odd');
	$(".Row:even").addClass('Even');
	
	// Auto Anchor
	var anchors = {};
	$("strong").each(function(){
		var t=$(this);	
		var c=t.html();
		if( BeginsWith(c,'##') ){
			var name = c.replace('##','');
			anchors[ToSlug(name)] = name;
		}
	});
	
	anchorItems = '';
	var first = '';
	for( var n in anchors ){
		if( first == '' ) first = n ;
		anchorItems += Format('<li><a href="#{0}">{1}</a></li>',n,anchors[n]);
		newHtml = $('#Content').html().replace('##'+anchors[n],Format('<a href="#">&lt;&lt;</a> <a name="{0}"></a>{1}',n,anchors[n]))
		$('#Content').html(newHtml);
	}
	if( anchorItems != '' ){
		var anchorList = Format('<ul class="Anchors">{0}</ul>',anchorItems);
		$("a[name='"+first+"']").parent().before(anchorList);
	}
	
	// CesarinesDetail
	$("a[rel^='prettyPhoto']").prettyPhoto({autoplay_slideshow: true,slideshow:3000});

	
	// CalendarEvent Click
	$(".CalendarEvent").each(function(){
		var t=$(this);
		var href = t.find("a").attr("href");
		t.click(function(e){ window.location=href; });
		t.css("cursor","pointer");
	});
	
	$('.SeparatorPipe a').each(function(){
		var t=$(this);
		
		if( t.is(':first-child') ) return;
		
		t.before(' | ');
		
	});
	
	$("div[id^='Tab']").each(function(){
		var t = $(this);
		var id = '#'+t.attr('id');
		var v = Trim(t.html());
		if( v == '' )
		{
			$(id).hide();
			$("a[href='"+id+"']").parent().hide();
		}
	});
	
	var showMargins = 0;
	
	$('div').each(function(){
		
		if(!showMargins) return;
		
		var t=$(this);
		var id = t.attr('id');
		var cls = t.attr('class');
		
		var cond1 = false;
		var cond2 = false;
		
		cond1 = cond1 || cls == 'Title';
		cond1 = cond1 || cls == 'Text';
		cond1 = cond1 || cls == 'Location';
		cond1 = cond1 || cls == 'Link';
		cond1 = cond1 || cls == 'Video';
		cond1 = cond1 || cls == 'Picture';
		
		cond2 = cond2 || cls == 'Label';
		cond2 = cond2 || cls == 'Value';
		
		if( t.hasClass('Block') || t.hasClass('Container') )
		{
			var w = t.width();
			var h = t.height();
			var nw = w-2 ;
			var nh = h-2 ;
			t.width(nw);
			t.height(nh);
			t.css('border','1px solid red');
			
			var top = '0px';
			
			if( t.hasClass('Top10') )
			top = '10px';
			else if( t.hasClass('Top20') )
			top = '20px';
			else if( t.hasClass('Top30') )
			top = '30px';
			else if( t.hasClass('Top40') )
			top = '40px';
			
			t.prepend('<div style="color:red; position:absolute; margin-top:'+top+'">'+id+' ('+w+','+h+')</div>');
		}
		else if(cond1)
		{
			t.css('border','1px dashed red');
		}	
		else if(cond2)
		{
			t.css('border','1px dashed black');
		}	
	});
});
