//
// overlay.js
// Copyright (c) Connect Group Ltd. All rights reserved.
//
// $Id: overlay.js 485 2010-02-02 07:57:00Z paul $
//

jQuery(function($) {
	// Explore Gallery
	var galleries = new Object();

	// Photo Gallery Slideshow
	var isSlideshowRunning = false;
	var slideshowInterval = 5000;
	var slideshowTimeout;

	// Modal Settings
	$.fn.nyroModal.settings.minHeight = 150;
	$.fn.nyroModal.settings.closeButton = '<div class="close-nav"><a class="nyroModalClose" id="overlay-close-nav" title="' + pageConfig.text.overlay.close + '" href="#">Cerrar</a></div>';

	var galleryOverlaySettings = {
		wrap: {
			ajax: '<div id="nyroModalContentWrapper" class="gallery"></div>',
			manual: '<div id="nyroModalContentWrapper" class="gallery"></div>'
		},
		processHandler: function(settings) {
			var link = $(settings.from);
			var parent = link.parent('dd.connect-overlay');
			if (parent.length) {
				var content = $(overlayTemplates.connect);

				$('img', content).attr('src', link.attr('href'));
				$('img', content).attr('alt', link.children('span:not(.parent-number)').text());
				$('div#text h2 strong', content).text(link.children('span:not(.part-number)').text());

				if (link.children('span.part-number').length) {
					$('div#text p.part-number', content).text(link.children('span.part-number').text());
				} else {
					$('div#text p.part-number', content).remove();
				}
				
				if (parent.children('p.extended-description').length) {
					$('div#text p:not(.part-number)', content).text(parent.children('p.extended-description').text());
				} else {
					$('div#text p:not(.part-number)', content).remove();
				}

				settings.content = content;
				settings.wrap = galleryOverlaySettings.wrap;
				settings.type = 'manual';
			}
		},
		
		endFillContent: function(elts,settings) {
			var h1 = elts.wrapper.prev('h1');
			if (h1.length == 1) {
			
				var nav = $('div#nyroModalCaption span#nyroModalCaptionNav');
				nav.empty();

				var prev = $('.nyroModalPrev');
				var next = $('.nyroModalNext');

				if (prev.length == 1) {
					nav.append('<a href="' + prev.attr('href') + '" onclick="$.nyroModalPrev();return false;" title="prev">&lt; Previous</a>');
					if (next.length == 1) {
						nav.append('&nbsp;|&nbsp;');
					}
				}

				if (next.length == 1) {
					nav.append('<a href="' + next.attr('href') + '" onclick="$.nyroModalNext();return false;" title="next">Next &gt;</a>');
				}

				$('div#nyroModalCaption span#nyroModalCaptionText').html(galleries[settings.gallery] + ': ' + slide[0] + ' of ' + slide[1]);
			}
		}
	};

	var externalLinkOverlaySettings = {
		width: 1024,
		minHeight: 600,
		wrap: {
			ajax: '<div id="nyroModalContentWrapper" class="external-link"></div>'
		}
	};
	
	
	

	var photoGalleryOverlaySettings = {
		width: ($.browser.msie && $.browser.version=="6.0") ? 865 : 0,
		minHeight: ($.browser.msie && $.browser.version=="6.0") ? 700 : 0,
		wrap: {
			ajax: '<div id="nyroModalContentWrapper" class="photo-gallery"></div>'
		},
		endFillContent: function(elts,settings) {
			$('div:first', elts.content).css('overflow', 'hidden');
			processGalleryLinks(elts.content, settings);
		},
		beforeHideContent : function(elts, settings, callback) {
			clearTimeout(slideshowTimeout);
			isSlideshowRunning = false;
			callback();
		}
	};

	var videoGalleryOverlaySettings = {
		width: ($.browser.msie && $.browser.version=="6.0") ? 735 : 0,
		minHeight: ($.browser.msie && $.browser.version=="6.0") ? 700 : 0,
		wrap: {
			ajax: '<div id="nyroModalContentWrapper" class="video-gallery"></div>'
		},
		endFillContent: function(elts,settings) {
			processGalleryLinks(elts.content, settings);
		},
		endShowContent: function(elts, settings) {
			initializeFlowplayer($('div#main-content', elts.content));
		},
		hideContent: function(elts, settings, callback) {
			unloadFlowplayer($('div#main-content', elts.content));
			callback();
		},
		showTransition: function(elts, settings, callback) {
			// This is required to resolve a JavaScript error with IE6
			unloadFlowplayer($('div#main-content', elts.content));
			callback();
		}
	};

	var videoOverlaySettings = {
		wrap: {
			ajax: '<div id="nyroModalContentWrapper" class="video"></div>'
		},
		endShowContent: function(elts, settings) {
			initializeFlowplayer($('div#main-content', elts.content));
		},
		hideContent: function(elts, settings, callback) {
			unloadFlowplayer($('div#main-content', elts.content));
			callback();
		}
	};

	var graphOverlaySettings = {
		wrap: {
			ajax: '<div id="nyroModalContentWrapper" class="graph"></div>'
		}
	};

	// Explore and Accessories Gallery
	$('body#explore div.ui-accordion-content, body#accessories div.ui-accordion-content').each(function() {
		var title = $(this).prev('h3').text();
		var gallery = title.replace(/\W+/, '-').replace().toLowerCase();

		galleries[gallery] = title;

		$('div.cta-link-block dl dd:not(.connect-overlay) a[href]', $(this)).each(function() {
			var href = $(this).attr('href');
			if (href.indexOf('#') == -1) {
				$(this).attr('href', $(this).attr('href') + '#photo');
			}
			$(this).nyroModal(galleryOverlaySettings);
			// Gallery support disabled for the time being [LRO-125]
			// $(this).attr("rel", gallery);
		});
		$('div.cta-link-block dl dd.connect-overlay a', $(this)).nyroModal(galleryOverlaySettings);

		$('a.performance', $(this)).nyroModal({ forceType: 'image', wrap: { image: '<div id="nyroModalContentWrapper"></div>' } });
	});

	// Photo Gallery
	$('div.cta-link-photos-fullwidth').each(function() {
		var container = $(this);

		var title = $('dl dt strong', container).text();
		var gallery = title.replace(/ /, '-').toLowerCase();

		$('dl dd a', container).each(function() {
			//$(this).attr('href', $(this).attr('href') + '#wrapper');			
			//$(this).nyroModal(photoGalleryOverlaySettings);
			$(this).nyroModal(photoGalleryOverlaySettings);			
			$(this).attr("rel", gallery);
		});
	})



// Photo Gallery
	$('div.gallery').each(function() {
		var container = $(this);
		var hola = "hola";
		
		var title = $('dl dt a', container).attr('title');
		//var gallery = title.replace(/ /, '-').toLowerCase();

		//alert(title);
		
		$('dl dt a', container).each(function() {
			//$(this).attr('href', $(this).attr('href') + '#wrapper');			
			//$(this).nyroModal(photoGalleryOverlaySettings);
			$(this).nyroModal(photoGalleryOverlaySettings);			
			//$(this).attr("rel", gallery);
		});
	})




	// Video Gallery
	$('div.cta-link-videos-fullwidth').each(function() {
		var container = $(this);

		var title = $('dl dt strong', container).text();
		var gallery = title.replace(/ /, '-').toLowerCase();

		$('dl dd a', container).each(function() {
			$(this).attr('href', $(this).attr('href') + '#wrapper');
			$(this).nyroModal(videoGalleryOverlaySettings);
			$(this).attr("rel", gallery);
		});
	});

	// External Redirect Overlay
	$('a[rel="external"]').each(function() {
		$(this).click(function(e) {
			e.preventDefault();

			// NOTE: Access the href attribute using jQuery rather than 'this.href' because nyroModal 
			// only embeds content (rather than creating an iframe) on relative links.
			externalLinkOverlaySettings.url = $(this).attr('href').replace('?url=', '?view=Alt-1&url=');
			$.nyroModalManual(externalLinkOverlaySettings);
			return false;
		});
	});

	// External Redirect Overlay
	$('a[rel="foto_single"]').each(function() {
	$(this).click(function(e) {
			e.preventDefault();

			// NOTE: Access the href attribute using jQuery rather than 'this.href' because nyroModal 
			// only embeds content (rather than creating an iframe) on relative links.
			//externalLinkOverlaySettings.url = $(this).attr('href').replace('?url=', '?view=Alt-1&url=');
			$(this).nyroModalManual(videoGalleryOverlaySettings);
			return false;
		});
	});

	
	// Single videos
	$('a[rel^="video-"]').each(function() {
		$(this).attr('href', $(this).attr('href') + '#photo');
		$(this).nyroModal(videoOverlaySettings);
	});
	
	// Microsites
	$('a[rel^="microsite"]').each(function() {
		var re = new RegExp('\\d+x\\d+');
		var overlay = re.exec($(this).attr('rel'));
		if (overlay != null) {
			var size = overlay[0].split('x');
			$(this).nyroModal({ height: size[1], width: size[0], wrap: { iframe: '<div id="nyroModalContentWrapper"></div>' }, gallery: null });
			$(this).attr('rel', '');
		}
	});

	//
	// processGalleryLinks(overlay)
	// Processes any links that exist within an overlay that allow a user to jump around within a 
	// gallery.
	// PARAMS
	//  overlay: the current overlay
	//  settings: the settings for the modal
	//
	function processGalleryLinks(overlay, settings) {
		var links = $('[rel="'+settings.gallery+'"], [rel^="'+settings.gallery+' "]');

		var selector = 'div#text div.overlay-col-1 div dl dd a';
		$(selector, overlay).each(function() {
			var index = $(selector).index(this);
			if (index == links.index(settings.from)) {
				$(this).addClass("selected");
			}

			$(this).click(function(e) {
				e.preventDefault();

				if (isSlideshowRunning) {
					clearTimeout(slideshowTimeout);
				}

				links.eq(index).nyroModalManual(settings);
				if (isSlideshowRunning) {
					slideshowTimeout = setTimeout(function() { showNextSlide(settings) }, slideshowInterval);
				}
				return false;
			});
		});

		var slideshow = $('div#text div.overlay-col-1 dt span.slideshow', overlay);
		if (slideshow.length == 1) {
			if (isSlideshowRunning) {
				$(slideshow).children('a.play').hide();
				$(slideshow).children('a.stop').click(function() {
					clearTimeout(slideshowTimeout);
					isSlideshowRunning = false;
					$(this).hide();
					$(this).prev('a.play').show();
					return false;
				});

			} else {
				$(slideshow).children('a.stop').hide();
				$(slideshow).children('a.play').click(function() {
					slideshowTimeout = setTimeout(function() { showNextSlide(settings) }, slideshowInterval);
					$(this).hide();
					$(this).next('a.stop').show();
					return false;
				});
			}
		}
	}

	//
	// showNextSlide(settings)
	// Shows the next item in a modal gallery.
	// PARAMS
	//  settings: the settings for the modal
	//
	function showNextSlide(settings) {
		isSlideshowRunning = true;
		var nextSlide = $.nyroModalNext();
		if (nextSlide) {
			slideshowTimeout = setTimeout(function() { showNextSlide(settings) }, slideshowInterval);
		} else {
			var links = $('[rel="'+settings.gallery+'"], [rel^="'+settings.gallery+' "]');
			links.eq(0).nyroModalManual(settings);
			slideshowTimeout = setTimeout(function() { showNextSlide(settings) }, slideshowInterval);
		};
	}
});

//
// initializeFlowplayer(selector)
// Initializes a Flowplayer instance for the HTML elements that match the specified selector.
// PARAMS
//  selector: A selector for the element(s) that should have a Flowplayer instance, or a collection 
//            of element(s) that should have a Flowplayer instance.
// REMARKS
//  This method will not initialize a Flowplayer instance if:
//  * the variable flowplayerBaseUrl is undefined or null
//  * selector matches no elements
//  * the Flowplayer JavaScript libraries are not loaded
//
function initializeFlowplayer(selector) {
	if (typeof flowplayerBaseUrl == 'undefined' || flowplayerBaseUrl == null || $(selector).length == 0 || $(selector).flowplayer == undefined) {
		return;
	}

	var flowplayerUrl = flowplayerBaseUrl + 'flowplayer.commercial-3.1.5.swf';
	var flowplayerContentPluginUrl = flowplayerBaseUrl + 'flowplayer.content-3.1.0.swf';
	var flowplayerCaptionsPluginUrl = flowplayerBaseUrl + 'flowplayer.captions-3.1.4.swf';
	var flowplayerControlsPluginUrl = flowplayerBaseUrl + 'flowplayer.controls.swf';

	$(selector).flowplayer(flowplayerUrl, {
		key: '#@1adaadb8c178aa44dce',
		clip: {
			captionUrl: captionUrl,
			autoPlay: true,
			autoBuffering: true,
			scaling: 'fit'
		},
		play: { 
			url: flowplayerBaseUrl + '../play.png', 
			width: 90, 
			height: 90 
		},
		plugins: {
			content: {
				url: flowplayerContentPluginUrl,
				width: '100%',
				height: 40,
				bottom: 26,
				borderRadius: 0,
				border: 0,
				backgroundColor: '#000000',
				opacity: 0.7,
				style: {
					body: {
						textAlign: 'center',
						fontSize: 11,
						fontFamily: 'Arial'
					}
				},
				display: 'none'
			},
			captions: {
				url: flowplayerCaptionsPluginUrl,
				captionTarget: 'content',
				button: null
			},
			controls: { 
				url: flowplayerControlsPluginUrl,
				tooltips: {
					play: buttons.play,
					pause: buttons.pause,
					mute: buttons.mute,
					unmute: buttons.unmute,
					fullscreen: buttons.fullscreen,
					fullscreenExit: buttons.fullscreenExit,
					caption: buttons.subtitlesOn,
					captionExit: buttons.subtitlesOff,
					buttons: true
				},
				zIndex: 2
			}
		}
	});
}

//
// unloadFlowplayer(selector)
// Unloads the Flowplayer instance attached to the HTML elements that match the specified selector. 
// PARAMS
//  selector: A selector for the element(s) that should have a Flowplayer instance, or a collection 
//            of element(s) that should have a Flowplayer instance.
//
function unloadFlowplayer(selector) {
	if ($(selector).length == 0 || $(selector).flowplayer == undefined || $(selector).flowplayer(0) == undefined) {
		return;
	}
	$(selector).flowplayer(0).unload();
}

//
// TEMPLATES
//

var overlayTemplates = {
	connect: '<div id="photo" class="content-overlay-block"><div id="main-content"><img src="" alt="" height="360" width="720"/></div><div id="text"><h2><strong></strong></h2><p class="part-number"></p><p>{text}</p></div></div>'
};
