/* -------------------------------------------------------------- 
  
   Pioneer VSX-AV Amplifier Product Site
	 custom.js
   
-------------------------------------------------------------- */

var mp3PlayerLocation = '../../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();

		// 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.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) {
		
		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);	
									 }  
									 
							 }
							 
						 };
						 
             // 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", "../../swf/expressInstall.swf", flashvars, params, attributes);
						
						 };							 
						 
						
					   $($slides[index]).show();
					   previousSlide = index;

						 
				 };
			};	
			
		});
	};
})(jQuery);


function isInteger(value) {
	return Math.floor(value) == value ? true : false; 
}


/*
    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() {
			  
				
				
				$lightbox = $('body').append(lightbox);
				
				$('#lbFrame').attr('src',lbSettings.source);
				
				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)}); 
						};
				  });
				} 
				
				$('.'+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);


/* 
    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);