var content_menu_home = {
	
	init : function() {
		content_menu_home.hideGroups(0);
		
		jQuery('#menu .content-menu-home .tabs li a').each(function(index) {
			jQuery(this).bind('click', function(event) {
				content_menu_home.hideGroups(index);
				event.preventDefault();
			});
		});
	},
	
	hideGroups : function(index) {
		jQuery('#menu .content-menu-home .tabs li').each(function(i) {
			if (i != index) {
				jQuery('#'+ jQuery(this).removeClass('current').find('a').attr('href').split('#')[1]).hide();
			} else {
				jQuery('#'+ jQuery(this).removeClass('current').addClass('current').find('a').attr('href').split('#')[1]).show();
			}
		});
	}
	
};

var opentarget = {

	init : function() {
		jQuery('a.opentarget').each(function() {
			var element = jQuery(this);
			var target = element.attr('href');
			
			jQuery(target).hide();

			element.bind('click', function(e) {
				jQuery(element.attr('href')).slideToggle('slow');

				e.preventDefault();
			});
		});
	}
	
};

var auto_submit = {
	
	init : function() {
		jQuery('.auto-submit .submit').css({
			display: 'block',
			height: '1px',
			left: '-999em',
			overflow: 'hidden',
			position: 'absolute',
			top: '-999em',
			visibility: 'hidden',
			width: '1px'
		});
		jQuery('.auto-submit select').bind('change', function(e) {
			jQuery(this).parents('.auto-submit').find('.submit').click();
		});
	}
	
};

var content_menu_home_calls = {
	
	init : function() {
		jQuery('body.home #menu .content-menu-home #calls .actions ul li ul').hide();
		
		jQuery('body.home #menu .content-menu-home #calls .actions ul li strong').bind('click', function(event) {
			jQuery(this).next('ul').toggle('fast')
		});
	}
	
};

var dropdown = {
	
	init : function() {
		jQuery('.dropdown .target:not(:first)').hide();
		
		jQuery('.dropdown .bind').bind('click', function(event) {
			jQuery('.dropdown .target').hide('fast');
			jQuery(this).next('.target').toggle('fast');
		}).css('cursor','pointer');
	}
	
};

var tabs = {
	
	init : function() {
		var index = jQuery('#body .tabs:not(.anchors) .tabs-links li').index(jQuery('#body .tabs:not(.anchors) .tabs-links li.current'));
		
		if (index > -1) {
			tabs.hideGroups(index);
		} else {
			tabs.hideGroups(0);
		}
		
		jQuery('#body .tabs:not(.anchors) .tabs-links a').each(function(index) {
			jQuery(this).bind('click', function(event) {
				tabs.hideGroups(index,'slow');
				event.preventDefault();
			});
		});
	},
	
	hideGroups : function(index, speed) {
		jQuery('#body .tabs:not(.anchors) .tabs-links a').each(function(i) {
			var target = jQuery(this).attr('href').split('#')[1];
			
			if (i != index) {
				jQuery(this).parent().removeClass('current');
				if (speed != null) {
					jQuery('#'+ target).hide(speed);
				} else {
					jQuery('#'+ target).hide();
				}
			} else {
				jQuery(this).parent().addClass('current');
				if (speed != null) {
					jQuery('#'+ target).show(speed);
				} else {
					jQuery('#'+ target).show();
				}
			}
		});
	}
	
}

var showhideblock = {
	
	init : function() {
		jQuery('.show-hide-block:not(.opened) .block').hide();
		
		jQuery('.show-hide-block .current .block').show();
		
		jQuery('.show-hide-block.opened .binder').parent().addClass('current');
		
		jQuery('.show-hide-block .open-all').bind('click', function(event) {
			jQuery(this).parents('.show-hide-block').find('.block:not(:visible)').slideDown('slow');
			event.preventDefault();
		});
		
		jQuery('.show-hide-block .binder').bind('click', function(event) {
			if (jQuery.browser.msie) {
				if (jQuery(this).parent().toggleClass('current').end().parent().find('.block').css('display') == 'block') {
					jQuery(this).parent().toggleClass('current').end().parent().find('.block').hide();
				}
				else {
					jQuery(this).parent().toggleClass('current').end().parent().find('.block').show();
				}
			} else {
				jQuery(this).parent().toggleClass('current').end().parent().find('.block').slideToggle('fast');
			}
			
			event.preventDefault();
		});
	}
	
}

var showhideblockwithtext = {
	
	init : function() {
		jQuery('.show-hide-block-with-text .block').hide();
		
		jQuery('.show-hide-block-with-text .open-all').bind('click', function(event) {
			jQuery(this).parents('.show-hide-block').find('.block:not(:visible)').slideDown('slow');
			event.preventDefault();
		});
		
		jQuery('.show-hide-block-with-text .binder').bind('click', function(event) {
			jQuery(this).parent().toggleClass('current').end().parent().find('.block').slideToggle('slow');
			
			if (jQuery(this).parent().hasClass('current')) {
				jQuery(this).find('.replace-text').text('Cerrar');
			} else {
				jQuery(this).find('.replace-text').text('Abrir');
			}
			
			event.preventDefault();
		});
	}
	
}

var print = {
	
	init : function(selector) {
		jQuery(selector).after('<p class="print"><a href="#">Imprimir</a></p>');
		
		jQuery('.print a').bind('click', function(event) {
			window.print();
			event.preventDefault();
		});
	}
	
}

var file = {
	
	init : function() {
		jQuery('input.file').each(function(index) {
			jQuery(this).after('<input class="image attachfile" src="./attach.gif" alt="Adjuntar" title="Adjuntar" type="image" value="Enviar" id="send_'+ jQuery(this).attr('id') + index +'" />');
			jQuery(this).after('<input class="text" readonly="readonly" type="text" id="text_'+ jQuery(this).attr('id') + index +'" />');
			
			jQuery(this).parent().find('.attachfile').bind('click', function(event) {
				jQuery(this).parent().find('input.file').click();
				event.preventDefault();
			});
			
			jQuery(this).attr('size', 1)
		}).css({
			position: 'absolute',
			bottom: '5px',
			opacity : '0'
		}).bind('change', function(event) {
			jQuery(this).parent().find('.text').val(jQuery(this).val())
			event.preventDefault();
		}).parent().css({
			display: 'block',
			position: 'relative'
		});
	}
	
}

var org = {
	
	init : function() {
		// Hide all groups
		jQuery('#body .team .org .group').hide();
		
		// Opacity for links
		jQuery('#body .team .org div div.ul div.li').fadeTo('fast', 0.7);
		
		// Shadows and lights
		jQuery('#body .team .org div div.ul').prepend('<div class="shadow"></div>').find('div.shadow').css({
			background: '#000',
			opacity: '0',
			left: '0',
			height: '170px',
			position: 'absolute',
			top: '0',
			width: '100%',
			zIndex: '1'
		});
		
		jQuery('#body .team .org div div.ul').prepend('<div class="light"></div>').find('div.light').css({
			background: '#fff',
			opacity: '0',
			left: '0',
			height: '170px',
			position: 'absolute',
			top: '0',
			width: '100%',
			zIndex: '1',
			display: 'none'
		});
		
		jQuery('#body .team .org div div.ul div.li a').each(function(index) {
			// Get the target
			var target = jQuery(this).attr('href').split('#')[1];
			
			jQuery(this).bind('click', function(event) {
				// Link clicked
				var link = jQuery(this);
				
				// Remove previous marker classes
				jQuery('#body .team .org div .current').removeClass('current');
				
				// Close others
				jQuery('#body .team .org .group:visible').each(function(index) {
					if (jQuery(this).html() != jQuery('#'+ target).html()) {
						jQuery(this).slideUp('fast');
					}
				});
				
				// Change the target status
				jQuery('#'+ target).slideToggle('fast', function() {
					// Add marker classes
					if (jQuery('#'+ target +':visible').length > 0) {
						link.addClass('current').parent().addClass('current').parent().addClass('current');
					}
					
					jQuery('#body .team .org div div.ul div.li.current').fadeTo('slow', 0.9);
					jQuery('#body .team .org div div.ul div.li:not(.current)').fadeTo('slow', 0.7);
					
					if (jQuery('#body .team .org div div.ul.current').length <= 0) {
						jQuery('#body .team .org div div.ul .shadow, #body .team .org div div.ul .light').css('opacity', '0');
					}
					else {
						jQuery('#body .team .org div div.ul.current .shadow').fadeTo('slow', 0.6);
						jQuery('#body .team .org div div.ul.current .light').fadeTo('slow', 0);
						
						jQuery('#body .team .org div div.ul:not(.current) .shadow').fadeTo('slow', 0);
						jQuery('#body .team .org div div.ul:not(.current) .light').fadeTo('slow', 0.2);
					}
				});
				
				event.preventDefault();
			});
		});
	}
}

var tree = {
	
	init : function() {
		jQuery('ul.tree li ul').hide();
		
		jQuery('ul.tree li a').bind('click', function(event) {
			jQuery(this).next('ul').slideToggle('fast');
			event.preventDefault();
		});
		
		jQuery('ul.tree li li li a, ul.tree a.unbind').unbind('click');
	}
	
}


var comunicationImageGallery = {
	
	init : function() {
		jQuery('#body .comunication .gallery table td.images-block ul').hide();
		
		jQuery('#body .comunication .gallery table td .galleryBinder').bind('click', function(event) {
			jQuery(this).parents('tr').next('tr').find('ul').slideToggle('fast');
			event.preventDefault();
		});
	}
	
}

var redes = {
	
	init : function() {
		jQuery('#body .red .menu ul li a').each(function(index) {
			var element = jQuery(jQuery(this).attr('href'));
			if (element.length > 0) {
				element.hide();
			}
			
			jQuery(this).bind('click', function(event) {
				
				
				jQuery('#body .red .menu ul li a').each(function(index) {
					var element2 = jQuery(jQuery(this).attr('href'));
					
					if (element2.length > 0 && element.attr('id') != element2.attr('id')) {
						element2.slideUp('fast');
						jQuery(this).removeClass('current');
					}
				});
				
				element.slideToggle('fast');
				jQuery(this).toggleClass('current');
				
				event.preventDefault();
			});
		});
	}
	
};

var hidethings = {
	
	init : function() {
		jQuery('.hider .tabs-links a').bind('click', function() {
			jQuery('.group.hidden').hide();
		});
		
		jQuery('.services .hider .tabs .tab a[href='+ window.location.href.split('http://www.extenda.es')[1] +']').bind('click', function() {
			jQuery('.group.hidden').show();
		});
	}
	
};

jQuery(document).ready(function() {
	content_menu_home.init();
	content_menu_home_calls.init();
	dropdown.init();
	tabs.init();
	showhideblock.init();
	showhideblockwithtext.init();
	/* print.init('#body .directory-signup h2'); */
	file.init();
	org.init();
	tree.init();
	comunicationImageGallery.init();
	redes.init();
	auto_submit.init();
	opentarget.init();
	hidethings.init();
});

