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

var mp3PlayerLocation = '/images/ipod-dock/swf/mp3Player.swf';

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

$(document).ready(function () {

		// Rollover product images overview page
		$('.page_home .frame .bd').rolloverBoxes();
    
		// KODO rollover
		$('.menu h1 a').mouseover( function() { $('#kodo_ro').show(); }).mouseout( function() { $('#kodo_ro').hide(); });
		
		// 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();		

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

		// 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 : 887,
						 height : 459,
						 source: source
					});			
					e.preventDefault();			
					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();
				
				});
			});
		});		

		// Open 360°view lightbox onload if ?lb=3d
		if($.getAllQueryStrings()['lb'] && $.getAllQueryStrings()['lb'].value=='3d') {
					var source = '360view.html';
					var lightbox = jQuery.fn.lightbox({ 
						 overlayId : 'overlay_960-610',						
						 lightboxId : 'lightbox_960-610',
						 width : 960,
						 height : 610,
						 source: source
					});			
					lightbox.open();
		}

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

		
});


/* 
    ROLLOVERS BOXES
		Rollover product images overview page
*/

(function($){
	jQuery.fn.rolloverBoxes = function() {
    
		return this.each(function() { 
			 
			 //settings												
			 var shrinkPercentage = 0.6; 
			 
			 //var
			 var $this = $(this);
			 var $boxes   = $('.ro',$this);
			 var $visuals = $('.vi',$this);
			 var fullW   = $('.vi:eq(0) img',$this).width();
			 var fullH   = $('.vi:eq(0) img',$this).height();
 			 var shrinkW = fullW*shrinkPercentage;
			 var shrinkH = fullH*shrinkPercentage;
			 var currentRollover;
	     var timeout;
       
			 //show tooltip on mouseover product images
			 $visuals.each( function(i) {
					$(this).mouseenter( function(e) {
							$($boxes[i]).css('bottom','100px').show().animate({bottom:'50px'},200);
							currentRollover = i;
							clearTimeout(timeout);
							setSelectedVisual();
					});
			 }); 
			 
			 
			 $boxes.each( function(j) {
		            $(this).css('cursor','pointer'); 						   
					//hide tooltip on mouseout 
					$(this).mouseleave( function(e) {
							currentRollover = 'undefined';
							$($boxes[j]).hide();
							clearTimeout(timeout);
							timeout = setTimeout(setSelectedVisual,200);
					});
					
					//make entire box clickable
					$(this).click( function(e) {
						if ( $('a:eq(0)',$(this)).size() > 0 ) {					
							var url = $('a:eq(0)',$(this)).attr('href');
							location.href = url; 
						}
					});
					
			 }); 
			 
 
			 //on mouseover product image, the other products are resized and faded out
			 setSelectedVisual = function() {
				 $visuals.each( function(k) {
						if (currentRollover=="undefined") {
							  transformVisual($('img',$(this)), 1, fullW, fullH, 300);
						} 
						else {
							if (currentRollover==k) { 
							  transformVisual($('img',$(this)), 1, fullW, fullH, 150);
							}
							else { 
							  transformVisual($('img',$(this)), 0.5, shrinkW, shrinkH, 300);
							}						
						}
				 }); 
			 };
			 
			 //
       transformVisual = function($o,opacity,w,h,speed) {
				  if(!ie) { // resizing transparent PNG doesn't look good in IE
					   
				     $o.stop().animate({
								     'opacity': opacity,
										 'width':  w,
										 'height': h
								 },
						     speed
						 );		
				  }  
			 };  
			 
		});
		
	};
})(jQuery);



/* 
    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); e.preventDefault(); });
				 $('.prev',$(this)).click( function(e) { var i=current-1; if(i<0) i=$items.length-1; jQuery.fn.showItem(i); e.preventDefault(); });
			 }
			 			
			 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);



 
/* 
    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"></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]; 
							
							 // EMBED YOU TUBE VIDEO PLAYER
								 $('#'+videoContainer).html('<object width="640" height="385"><param name="movie" value="'+videoLocation+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+videoLocation+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>');								 
						
						 };				
					
					// 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(mp3PlayerLocation, soundContainer, "412", "35", "9.0.0", "", 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 id="lbFrame" width="'+(lbSettings.width+30)+'" height="'+(lbSettings.height+30)+'" frameborder="0" scrolling="no" allowtransparency="true" src="">&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
					  location.href = '#choose';
				}
				
				$lightbox = $('body').append(lightbox);
				
				$('#lbFrame').attr('src',lbSettings.source);

				
				
				//$('html').css({'height':'100%','overflow':'hidden'});
				
				if (ie6) {
					// position for IE6
					
					// 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)}); 
						};
				  });
				} 
				
				$('.'+lbSettings.closeButtonClass).click(function(){$lightbox.close();});
				$('.'+lbSettings.overlayClass).click(function(){$lightbox.close();});
				
			}	

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

	};	
})(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);
 

/* 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);
