/*
 * custom.js
 * Custom javascript Europ Assistance 
 */

var $tabs_get_quote;
 
$(function() {init(document);});

function init(obj) {
		// Navigation
		$("#nav .nav-1>li",obj).hoverIntent({
			interval: 150, 
			over: showDropdown, 
			timeout: 500, 
			out: hideDropdown
		}).find('.nav-dropdown').bgIframe();
		$("#nav .nav-1>li",obj).addClass('with-js');
		function showDropdown(){ 
		  $(this).addClass('show'); $(this).removeClass('with-js'); $(this).children('a').addClass('hover'); 
			var dropDown = $('.nav-dropdown',$(this)); 
			dropDown.height('auto'); /* set height to auto */
			var H = dropDown.height();  /* to obtain actual height for */
			dropDown.height(0).animate({height:H},200); /* animation */
		}
		function hideDropdown(){ 
			$(this).removeClass('show'); $(this).addClass('with-js'); $(this).children('a').removeClass('hover'); 
		}
		
		// Clear input fields on focus 
		$('.focus-field',obj).clearFieldOnFocus();
		
		// Collapsible table rows
		$('table.collapsible',obj).collapsibleTable();
		
		// Toggle next content
		$('.toggler',obj).toggler();
		
		// Tooltip
		$('.tip',obj).each( function() {
      $(this).attr('rel',$(this).attr('alt'));
			$(this).attr('alt','');  
	  	var c = $(this).attr('rel');
      
			$(this).simpletip({
				content: c, position: 'bottom', offset: [110,0], boundryCheck: true, fixed: true, showEffect: 'fade'
			});
		});
		
    jQuery('.slideshow').slideshow();
    
		// Datepicker
		$('.datepicker',obj).datepicker({
			changeMonth: true, changeYear: true, dateFormat : 'dd/mm/yy', minDate : +1, duration: ''														
		});

		// Dialog
		if ($('#dialog-email-quote',obj)) {
		var dia = $('#dialog-email-quote',obj).dialog({
			bgiframe: true, autoOpen: false, closeOnEscape: true, draggable: true, resizable: false, width: 360, height: 230, hide: 'fadeOut', show: 'fadeIn', modal: true //position: 'center'
		});
	  $('#link-email-quote',obj).click(function() {
			$('#dialog-email-quote').dialog('open');
			$('#dialog-email-quote #button-cancel').click(function() {dia.dialog('close');} );
			$('#dialog-email-quote #button-submit').click(function() {if($('#dialog-email-quote input[type=radio]:checked').val() != undefined) dia.dialog('close');} );
		});		
		}

    // Tabs - what's included
    if ( $.browser.msie && $.browser.version < 7 ) var $tabs_included = $('.tabs-included').tabs({});
		else var $tabs_included = $('.tabs-included').tabs({ /*fx: { height: 'toggle', opacity: 'toggle' }*/ }); 
		// Demo
		$('.tabs-included .link-next-large',obj).click(function() { // switch to next tab
				var selected = $tabs_included.tabs('option', 'selected');
				$tabs_included.tabs('select', selected+1); 
				return false;
		});
		$('.tabs-included .link-prev',obj).click(function() { // switch to previous tab 
				var selected = $tabs_included.tabs('option', 'selected');
				$tabs_included.tabs('select', selected-1); 
				return false;
		});

    // Tabs - get quote
/*    if ( $.browser.msie && $.browser.version < 7 ) $tabs_get_quote = $('.tabs-get-quote').tabs({});
		else $tabs_get_quote = $('.tabs-get-quote').tabs({ fx: { height: 'toggle', opacity: 'toggle'}, show : function(e,ui) {
           if ($tabs_get_quote != undefined)
            for (i=ui.index+1; i<5; i++)
               $tabs_get_quote.tabs('disable',i);
           return true;
           } });*/
    
    $tabs_get_quote = $('.tabs-get-quote').tabs({ show : function(e,ui) {
           if ($tabs_get_quote != undefined)
            for (i=ui.index+1; i<5; i++)
               $tabs_get_quote.tabs('disable',i);
           return true;
           } }); 		  
    /* Onclick events for demo */
		$('#box-quote-summary').hide();
	
}



// CLEAR FIELD ON FOCUS : used for fields without label - when the field is cleared, the title text is placed as value
	
	jQuery.fn.extend({
		clearFieldOnFocus: function() {
			return this.each(function() { 
				if (($(this).attr('title') != undefined) && ($(this).val() == '')) $(this).val($(this).attr('title'));
				$(this).focus(function() { if($(this).attr('value') == $(this).attr('title')) $(this).attr('value', ''); });
				$(this).blur(function() { if($(this).attr('value') == '') $(this).attr('value', $(this).attr('title')); });
			});
		}
	});
	

// COLLAPSIBLE TABLE ROWS

		jQuery.fn.extend({
		collapsibleTable: function() {
			
			return this.each(function() {
					 $('tbody td',$(this)).wrapInner('<div class="td-content"></div>');
					 $('tbody th',$(this)).wrapInner('<span></span>');
					 // if no TR has class open, the first one is opened initially
					 var initOpen;
					 $('tbody tr',$(this)).each( function() { if ($(this).hasClass('open')) initOpen = $(this);  });
					 if (initOpen == undefined) { $('tbody tr:first',$(this)).addClass('open'); }
					 // TH click event
					 $('tbody th',$(this)).click( function () {
							 var tableRow = $(this).parent('tr');
		           toggleTableRow(tableRow);
					 });
					 // toggle TR visibility
					 function toggleTableRow (tableRow) { 
						 if ( tableRow.hasClass('open') ) {
							 tableRow.removeClass('open');
							 $('td div.td-content',tableRow).animate({height: 0, opacity: 0}, 400);
						 } else {
							 tableRow.addClass('open');
							 $('td div.td-content',tableRow).each( function() { 
								 $(this).height('100%'); /* set height to 100% */
								 var H = $(this).height(); /* which gives us the actual content height */
								 $(this).height(0); /* set height to 0 before animation */
								 $(this).animate({ height: H, opacity: 1}, 400); 
							 });
						 }
					 };	
			});
		}
	});


// TOGGLER : toggles the visibility of the next element with class .toggle-content

		jQuery.fn.extend({
		toggler: function() {
			return this.each(function() { 
					var toggleContent = $(this).parents().next('.toggle-content');
						$(this).append('<span class="toggle-more">&nbsp;&nbsp;</span>');	
						if ( $(this).hasClass('toggle-open')) { toggleContent.show(); } 
						else { $(this).addClass('toggle-closed'); toggleContent.hide(); };		
						$(this).click(function() { 
							var toggleContent = $(this).parents().next('.toggle-content');
							if ( $(this).hasClass('toggle-open')) {
								$(this).removeClass('toggle-open').addClass('toggle-closed');
								if ( $.browser.msie && $.browser.version < 7 ) { toggleContent.hide(); }
								else { toggleContent.slideUp(); }
							} else {
								$(this).removeClass('toggle-closed').addClass('toggle-open');
								if ( $.browser.msie && $.browser.version < 7 ) { toggleContent.show(); }
								else { toggleContent.slideDown(); }
						  };
			      });
					
			});
		}
	}
  );
    
    
 // SLIDESHOW : Homepage main promo

(function($){
	jQuery.fn.slideshow = function(options) {
		
		settings = jQuery.extend({
			 menuContainer:   '.slideshow-menu',  
			 slidesContainer: '.slideshow',
			 timeout: 10000
		}, options);
	
		return this.each(function() {
 
			var $container = $(this);
			var $slides = $container.children();		
			var $menu = $(settings.menuContainer);    
      var $menuItems;
			var previousSlide;
			
			// position slides 
			$container.css('position', 'relative');
			$slides.each( function(i){
				$(this).css({'z-index':$slides.length-i,'position':'absolute','top':0,'left':0,'opacity':0}).hide();
				$('a',this).hide();
			});
	
			// build menu
      $menuItems = buildMenu();
			
			// go to first slide
		  gotoSlide(0);
			
			// start timer
			timer = setInterval(function() {gotoNextSlide();},settings.timeout);

			function buildMenu() {
				var menuStr = '';
				var menuItems;
				$slides.each(function(i) { 
				  menuStr += '<li';
					if (i==0) menuStr += ' class="first"';
					menuStr += '><a href="#">'+(i+1)+'</a></li>';
				}); 
				$menu.html('<ul>'+menuStr+'</ul>');
				menuItems = $('a',$menu);					
				//add click events menu items
				$('a',$menu).each(function(i) {
						$(this).click(function(e) { gotoSlide(i); clearInterval(timer); return false; });											 
				}); 
				return menuItems;
			};
	
	    function gotoNextSlide() {
			  var next = previousSlide+1;
				if (next >= $slides.length) next = 0;
				gotoSlide(next);
			};
			
			function gotoSlide(index) {
				 $slides.stop();
				 $menuItems.stop();
				 if (previousSlide!=undefined) { 
				   $($menuItems[previousSlide]).removeClass('selected');
					 $('a',$slides[previousSlide]).hide();
				   $($slides[previousSlide]).css({'z-index':1}).animate({'opacity':0},{duration:1000}).hide();
					 
				 }
				 $($menuItems[index]).addClass('selected'); 
				 $('a',$slides[index]).show();
				 $($slides[index]).css({'z-index':0,'opacity':0}).animate({'opacity':1},{duration:1000}).show(); 
				 previousSlide = index;
			};			
		
		});
		
	};
})(jQuery);	
