$(function(){
	$.toggleize = function(){
		// toggleize .toggle divs and stuff
		$(".toggle").each(function(index, trigger){
			var trigger = $(trigger);         // the trigger
			var dest_id = trigger.attr('rel');// the destination id
			
			if(typeof dest_id != 'string') return;
			
			var dest    = $('#' + dest_id) // the destination		
			
			if(dest.length == 0) return;
			
			// converts trigger tags into link if the are not so by default
			if(trigger.get(0).tagName != 'A'){
				var new_trigger = $(document.createElement('A'));
				new_trigger.html(trigger.html());
				trigger.html("");
				trigger.removeAttr('rel');
				trigger.append(new_trigger);
				
				var trigger = new_trigger;
			}
			trigger.attr('href', 'javascript:void(0)');
			trigger.attr('rel', dest_id);
			
			// add another image to trigger
			$("<img border='0' class='arrowSmallUp' style='display:none' src='images/arrows_small_up.gif' alt='espandi' width='7' height='7'/>").appendTo(trigger);
			
			// sets callback
			trigger.click(function(){
				var trigger      = $(this);
				var trigger_i    = trigger.children('img.arrowSmall');	
				var trigger_i_up = trigger.children('img.arrowSmallUp');	
				var dest         = $('#' + trigger.attr('rel'));
				
				var status_prev = dest.css('display'); 
				var status_next = status_prev == 'none'? '': 'none';
				
				
				// <<<<<<<< START
				if(!trigger.hasClass('js_initialized')){
					if(!trigger.hasClass('toggle_expanded') && dest.css('display') != 'none'){
						status_next = 'none';
					}else{
						status_next = '';
					}
					trigger.addClass('js_initialized');
				}
				// <<<<<<<< END
				
				if(status_next != 'none'){
					trigger_i.hide();
					trigger_i_up.show();				
				}else{				
					trigger_i_up.hide();
					trigger_i.show();				
				}	
							
				// sets the new status
				status_next == 'none'? dest.fadeOut(300): dest.show(300);	
			});
			
			// added to show elements with a certain class
			trigger.click();					
		});
	};
	
	var Menu = function(opts){
		var obj      = this;
		var defaults = {};
		defaults.cssclass = 'menutrigger';
		defaults.collapsed = true;
		defaults.default_el = undefined;
		defaults.hide_label = 'Chiudi';
		defaults.show_label = 'Espandi';
		defaults.effect_time = 500;
		defaults.max_opened = 1;
		defaults.trigger_type = 'image';
		defaults.trigger_images = {};
		defaults.trigger_images.menu_trigger_1 = {};
		defaults.trigger_images.menu_trigger_1.img1 = APP_PATH + "images/cronaca_istituzionale_act.gif";
		defaults.trigger_images.menu_trigger_1.img2 = APP_PATH + "images/cronaca_istituzionale.gif";
		defaults.trigger_images.menu_trigger_2 = {};		
		defaults.trigger_images.menu_trigger_2.img1 = APP_PATH + "images/documentari_act.gif"
		defaults.trigger_images.menu_trigger_2.img2 = APP_PATH + "images/documentari.gif"
		defaults.trigger_images.menu_trigger_3 = {};		
		defaults.trigger_images.menu_trigger_3.img1 = APP_PATH + "images/programmi_televisivi_act.gif"
		defaults.trigger_images.menu_trigger_3.img2	= APP_PATH + "images/programmi_televisivi.gif"	
		defaults.has_decorator = true;
		defaults.decorator_id_suffix = '_dD';
		defaults.decorator_type = 'image';
		defaults.decorator_image_hide = APP_PATH + 'images/graphic_corner_act.gif';
		defaults.decorator_image_show = APP_PATH + 'images/graphic_corner.gif';
		
		// merge options with defaults
		var opts = $.extend({}, defaults, opts);

		var triggers = $('.' + opts.cssclass);	

		triggers.each(function(index, trigger){
			var trigger = $(trigger);         // the trigger
			var dest_id = trigger.attr('rel');// the destination id	

			// destination id not specified
			if(typeof dest_id != 'string') return;
			
			var dest    = $('#' + dest_id) 		                      // the destination		
			var deco    = $('#' + dest_id + opts.decorator_id_suffix) // decorator
			
			// destination unexistant
			if(dest.length == 0){
				return false;
			}
			
			// sets visibility to none by default
			if(opts.collapsed) dest.hide();		

			// converts trigger tags into link if the are not so by default
			if(trigger.get(0).tagName != 'A'){
				var new_trigger = $(document.createElement('A'));
				// adds the toggle class
				new_trigger.addClass(opts.cssclass);
				new_trigger.html(trigger.html());
				trigger.html("");
				trigger.removeAttr('rel');
				trigger.removeClass(opts.cssclass);
				trigger.append(new_trigger);
				var trigger = new_trigger;
			}
			trigger.attr('href', 'javascript:void(0)');
			trigger.attr('rel', dest_id);
			trigger.attr('title', opts.show_label);

			// sets callback for this trigger
			trigger.click(function(){				
				var trigger   = $(this);				
				obj.toggle(trigger.attr('rel'));		
			});
		});
		
		this.toggle = function(id){
			var trigger     = $('a[rel=' + id + ']')
			var trigger_i   = trigger.find('img');		
			var dest        = $('#' + id);
			var deco        = $('#' + id + opts.decorator_id_suffix) // decorator		
			var status_prev = dest.css('display'); 
			var status_next = status_prev == 'none'? '': 'none';					
			
			// open menu
			if(status_next == ''){
				// close all other opened menus
				$('.js_menu_shown').each(function(){
					obj.toggle($(this).attr('id'));
				});
				
				dest.slideDown(opts.effect_time);
				dest.addClass('js_menu_shown');
			// close menu
			}else{				
				$(trigger_i.get(1)).addClass('js_disable_fade');
				
				dest.slideUp(opts.effect_time);
				dest.removeClass('js_menu_shown');
			}	
			
			trigger.attr('href', 'javascript:void(0)');
			trigger.attr('rel', id);
			trigger.attr('title', opts.show_label);				
			
			// updates trigger and indicator too
			trigger.attr('title', status_next == 'none'? opts.show_label: opts.hide_label);		

			// decorator is an image
			if(opts.trigger_type == 'image'){
				// check if there is an image tag defined into the menu section
				var trigger_im = $('img.channel', trigger);
				
				
				trigger_im.css({ display: status_next == ''? 'none': '' });
			}				
			
			// creates a flexible trigger indicator
			if(opts.has_decorator && deco.length > 0){
				// decorator is an image
				if(opts.decorator_type == 'image'){
					var deco_corner      = $('img.grphCrn', deco);
					var deco_corner_img  = APP_PATH + (status_next == ''? opts.decorator_image_show: opts.decorator_image_hide);
					var deco_corner_img2 = APP_PATH + (status_next == ''? opts.decorator_image_hide: opts.decorator_image_show);
					
					if(deco_corner.length > 0){
						// acts only on first matched element
						$(deco_corner.get(0)).attr('src', deco_corner_img);
						$(deco_corner.get(0)).attr('rel', deco_corner_img2);
					}			
					
					if(opts.decorator_image_show == 'undefined' || opts.decorator_image_hide == 'undefined'){							
						var deco_corner_img  = APP_PATH + trigger_i.attr('rel');
						var deco_corner_img2 = APP_PATH + trigger_i.attr('src');		
					}
					deco_corner.attr('src', deco_corner_img);							
					deco_corner.attr('rel', deco_corner_img2);	
				}							
			}		
		}
		if(typeof opts.default_el != 'undefined'){
			obj.toggle(opts.default_el);
		}
	}
	
	var Scrollize = function(opts){
		var obj      = this;
		var defaults = {};	
		defaults.baseclass = undefined;
		defaults.display = 'inline';
		defaults.next_class = 'next_item';
		defaults.next_class_link = 'next_item_class';
		defaults.prev_class = 'prev_item';
		defaults.prev_class_link = 'prev_item_class';
		defaults.prev_link_html = undefined;
		defaults.next_link_html = undefined;
		defaults.prev_label = 'Indietro';
		defaults.next_label = 'Avanti';
		defaults.spacing = 0;
		defaults.navigation_fade_time = 300;
		defaults.effect_time = 400;
		
		var list = undefined;
		var count = 0;
		var shown_index = 0;
		var offset_width = 0;		
		
		// merge options with defaults
		var opts = $.extend({}, defaults, opts);
		
		if(typeof opts.baseclass == 'undefined'){
			return false;
		}		
		
		// the list object (gets only the first element)
		var list = $('.' + opts.baseclass);
		if(!list.length) return false;
		list = $(list.get(0));
		
		// elements count
		var count = $('.' + opts.baseclass + '_items').children().length;	
		if(!count) return false;		

		this.show = function(index){	
			index = parseInt(index);		
			if(index < count){
				var item = $($('.' + opts.baseclass + '_items_child_' + index, list).get(0));

				shown_index = index;	

				// updates rapid selector, if any
				var selector = $('.' + opts.baseclass + '_selector_child_' + index).get(0);
				if(typeof selector != 'undefined'){
					$('.js_carousel_shown', $('.' + opts.baseclass + '_selector')).each(function(){ $(this).show(); });
					$($(selector).find('img.' + opts.baseclass + '_selector_im').get(0)).hide();
					$($(selector).find('img.' + opts.baseclass + '_selector_im').get(0)).addClass('js_carousel_shown');
				}
				
				// move items horizontally
				var left = (item.outerWidth() + opts.spacing) * index;
				if(left < 0) left = 0;
				
					
				// check if we have to hide the nav links
				var prev_link = $($('.' + opts.baseclass + '_prev_link').get(0));
				var next_link = $($('.' + opts.baseclass + '_next_link').get(0));
				
				if(shown_index > 0){
					prev_link.attr('rel', index - 1);
					prev_link.fadeIn(opts.navigation_fade_time);
				}else{
					prev_link.fadeOut(opts.navigation_fade_time);
				}

				if(shown_index < count -1){
					next_link.attr('rel', index + 1);
					next_link.fadeIn(opts.navigation_fade_time);
				}else{
					next_link.fadeOut(opts.navigation_fade_time);
				}			
				
				$('.' + opts.baseclass + '_items').animate({ left: '-' + left + 'px' }, opts.effect_time);
			}
		};

		this.createTrigger = function(index, sense){
			if(typeof sense == 'undefined')  var sense = 'n';
			// [n]ext, [p]rev
			if(sense != 'n' && sense != 'p') var sense = 'n';
			
			var link = $(document.createElement('a'));
			link.hide(); // hides trigger so it does not flash when the page loads		
			link.attr('href', 'javascript:void(0)');
			link.addClass(opts.baseclass + '_' + (sense == 'n'? 'next': 'prev') + '_link');
			link.attr('title', (sense == 'n'? opts.next_label: opts.prev_label));
			if(index < 0) index = 0;
			link.attr('rel', index);
			
			var label, html_label;
			html_label = (sense == 'n'? opts.next_link_html: opts.prev_link_html);
			
			// creates and append user-defined html label		
			if(typeof html_label == 'undefined'){
				link.prepend(html_label);
			// text label
			}else{
				label = $(document.createElement('span'));
				label.css({ display:  'none' });
				label.append((sense == 'n'? opts.next_label: opts.prev_label));
				link.append(label);
				link.append('&nbsp;');		
			}
			
			if(sense == 'p'){
				link.append(opts.prev_link_html);
				
				link.click(function(){
					rel = parseInt($(this).attr('rel'));
					obj.show(rel);			
				});
			}else if(sense == 'n'){
				link.append(opts.next_link_html);
				
				link.click(function(){
					rel = parseInt($(this).attr('rel'));
					obj.show(rel);	
				});
			}
			
			return link;
		};
		
		// set containers properties
		list.css({ position: 'absolute' });
		// add layout props and margins for next/prev buttons
		$('.' + opts.baseclass + '_wrapper').css({ position: 'absolute', overflow: 'hidden', float: 'left', marginLeft: '0' });
		$('.' + opts.baseclass + '_items').css({   position: 'relative' });
		
		// check for rapid selectors
		var selector = $('.' + opts.baseclass + '_selector');
		if(selector.length){
			// make the selector wrapper visible
			$(selector.get(0)).show();
			
			$(selector.get(0)).children('a').each(function(i, item){
				$(this).addClass(opts.baseclass + '_selector_child_' + i);
				$(this).attr('href', 'javascript:void(0)');
				$(this).attr('rel',  i);
				
				// hide the element by default
				$(this).hide();
				
				$(item).click(function(){
					obj.show($(this).attr('rel'));
				});
			});			
		}
				
		// adapts width of items wrapper and make the childs recognizable by adding some props
		if(opts.display != 'none'){
			
			$('.' + opts.baseclass + '_items').children().each(function(i, child){	
				var offset_width;
				
				$(child).addClass(opts.baseclass + '_items_child_'+i);
				$(child).css({ float: 'left' });

				// add spacing
				if(opts.spacing > 0){
					$(child).css({ marginRight: opts.spacing + 'px' });
				}				

				// get new outer width
				var ow = $(child).outerWidth() + opts.spacing;	
				
				// calculate new container width
				offset_width = ow * (i + 1);
				
				// resize wrapper
				
				//alert('el: ' + i + ' w: ' + offset_width);
				
				$(this).parent().css({ 'width': (offset_width + opts.spacing) + 'px' });
				//$(this).css({ 'width': ow + 'px' });
				
				// check if we have to enable a rapid selector
				var selector = $('.' + opts.baseclass + '_selector_child_' + i);
				if(selector.length == 0) return;
				
				// TEST
				$(window).load(function(){ $(selector.get(0)).fadeIn(2000); });						
			});
		}
		
		// prev
		list_item = $(document.createElement('div'));
		list_item.attr('class', opts.baseclass + '_prev_wrapper');
		list_item.append(obj.createTrigger(shown_index - 1, 'p'));
		list_item.css({ 
			width: '1.4em', 
			height: '9.75em', 
			float: 'left', 
			display: (shown_index == 0? '': 'none')
		});				
		$('.' + opts.baseclass).prepend(list_item);
		
		// next
		list_item = $(document.createElement('div'));
		list_item.attr('class', opts.baseclass + '_next_wrapper');
		list_item.append(this.createTrigger(shown_index + 1, 'n'));
		list_item.css({ 
			width: '1.4em', 
			height: '9.75em',
			marginLeft: '0', 
			float: 'right',
			display: (shown_index < (count -1)? '': 'none')
		});		
		$('.' + opts.baseclass).append(list_item);
		
		// shows first item
		obj.show(0);		
	};	
	
	// extends jquery
	$.extend({
		menu: function(opts){
			var menu = new Menu(opts);
		},
		scrollize: function(opts){
			var scrollize = new Scrollize(opts);
		}
	});
});

var TOGGLE_IMG_SHOW = 'images/arrows_small.gif';
var TOGGLE_IMG_HIDE = 'images/arrows_small_up.gif';

$(function(){
	// applies a specific stylesheet for 800x600 monitors
	if((screen.width * screen.height) <= (800 * 600)){
		$("<link rel='stylesheet' type='text/css' href='style/common_800x600.css'>").appendTo('body');
	}
	
	$('#advSrcTrigger').click(function(){ 		
		var status_next = $('#advSrc').css('display') == 'none'? '': 'none';
		
		
		// focus textbox
		if(status_next != 'none'){
			$('#txtSrc').focus();		
		
		// reset fields
		}else{
			$('#txtSrc').attr('value', '');
			$('#giorno').attr('value', 'GG');
			$('#mese').attr('value', 'MM');
			$('#anno').attr('value', 'AAAA');
			$('#giorno2').attr('value', 'GG');
			$('#mese2').attr('value', 'MM');
			$('#anno2').attr('value', 'AAAA');
		}
	});
	
	// hides codebox by default
	$('#videoCode').hide();
	$('#videoCode').children('input').focus(function(){ $(this).select(); });
	
	$('#linkToCode').attr('href', 'javascript:void(0)');
	
	$('#linkToCode').click(function(e){
		var dest        = $('#' + $(e.target).attr('rel'));
		var status_next = dest.css('display') == 'none'? '': 'none';
		
		if(status_next != 'none'){
			dest.css({ display: 'inline-block' });
			dest.children('input').focus();
			dest.children('input').select();
		}else{
			dest.css({ display: 'none' });
		}
	});
	
	// date focus
	$('#giorno').focus(function(){ if($('#giorno').attr('value') == 'GG') $('#giorno').attr('value', ''); });
	$('#mese').focus(function(){   if($('#mese').attr('value') == 'MM') $('#mese').attr('value', ''); });
	$('#anno').focus(function(){   if($('#anno').attr('value') == 'AAAA') $('#anno').attr('value', ''); });
	$('#giorno2').focus(function(){ if($('#giorno2').attr('value') == 'GG') $('#giorno2').attr('value', ''); });
	$('#mese2').focus(function(){   if($('#mese2').attr('value') == 'MM') $('#mese2').attr('value', ''); });
	$('#anno2').focus(function(){   if($('#anno2').attr('value') == 'AAAA') $('#anno2').attr('value', ''); });	
	// date unfocus
	$('#giorno').blur(function(){ if($('#giorno').attr('value').length <= 0) $('#giorno').attr('value', 'GG'); });
	$('#mese').blur(function(){   if($('#mese').attr('value').length <= 0) $('#mese').attr('value', 'MM'); });
	$('#anno').blur(function(){   if($('#anno').attr('value').length <= 0) $('#anno').attr('value', 'AAAA'); });		
	$('#giorno2').blur(function(){ if($('#giorno2').attr('value').length <= 0) $('#giorno2').attr('value', 'GG'); });
	$('#mese2').blur(function(){   if($('#mese2').attr('value').length <= 0) $('#mese2').attr('value', 'MM'); });
	$('#anno2').blur(function(){   if($('#anno2').attr('value').length <= 0) $('#anno2').attr('value', 'AAAA'); });	
});

// FIX: due to a bug of google chrome this has been added in order to correctly visualize the carousel 
$(window).load(function(){ 
	$('.vList div.box h6, .vList2 div.box h6').css({ 'height': 'auto' }); 
});
