/* -------------------------------------------------------------- 
  
   Pioneer Europe
   lightbox.js
	 
-------------------------------------------------------------- */
	

var $j = jQuery.noConflict();

var ie6 = ($j.browser.msie && $j.browser.version < 7 ) ? true : false;
var ie7 = ($j.browser.msie && $j.browser.version == 7 ) ? true : false;

$j(document).ready(function () {

		// Slideshow
		$j('.slideshow').slideShow();		

});

/*
    LIGHTBOX
*/

/* 
    SLIDESHOW
		Pass the number of the slide you want to show initially, eg. play_entertainment#1 shows 2nd slide
*/

(function($){
	jQuery.fn.slideShow = function(options) {
		
		settings = jQuery.extend({
			 menuContainer:   '.thumbs',
			 slidesContainer: '.slideshow_content',
			 animation:		''
		}, options);
	
		return this.each(function() {

			var $container = $(this);
			var $slides = $(settings.slidesContainer,$(this)).children();		
			var $menu = $(settings.menuContainer,$(this));
			$menu.append('<ul></ul>');
			var $menuItems;
			var previousSlide;
			
			$slides.each( function(i){
			
				$($slides[i]).css({'z-index':$slides.length-i,'position':'absolute','top':0,'left':0}).hide();
				var data = (new Function("return " + $($slides[i]).attr('rel')))(); 
				var thumbStr = '<li style="background:url('+data.thumb+')"><a href="#'+$($slides[i]).attr('id')+'" title="'+data.title+'"';
				if ( $($slides[i]).hasClass('video_item') ) { thumbStr += ' class="play" '; };
				thumbStr += '><span>'+data.title+'</span>';
				if ( $($slides[i]).hasClass('video_item') ) thumbStr += '<span class="icon"></span>';
				thumbStr += '</a></li>';
				$('ul',$menu).append(thumbStr);
				
			});
			$menuItems = $('li a',$menu);
     
	    $menuItems.each(function(i) {
						$(this).click(function(e) { e.preventDefault(); gotoSlide(i); });											 
			});
			
			
			// determine which slide to show initially
			// you can pass the ID of the slide or the slide number ( only if there is only one lightbox, e.g. in lightboxes )
			var initSlide;
      var h = window.location.hash.replace('#','');
		  
			if (h=='') {
				initSlide = 0;
			} else {
				if(isInteger(h)) {
					initSlide = Number(h);					 
				} else { 
					initSlide = getSlideNumber(h);
				}
	    }
			
			// go to initial slide
		  gotoSlide(initSlide);

			// Get slidenumber, you pass the id of the slide, you get the slide index in return
			function getSlideNumber(id) {
				  var index;
					 
					$slides.each( function(i){
				 		  if( $($slides[i]).attr('id') == id ) { index = i; return;}
							
			   	});
					
					if (index == undefined) { index = 0; }; 
					 
				  return index;	
			}


			// Go to slide 
			function gotoSlide(index) {
				 //alert('goToSlide '+ index + ' - ' + previousSlide);
				 if (index!=previousSlide) {
					 
					 // hide previous slide
						 if (previousSlide!=undefined) { 
							 $($menuItems[previousSlide]).removeClass('selected');						 
							 // remove video
							
							 if ( $($slides[previousSlide]).hasClass('video_item') ) { 
							   $('.flash_container',$($slides[previousSlide])).remove();
							 }
							 
							 $($slides[previousSlide]).hide();
						 }
					
					 // show current slide
					   $($menuItems[index]).addClass('selected');
					 	 // embed video
						 
						 if ( $($slides[index]).hasClass('video_item') ) { 
						   
							 var $slide = $($slides[index]);
							 var $videoLink = $('.video a',$slide);
						   
							 if ( $videoLink.size() == 0 ) {
									 alert('please define a link to youtube'); 
							 } else {

									 var videoData = (new Function("return " + $videoLink.attr('rel')))();
									 
									 var url = $videoLink.attr('href');
									 var id = videoData.id;
									 var w = videoData.width;
									 var h = videoData.height;
									 
									 var flashvars = {};
									 var params = {
										 scale: 'noScale',
										 allowfullscreen: 'true',
										 bgcolor: '#000000',
										 wmode: 'opaque',
										 allowscriptacces: 'always'
									 };
									 var attributes = {};
									 
									 $videoLink.hide();
									 $videoLink.after('<div class="flash_container"><div id="'+id+'" ></div></div>');
								 
									 // YOU TUBE EMBED
 								   if ( $('#'+id).size() > 0 ) { 
									   swfobject.embedSWF(url,id,w,h,"9.0.0","/files/swf/videoplayer/expressInstall.swf", flashvars, params, attributes);	
									 }  
									 
							 }
							 
						 };
						
					   $($slides[index]).show();
					   previousSlide = index;
						 
				 };
			};	
			
		});
	};
})(jQuery);



function isInteger(value) {
	return Math.floor(value) == value ? true : false; 
}


