jQuery.noConflict();

/*
 * Author:		Phil Sumner [phil.sumner@archant.co.uk]
 * Agency:		Archant Dialogue Ltd.
 * Client: 		British Parachuting Association
 * Project: 	Skydive Mag
 * Comment:		Main JS

----------------------


Contents:

1. SIFR
2. Generic JS Functions
3. Additional JQuery custom functions


/* 

 * All other functions and objects should be apart of this.

 */





/*--------------------------------------------------------------------------
	SIFR
----------------------------------------------------------------------------*/
//sIFR.debug.ratios({ src: '/assets/swf/urloved.swf', selector: 'h2' });
var urloved = {
	src: '/assets/swf/urloved.swf'
};

sIFR.activate(urloved); // From revision 436 and onwards

sIFR.replace(urloved, {
	selector: '#content #sectionContainer h2.news',
	ratios: [7, 1.47, 10, 1.43, 11, 1.36, 14, 1.38, 18, 1.36, 24, 1.34, 25, 1.32, 28, 1.33, 29, 1.31, 35, 1.32, 37, 1.3, 40, 1.31, 43, 1.3, 46, 1.31, 59, 1.3, 101, 1.29, 102, 1.28, 108, 1.29, 109, 1.28, 110, 1.29, 112, 1.28, 113, 1.29, 114, 1.28, 115, 1.29, 116, 1.28, 117, 1.29, 1.28],
	tuneHeight: -1,
	wmode: 'transparent',
	thickness:100,
	css: [
		'.sIFR-root { color: #1553a5; font-size:20px; text-decoration:none; text-transform: uppercase; }',
		'.sIFR-root em { color: #39b5ee; font-size:20px; text-decoration:none; text-transform: uppercase; font-style:normal; }'
	]
});
sIFR.replace(urloved, {
	selector: '#content #sectionContainer h2.club',
	ratios: [7, 1.47, 10, 1.43, 11, 1.36, 14, 1.38, 18, 1.36, 24, 1.34, 25, 1.32, 28, 1.33, 29, 1.31, 35, 1.32, 37, 1.3, 40, 1.31, 43, 1.3, 46, 1.31, 59, 1.3, 101, 1.29, 102, 1.28, 108, 1.29, 109, 1.28, 110, 1.29, 112, 1.28, 113, 1.29, 114, 1.28, 115, 1.29, 116, 1.28, 117, 1.29, 1.28],
	tuneHeight: -1,
	wmode: 'transparent',
	thickness:100,
	css: [
		'.sIFR-root { color: #1553a5; font-size:20px; text-decoration:none; text-transform: uppercase; }',
		'.sIFR-root em { color: #cf3c46; font-size:20px; text-decoration:none; text-transform: uppercase; font-style:normal; }'
	]
});
sIFR.replace(urloved, {
	selector: '#content #sectionContainer h2.events',
	ratios: [7, 1.47, 10, 1.43, 11, 1.36, 14, 1.38, 18, 1.36, 24, 1.34, 25, 1.32, 28, 1.33, 29, 1.31, 35, 1.32, 37, 1.3, 40, 1.31, 43, 1.3, 46, 1.31, 59, 1.3, 101, 1.29, 102, 1.28, 108, 1.29, 109, 1.28, 110, 1.29, 112, 1.28, 113, 1.29, 114, 1.28, 115, 1.29, 116, 1.28, 117, 1.29, 1.28],
	tuneHeight: -1,
	wmode: 'transparent',
	thickness:100,
	css: [
		'.sIFR-root { color: #1553a5; font-size:20px; text-decoration:none; text-transform: uppercase; }',
		'.sIFR-root em { color: #d67f36; font-size:20px; text-decoration:none; text-transform: uppercase; font-style:normal; }'
	]
});
sIFR.replace(urloved, {
	selector: 'h1, #at-a-glance h2, form.contactForm h2',
	ratios: [7, 1.47, 10, 1.43, 11, 1.36, 14, 1.38, 18, 1.36, 24, 1.34, 25, 1.32, 28, 1.33, 29, 1.31, 35, 1.32, 37, 1.3, 40, 1.31, 43, 1.3, 46, 1.31, 59, 1.3, 101, 1.29, 102, 1.28, 108, 1.29, 109, 1.28, 110, 1.29, 112, 1.28, 113, 1.29, 114, 1.28, 115, 1.29, 116, 1.28, 117, 1.29, 1.28],
	tuneHeight: -1,
	wmode: 'transparent',
	thickness:100,
	css: [
		'.sIFR-root { color: #ff00cc; font-size:16px; text-decoration:none; text-transform: uppercase; }',
		'.sIFR-root em { color: #000000; font-size:16px; text-decoration:none; text-transform: uppercase; font-style:normal; }',
		'.sIFR-root a { color: #333333; font-size:16px; text-decoration:none; text-transform: uppercase; }',
		'.sIFR-root a:hover { color: #333333; font-size:16px; text-decoration:none; text-transform: uppercase; }'
	]
});

sIFR.replace(urloved, {
	selector: '#cal h2',
	ratios: [7, 1.47, 10, 1.43, 11, 1.36, 14, 1.38, 18, 1.36, 24, 1.34, 25, 1.32, 28, 1.33, 29, 1.31, 35, 1.32, 37, 1.3, 40, 1.31, 43, 1.3, 46, 1.31, 59, 1.3, 101, 1.29, 102, 1.28, 108, 1.29, 109, 1.28, 110, 1.29, 112, 1.28, 113, 1.29, 114, 1.28, 115, 1.29, 116, 1.28, 117, 1.29, 1.28],
	tuneHeight: 2,
	wmode: 'transparent',
	thickness:100,
	css: [
		'.sIFR-root { color: #ff00cc; font-size:16px; text-decoration:none; text-transform: uppercase; text-align:center; }',
	]
});


/*--------------------------------------------------------------------------
	Generic JS Functions
----------------------------------------------------------------------------*/
jQuery.promo = function() {
	jQuery("#latest-video").jMyCarousel({
		 visible: '3',
		eltByElt: true,
		evtStart: 'mousedown',
		evtStop: 'mouseup'
	});
	jQuery("#latest-photo").jMyCarousel({
		 visible: '3',
		eltByElt: true,
		evtStart: 'mousedown',
		evtStop: 'mouseup'
	});
			
	jQuery('.promo-gallery .actions li.inactive a').animate({opacity:0.5},1);
	jQuery("#latest-video").addClass('inactive');
	
	jQuery('.promo-gallery .actions li a').live('click',function() {
		
		var tag = jQuery(this).attr('rel');
		
		// specific to action links
		jQuery('.promo-gallery .actions li:not(.news) a').fadeTo(1,1);
		jQuery('.promo-gallery .actions li:not(.news)').addClass('inactive');
		jQuery(this).closest('li').removeClass('inactive');
		jQuery('.promo-gallery .actions li.inactive a').fadeTo(1,0.5);
		// specific to action links
		
		
		jQuery('.promo-gallery #latest-promo div').not(jQuery('#'+tag)).fadeOut('slow',function() {
			jQuery('#'+tag).fadeIn('slow');										
		});
		
		return false;
	});
}

jQuery.fn.disabledMessage = function() {
	
	if(jQuery(this).hasClass('login-first'))
	{
		jQuery(this).append('<a href="/log-in/"><label>'+jQuery(this).attr('title')+'</label></a>');
	}
	else
	{
		jQuery(this).append('<label>'+jQuery(this).attr('title')+'</label>');
	}
	jQuery(this).attr('title','');
	jQuery(this).live('mouseover',function() {jQuery(this).find('label').fadeIn('fast');});
	jQuery(this).find('label').live('mouseout',function() {jQuery(this).fadeOut('fast');});
		
}


/* Preload js rollover images. */
/* ********************************************************************************* */

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++) {
	jQuery('<img>').attr('src', arguments[i]);
  }
}

/* Simulate rollover on form buttons.  Inputs of type="button" must have a rollover image '-on.gif' */
/* ********************************************************************************* */

jQuery.fn.rolloverImage = function() {
	jQuery(this).hover(function(){
		if (!jQuery(this).data('source')) jQuery.data(jQuery(this).get(0), 'source', { original: jQuery(this).attr('src'), file: jQuery(this).attr('src').split(".")[0] });
		jQuery(this).attr('src', jQuery(this).data('source').file + '-on.gif');
	},
	function(){
		jQuery(this).attr('src', jQuery(this).data('source').original);
	});
}

/* Addthis Config */
var addthis_pub="4a12aca440be8b2d"; // Required for addthis button (http://www.addthis.com/)

jQuery.fn.flagger = function() {
	// this = #language-selector ul
	
	jQuery(this).find("a[class!='active'] img").animate({
		opacity:0.4,
		height:"19px",
		width:"26px",
		marginTop:"8px"
	},1000);
	
	jQuery(this).find("li a[class!='active']").live('mouseover',function() {
		jQuery(this).find("img:not(:animated)").animate({
			opacity:1,
			height:"26px",
			width:"33px",
			marginTop:"0px"
		},300);											  
	});
	
	jQuery(this).find("li a[class!='active'] img").live('mouseout',function() {
		jQuery(this).animate({
			opacity:0.4,
			height:"19px",
			width:"26px",
			marginTop:"8px"
		},100);		
	});
	
}


var et;

jQuery.fn.showRelatedEvent = function() {
	
	// this = '#events .month td a'
	// get pos of parent H2 to align popup with
	var pos = jQuery(this).closest('.month').children('h2').position();
	var setHeight = jQuery(this).closest('div').height();
	
	// call the loading animation while AJAX is firing
	jQuery(this).closest('.month').loader();
	
	// get link ID used to get relevent events listing and set the URL for ajax to call
	var eventDate = jQuery(this).attr('rel');
	var toLoad = '/events-ajax/' + eventDate + '/';
	
	// if event-detail DATA is not set or its a new event ID make AJAX call
	if(!jQuery('#events-detail').data('id') || jQuery('#events-detail').data('id') != eventDate)
	{
		if (jQuery.browser.msie) {
			// IE6 refuses to set height
		}
		else {
			jQuery('#events-detail').css('height','inherit');
		}

		// define DATA for next time a click happens
		jQuery('#events-detail').data('id',eventDate);
		
		// slide up the popup (if previously displaying)
		jQuery('#events-detail').slideUp('fast',function() {
			
			// get the event data from server
			jQuery.ajax({
				type: "GET",
				url: toLoad,
				success: function(data){
	
					// on success populate html
					jQuery('#events-detail').html(data);
					if(jQuery('#events-detail').height() < setHeight)
					{
						setHeight = jQuery('#events-detail').height() + (setHeight - jQuery('#events-detail').height()) -44;
					}
					else
					{
						setHeight = jQuery('#events-detail').height();
					}
					// align to top h2
					jQuery('#events-detail').css({'left':pos.left + 'px','top':pos.top + 29 + 'px'/*,'height':setHeight+'px'*/});
					// fire Timeout to stop and load lag
					et = setTimeout(showEventsDetail,500);
					
					// Fix the flag links to point at the new selected date
					jQuery('#language-selector li a').each( function() {
						jQuery(this).attr('href', jQuery(this).attr('href').replace(/&selectedDate=[0-9]{2}-[0-9]{2}/, '') + '&selectedDate=' + eventDate.replace(/^[0-9]{4}-/, ''));
					});
				}
			 });
		
		});
		
	}
	// otherwise just slide event popup down (dont re-call AJAX)
	else
	{
		showEventsDetail();
	}

}

jQuery.fn.loader = function() {

	var pos = jQuery(this).position();
	
	jQuery('#loading').css({'top':pos.top + 48 + 'px','left':pos.left + 'px','height':jQuery(this).height() - 28 + 'px','width':jQuery(this).width() + 'px'});
	jQuery('#loading, #loading img').show();
		
}

jQuery.fn.bindAccordion = function() {
	jQuery(this).accordion({ 
		header: '.feature-heading',
		changestart: function(event, ui) { 
			ui.oldHeader.find('img').show();
			ui.newHeader.find('img').hide();
			jQuery('#events-detail,.feature-content').css({height:'auto'}); 
		},
		change: function(event, ui) { 
			jQuery('.feature-content').css({'overflow':'visible'}); // Fix IE overflow issue
			
			ui.oldHeader.closest('li').addClass('inactive');
			ui.newHeader.closest('li').removeClass('inactive');
			
			jQuery(this).find('h3').fadeTo(1,1);
			jQuery(this).find('li.inactive h3').fadeTo(1,0.4);
			
		}
	})
		
}

jQuery.fn.bindEventsCalendar = function() {
	jQuery(this).click( function() {
		jQuery.get('/events-calendar-ajax/' + jQuery(this).attr('rel') + '/', function(data) {
			jQuery('div#cal').html(data);
			jQuery('.events-calendar .actions a').bindEventsCalendar();
			jQuery('#cal div').calendarPanel();
			sIFR.replace(stag, {
				selector: '#cal h2',
				ratios: [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01],
				tuneHeight: 4,
				wmode: 'transparent',
				thickness:100,
				css: [
					'.sIFR-root { color: #333333; font-size:14px; font-weight:bold; text-decoration:none; text-transform: uppercase; text-align:center; }',
				]
			});
		});
		return false;
	});
}

function showEventsDetail() {
	jQuery('#loading img').hide();
	jQuery('#events-detail li h3').fadeTo(1,1);
	jQuery('#events-detail li.inactive h3').fadeTo(1,0.4);
	jQuery('#events-detail').slideDown('slow');
	var targetOffset = jQuery('#events-detail').offset().top - 100;
	jQuery('html,body').animate({scrollTop: targetOffset}, 1000);
	jQuery('#events-detail ul').bindAccordion();
	clearTimeout(et);
}



jQuery(function(){
				
	jQuery('.thickbox').lightBox();
	
	var ex,ey;
	if(jQuery('#tip').length > 0)
	{
		jQuery('.help-icon').live('click',function(e) {
			jQuery('#tip').css({'left':e.pageX +'px','top': e.pageY - 200 +'px'});				  
			return false;
		});
		jQuery('#tip .close').live('click',function() {
			jQuery('#tip').css({'left':'-9999px','top':'-9999px'});				  
			return false;
		});
	}
	
	jQuery('#language-selector ul').flagger();
	
	// Preload JS-inserted images
	jQuery.preloadImages('/assets/images/admin/btn-login-on.gif');

	// include external js
	jQuery.getScript('http://s7.addthis.com/js/200/addthis_widget.js');
	
	jQuery('p.share a')
	.mouseover(function(){return addthis_open(this, '', '[URL]', '[TITLE]')})
	.mouseout(function(){addthis_close()})
	.click(function(){return addthis_sendto()});

	if (jQuery('#latest-promo').length > 0) jQuery.promo(); // Initiate promo feature where '#latest-promo' is present
	
	jQuery('#gallery-detail .photo .actions ul li.enlarge a').attr('target','_blank');
	jQuery("a[href^='http:']").not("[href*='gallery']").attr('target','_blank');

	jQuery('#gallery-detail .disabled').disabledMessage();
	//jQuery('#cal div').calendarPanel();
	
	jQuery('#go').rolloverImage();
	
	// image, video, rotw rating 
	jQuery('.rating').each(function() {
		var rating = jQuery(this).find('span').attr('class');
		jQuery(this).data('rating',rating);
		jQuery(this).find('span a').click( function () {
			var rating = jQuery(this).attr('class').split('-', 2)[1];
			var type = jQuery(this).attr('rel').split('-', 2)[0];
			var id = jQuery(this).attr('rel').split('-', 2)[1];
			jQuery.get( '/rate-' + type + '/' + id + '/' + rating + '/',
				function(data) {
					jQuery('p.rating').replaceWith(data);
				}
			 );
			 return false;
		});
	});
	
	jQuery('.rating a').hover(function() {
		var newClass = jQuery(this).attr('class');
		jQuery('.rating span').attr('class',newClass);	
	});
	
	jQuery('.rating').bind('mouseleave',function(e){
		var newClass = jQuery(this).data('rating');
		jQuery(this).find('span').attr('class',newClass);										
	});
	
	// Detail page - email form display toggle
	jQuery('.user-comments .leave-comment').not('.open').hide();
	
	jQuery('.user-comments .actions a').click(function() {
		if(!jQuery(this).data('state') || jQuery(this).data('state') == 'hidden')
		{
			jQuery('.user-comments .success').slideUp('fast',function() {});
			jQuery('.user-comments .leave-comment').slideDown('normal');
			if (jQuery(this).hasClass('btn-leave-comment')) {
				jQuery(this).removeClass().addClass('btn-cancel-leave-comment');
			}
			jQuery(this).data('state','showing');
		}
		else
		{
			jQuery('.user-comments .leave-comment').slideUp();
			if (jQuery(this).hasClass('btn-cancel-leave-comment')) {
				jQuery(this).removeClass().addClass('btn-leave-comment');
			}
			jQuery(this).data('state','hidden');
		}
		jQuery(this).blur();
		return false;
	});
		
	//hover states on the static widgets
	jQuery('#dialog_link, ul#icons li').hover(
		function() { jQuery(this).addClass('ui-state-hover'); }, 
		function() { jQuery(this).removeClass('ui-state-hover'); }
	);	
	
	jQuery('#events-detail .close').live('click',function() {
		jQuery('#loading').hide();
		jQuery('#events-detail').slideUp();
		// Fix the flag links to point at an empty selected date
		jQuery('#language-selector li a').each( function() {
			jQuery(this).attr('href', jQuery(this).attr('href').replace(/&selectedDate=[0-9]{2}-[0-9]{2}jQuery/, ''));
		});
		return false;
	});
	
	jQuery('.events-calendar .actions a').bindEventsCalendar();
	
	jQuery('.latest-gallery .summary ul a').click( function() {
		jQuery('.latest-gallery .summary ul li.selected').removeClass('selected');
		jQuery(this).parent().addClass('selected');
		jQuery.get('/latest-gallery-ajax/' + jQuery(this).attr('rel') + '/', function(data) {
			jQuery('.latest-gallery .gallery').fadeOut();
			setTimeout( function() {
				jQuery('.latest-gallery .gallery').html(data).fadeIn();
			}, 800);
		});
		return false;
	});
	
	jQuery(function() {
		setInterval( "slideSwitch()", 5000 );
	});

});

function slideSwitch() {
	var $active = jQuery('#eyeCandyContainer img.active');
	if ( $active.length == 0 ) $active = jQuery('#eyeCandyContainer img:last');
	var $next =  $active.next().length ? $active.next()
		: jQuery('#eyeCandyContainer img:first');
	$active.addClass('last-active');
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
		});
}


// Allow file upload to be re-selected
function uploadDifferentFile(id) {
    jQuery('#' + id).replaceWith('<input id="' + id + '" name="' + id + '" type="file" />');
}


