/* -------------------------------------------------------------- 
  
   Pioneer VSX-AV Amplifier Product Site
	 custom.js
   
-------------------------------------------------------------- */

var ie6 = ($.browser.msie && $.browser.version < 7 ) ? true : false;
var ie7 = ($.browser.msie && $.browser.version == 7 ) ? true : false;

$(document).ready(function () {

		// Clear input field on focus 
	  $('.focusField').clearFieldOnFocus();

		// KODO rollover
		$('.menu h1 a').mouseover( function() { $('#kodo_ro').show(); }).mouseout( function() { $('#kodo_ro').hide(); });

		// Choose your colour Overlay
		$('#overlay_colourpicker').colourPickerOverlay();
 		
		// Slideshow
		$('.slideshow').slideShow();		

		// Swap image
		$('#swapImages').css({'position':'relative', 'width':'600px', 'height':'305px'});
		$('#swapImages div').css({'position':'absolute', 'left':0, 'top': 0, 'width':'600px', 'height':'305px', 'z-index':100});
		$('#swapImages div:eq(0)').css({'z-index':200});
		$('.swap_image').swapImage();		

		// Lightbox
		$('a.open_lightbox').each( function() {
			$(this).each( function() {																
				$(this).click( function(e) { 
					var source = $(this).attr('href');  
					var lightbox = jQuery.fn.lightbox({ 
						 overlayId : 'overlay_885-457',						
						 lightboxId : 'lightbox_885-457',
						 width : 885,
						 height : 457,
						 source: source
					});			
					e.preventDefault();			
					lightbox.open();
				
				});
			});
		});

		// Open 360°view lightbox onload if ?lb=3d
		if($.getAllQueryStrings()['lb'] && $.getAllQueryStrings()['lb'].value=='3d') {
					var source = '/'+$('.landing').attr('id')+'/page/products/ipod_speaker_system/360view.html';  
					var lightbox = jQuery.fn.lightbox({ 
						 overlayId : 'overlay_960-610',						
						 lightboxId : 'lightbox_960-610',
						 width : 960,
						 height : 610,
						 source: source
					});			
					lightbox.open();
		}
		
		$('a.open_lightbox_3D').each( function() {
			$(this).each( function() {																
				$(this).click( function(e) { 
					var source = $(this).attr('href');  
					var lightbox = jQuery.fn.lightbox({ 
						 overlayId : 'overlay_960-610',						
						 lightboxId : 'lightbox_960-610',
						 width : 960,
						 height : 610,
						 source: source
					});			
					e.preventDefault();			
					lightbox.open();
				
				});
			});
		});		
		
});



/* 
    SWAP IMAGE
*/

(function($){
	jQuery.fn.swapImage = function(options) {

    var current;

		return this.each(function() { 
				 
			 $(this).click( function(e) {
				 var id = $(this).attr('href');
				 if (id!=current) {
				   if (current!= undefined) $(current).fadeOut('fast');
				   $(id).css({'z-index':200}).fadeIn('fast');
				   current = id;
				 }
				 e.preventDefault();		
			 });
			 
		});
		
	};
})(jQuery);


/* 
    OVERLAY PICK YOUR COLOUR
*/

(function($){
	jQuery.fn.colourPickerOverlay = function(options) {
		return this.each(function() { 
			 
			 var current;
			 var visible = false;
			 var h = 418; 
			 
			 $overlay = $(this);
		   $items = $('.items div',$overlay);
			 $items.fadeOut();
			 
			 if ( $items.length > 1 ) { 
				 $overlay.append('<div class="close"></div><div class="next"><a href="#"></a></div><div class="prev"><a href="#"></a></div>');
			   $overlay.css({'height':0});
				 $('.close',$(this)).click( function(e) { slideOut(); });
				 $('.next',$(this)).click( function(e) { var i=current+1; if(i>$items.length-1) i=0; jQuery.fn.showItem(i); });
				 $('.prev',$(this)).click( function(e) { var i=current-1; if(i<0) i=$items.length-1; jQuery.fn.showItem(i); });
			 }
			 			
			 jQuery.fn.showItem = function (id) {
				 
					if (!visible) { $overlay.css({'height':0}).animate({'height':h},500, function() {} ); visible = true; };
					
					if ( id != current ) {
						var prevId = current;
						var currId = id;
						if (prevId!=undefined) $($items[prevId]).css('z-index',10);
						$($items[currId]).css('z-index',100).fadeIn('800',function() { if(prevId!=undefined) $($items[prevId]).hide();  });
						current = id;
					} 
		
			 };	 
			 
			 function slideOut() { $overlay.css({'height':h}).animate({'height':0},500); visible = false; $($items[current]).fadeOut(); current = undefined; }
			 
		});
	};
})(jQuery);


/* 
    CLEAR FIELD ON FOCUS 
		For input text where the field label is displayed as initial value of the field 
		when the field is cleared, the initial value - stored in rel attribute - is displayed 
*/
 
(function($){
	jQuery.fn.clearFieldOnFocus = function() {
		return this.each(function() {
      var initVal = ''; if($(this).attr('value') != undefined) initVal = $(this).attr('value'); $(this).attr('rel',initVal); 
			$(this).focus(function() { if($(this).attr('value') == $(this).attr('rel')) $(this).attr('value', ''); });
			$(this).blur(function() { if($(this).attr('value') == '') $(this).attr('value', $(this).attr('rel')); });
		});
	};
})(jQuery);
 
/* 
    SLIDESHOW
    Lightbox pages
		Pass the number of the slide you want to show initially, eg. play_entertainment#1 shows 2nd slide
*/

(function($){
	jQuery.fn.slideShow = function(options) {
		
		ssSettings = jQuery.extend({
			 menuContainer:   '.slideshow_menu',
			 slidesContainer: '.slideshow_content'
		}, options);
	
		return this.each(function() {
			//												
 			var $container = $(this);
			var $slides = $(ssSettings.slidesContainer).children();		
			var $menu = $(ssSettings.menuContainer);    
      var $menuItems = $('a',$menu);
			var previousSlide;
			
			// position slides 
			$container.css('position', 'relative');
			$slides.each( function(i){
				$($slides[i]).css({'z-index':$slides.length-i,'position':'absolute','top':0,'left':0}).hide();
			});
			// add click events slideshow menu
	    $('a',$menu).each(function(i) {
						$(this).click(function(e) { gotoSlide(i); e.preventDefault(); });											 
				}); 
			// determine which slide to show initially
			var initSlide = 0;
      var h = window.location.hash;
			if(h!='') { initSlide = parseInt(h.replace('#',''));  };
			// go to initial slide
		  gotoSlide(initSlide);

			// Go to slide 
			function gotoSlide(index) {
				
				 if (index!=previousSlide) {
					 
					 // hide previous slide
						 if (previousSlide!=undefined) { 
							 $($menuItems[previousSlide]).removeClass('selected');						 
							 // remove video
							 if ( $($slides[previousSlide]).hasClass('video_item') ) { 
								 var relAttr = $($slides[previousSlide]).attr('rel').split('|');;
								 var videoContainer = relAttr[1]; 
								 $($slides[previousSlide]).html('<div id="'+videoContainer+'" class="video"><p><a href="http://www.adobe.com/go/getflashplayer"><img src="/images/lx/img/global/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>');
							 }
							 $($slides[previousSlide]).hide();
						 }
					
					 // show current slide
					   $($menuItems[index]).addClass('selected');
						 
						 
					 // embed video
						 if ( $($slides[index]).hasClass('video_item') ) { 
						 
						   var relAttr = $($slides[index]).attr('rel').split('|');
						 	 var videoLocation  = relAttr[0];
						   var videoContainer = relAttr[1]; 							
							
							 if (videoLocation.indexOf('youtube')!=-1) {
								 // EMBED YOU TUBE VIDEO PLAYER
								 $('#'+videoContainer).html('<object width="640" height="360"><param name="movie" value="'+videoLocation+'&color1=0x333333&color2=0x000000"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+videoLocation+'&color1=0x333333&color2=0x000000" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="360"></embed></object>');								 
							 } else {
								 // EMBED CUSTOM FLASH PLAYER
								 var flashvars = {
										xmldata: videoLocation
								 };
								 var params = {
										scale: 'noScale',
										allowfullscreen: 'true',
										bgcolor: '#000000',
										wmode: 'opaque',
										allowscriptacces: 'always'
								 };
								 var attributes = {};
								 swfobject.embedSWF("/files/swf/videoplayer/pionplayer.swf", videoContainer, "640", "360", "9.0.0", "swf/videoplayer/expressInstall.swf", flashvars, params, attributes);
							 } 
						
						 };				
					
					// embed sound
						 if ( $($slides[index]).hasClass('sound_item') ) { 

						   var relAttr = $($slides[index]).attr('rel').split('|');
						 	 var soundLocation  = relAttr[0];
						   var soundContainer = relAttr[1]; 
							 var flashvars = {
								 mp3: soundLocation,
								 auto: true
							 };
							 var params = {
								 wmode: 'transparent',
								 allowscriptacces: 'always'
							 };
							 var attributes = {};
						 	 swfobject.embedSWF("/images/ipod_speaker_system/swf/mp3player.swf", soundContainer, "412", "35", "9.0.0", "/images/ipod_speaker_system/swf/expressInstall.swf", flashvars, params, attributes);
						
						 };				
					
						 
						 
					   $($slides[index]).show();
					   previousSlide = index;
						 
				 };
			};			
		});
	};
})(jQuery);



/*
    LIGHTBOX
*/

(function($) {
	jQuery.fn.lightbox = function(options) {
		
		// defaults, override with options
		lbSettings = jQuery.extend({
		  overlayId : 'overlay_885-457',
			lightboxId : 'lightbox_885-457',
			overlayClass: 'lightbox_overlay',
			lightboxClass: 'lightbox',
			closeButtonClass: 'lightbox_close',
			contentClass: 'lightbox_content',
			width : 885,
			height : 457,
			source: 'blank.html'
		}, options);
	  
		return this.each(function(){
			// lightbox html
		  var lightbox = '';
			lightbox += '<div id="' + lbSettings.overlayId + '" class="' + lbSettings.overlayClass + '"></div>';
			lightbox += '<div id="' + lbSettings.lightboxId + '" class="' + lbSettings.lightboxClass + '" style="width:' + (lbSettings.width) + 'px; height:' + (lbSettings.height) + 'px; margin-top:-' + Math.round(lbSettings.height/2) + 'px; margin-left:-' + Math.round(lbSettings.width/2) + 'px;">';
			lightbox += '<div class="' + lbSettings.closeButtonClass + '"></div><div class="' + lbSettings.contentClass + '">';
			lightbox += '<iframe width="'+(lbSettings.width+30)+'" height="'+(lbSettings.height+30)+'" frameborder="0" scrolling="no" allowtransparency="true" src="' + lbSettings.source + '">&lt/iframe>';
			lightbox += '</div></div>';	
			// the iframe is bigger than the overlay to hide scrollbars in chrome, opera
			jQuery.fn.open = function() {

				if ( lbSettings.source.indexOf('360view.html')!=-1 && navigator.appVersion.indexOf("Mac")!=-1 && navigator.userAgent.toLowerCase().indexOf('safari') != -1 ) {
						// Safari on Mac	
						// Jump to page bottom when 360 view opens -- if the header flash is on screen the 360 flash in the iframe doesn't load
					  location.href = '#choose';
				}
				
				$lightbox = $('body').append(lightbox);
				
				if (ie6) {
					// position for IE6
					//$('html').css({'height':'100%','overflow-y':'hidden'});
					// position background overlay
					lightboxPos = $('.'+lbSettings.lightboxClass).offset();
					$('.'+lbSettings.overlayClass).css({'background-position':'center '+(lightboxPos.top-150)});
					$('.'+lbSettings.overlayClass).focus();				
					$(window).resize( function() { 
					  if ($('.'+lbSettings.lightboxClass).length!=0) {												 
					  	lightboxPos = $('.'+lbSettings.lightboxClass).offset();
					  	$('.'+lbSettings.overlayClass).css({'background-position':'center '+(lightboxPos.top-150)}); 
						};
				  });
				} /*else { 
				  
				}*/
				/*if (ie6 ) {
					$('html').css({'height':'100%','overflow-y':'hidden'});
				}*/
				
				
				$('.'+lbSettings.closeButtonClass).click(function(){$lightbox.close();});
				$('.'+lbSettings.overlayClass).click(function(){$lightbox.close();});
			

				
			}	
			
			

			jQuery.fn.close = function() {
				/*if (ie6) {
				  $('html').css({'height':'auto','overflow-y':'scroll'});
				}*/
				$('.'+lbSettings.overlayClass).remove();
				$('.'+lbSettings.lightboxClass).remove();
			}
			
			
    });

	};	
})(jQuery);


/* jQueryString v1.7.1 - Minified Version
   By James Campbell 
   Many thanks to Mike Willis for his suggestions and additions to this jQuery plugin.
*/
(function($){$.getAllQueryStrings=function(options){ options = $.extend({URL:location.href}, options); var a = unescape(options.URL).split("?")[1];var b=new Array();if(typeof(a)!="undefined"){a=a.split('&');$.each(a,function(i){var c=this.split('=');b[c[0]]=b[i]={name:c[0],value:(function(){if(c.length == 2){return c[1]}else{return c[0];}})()};});}return b;};$.QueryStringExsist=function(options){return(typeof($.getAllQueryStrings()[options.ID])!="undefined");};$.getQueryString=function(options){options=$.extend({URL:location.href,onStart:function(options){},onError:function(options){},onSuccess:function(options,d){},callback:function(options,d){}},options);var d=options.DefaultValue;options.onStart(options);if($.QueryStringExsist({ID:options.ID})){d=$.getAllQueryStrings(options)[options.ID].d;options.onSuccess(options,d);}else{options.onError(options);}options.callback(options,d);return d;};})(jQuery);