/* -------------------------------------------------------------- 
  
   Pioneer NavGate Product Site
	 custom.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;

// carfinder
function goToCaspdBrand() {
	var brandSelect = document.getElementById("caspd_brand");
	if( brandSelect && brandSelect.selectedIndex != 0 ) {
		 window.location.href = brandSelect.options[brandSelect.selectedIndex].value;
	};
};

// change window location from link inside lightbox iframe 
function changeTopLocation(url) {
	window.location.href = url;	
};

$j(document).ready(function () {

		// Clickable lightbox item
		$j('#play_cross-sell_0').css({'cursor':'pointer'});

    // Disable links to  AVIC-F320BT, AVIC-F9210BT and AVIC-F3210BT - Products go live in june
		$j('.disabled').each( function() {
				$j(this).css('cursor','text'); 
				$j(this).click( function(e) { e.preventDefault(); });
		}); 
		

	  // Toggle
		$j('.toggle').toggler();

	  // Toggle table
		var counter = 0;
		$j('tr.toggle_table_row td').each( function() {
		  $j(this).html('<span class="toggle_table_cell">'+ $j(this).html() +'</span>');															 
		});	
		$j('.toggle_table_cell').hide();
					
		$j('.toggle_table').tableToggler();

		// Slideshow
		$j('.slideshow').slideShow();		

		// Lightboxes
		$j('#explore .thumbs li a, #play .thumbs li a, #introduction .thumbs li a, .open_lightbox, .thumbs_large li a').each( function() {
			$j(this).each( function() {																
				$j(this).click( function(e) { 
					var source = $j(this).attr('href');  
					var lightbox = jQuery.fn.lightbox({ 
						 overlayId : 'overlay_885-457',						
						 lightboxId : 'lightbox_885-457',
						 width : 885,
						 height : 457,
						 source: source
					});					
					lightbox.open() 
					e.preventDefault();
				});
			});
		});

		// Slideshow
		$j('.carousel').carousel();		
														
});







/* 
    CAROUSEL
*/

(function($){
	jQuery.fn.carousel = function(options) {
		
		settings = jQuery.extend({
			scroller: 'zoom_tool_scroller'														 
 		}, options);

		return this.each(function() {
								  
			 var $carousel = $(this);
			 var $carouselFrame = $('.carousel_frame',$carousel);
			 var $carouselMover = $('ul',$carousel);
		   var $carouselItems = $('li',$carousel);
			 var $next;
			 var $prev;
			 var selectedItem = 0;
			 var currentPos = 1;
			 var nrVisibleItems = 4;
			 var nrItems = $carouselItems.size();
       
			 $carouselFrame.css({'width':'800px','overflow':'hidden'});
			 $carouselMover.css({'width':'9999px','position':'absolute','left':'0px','top':'0px'});
			 
			 if($carouselItems.size()<=nrVisibleItems) {
				  //$next.addClass('next_button_disabled');
					//$prev.addClass('previous_button_disabled');
			 } else {
				  $next = $('<div class="carousel_next"></div>').prependTo($carousel); 
					$prev = $('<div class="carousel_prev"></div>').prependTo($carousel); 
			 	  $next.click( function(e) { 
					  moveCarousel(currentPos+nrVisibleItems);
					  e.preventDefault(); 
				  });
				  $prev.click( function(e) { 
					  moveCarousel(currentPos-nrVisibleItems);
				 	  e.preventDefault(); 
				  });					 
			 }

       function moveCarousel(pos) {
				 //alert(pos);
         if (pos<0) pos = 0;
		     else if (nrItems>nrVisibleItems && pos>nrItems-nrVisibleItems) pos = nrItems-nrVisibleItems;
				 
				 var left = Number($carouselMover.css('left').replace('px',''));
			   //$carouselMover.animate({'left':(-200*pos)+'px'}, 300);
				 $carouselMover.animate({'left':(-200*pos)+'px'}, 300);
				 //$carouselMover.css('left','-30px');
				 currentPos = pos;
				 if ($prev) {
					 if (pos==0) $prev.addClass('carousel_prev_disabled');
					 else $prev.removeClass('carousel_prev_disabled');
				 };
				 if ($next) {
					 if (pos==$carouselItems.size()-nrVisibleItems) $next.addClass('carousel_next_disabled');
					 else $next.removeClass('carousel_next_disabled');
				 };
			 };
			 moveCarousel(0);
				 
		});
		
		
		
		
	};
})(jQuery);
   
/* 
    TABLE TOGGLER
*/

(function($){
	jQuery.fn.tableToggler = function(options) {
		
		settings = jQuery.extend({
			 openClass: 	'open_toggle_table',
			 closedClass: 	'close_toggle_table',
			 contentClass:	'toggle_table_row'
		}, options);

		return this.each(function(i) {
								  
									
			var $handle = $(this);
			var $content = $('span.toggle_table_cell');			
			
			$('.toggle_table').addClass('close_toggle_table');
			/*
			if ($handle.hasClass('open_toggle_table')) {
				$content.show();
			} else {
				$handle.addClass('close_toggle_table');
				$content.hide();
			};
			*/
			
			$handle.mouseover( function() { $handle.addClass('hover'); }).mouseout( function() { $handle.removeClass('hover'); });
			$handle.click( function() { toggleContent(); });
			
			
			function toggleContent() {
				 
				if ($handle.hasClass('open_toggle_table')) {
					$('.toggle_table').removeClass('open_toggle_table');
					$('.toggle_table').addClass('close_toggle_table');
					//$content.slideUp();
				  $content.hide();
					//$('tr.toggle_table_row td:eq('+(i)+') span.toggle_table_cell').hide();
				} else {
					$('.toggle_table').removeClass('close_toggle_table');
					$('.toggle_table').addClass('open_toggle_table');
					//$content.slideDown();
				  $content.show();
					//$('tr.toggle_table_row td:eq('+(i)+') span.toggle_table_cell').show();
				};
				
			};
			
			
		});
		
		
		
		
	};
})(jQuery);



/* 
    TOGGLER
*/

(function($){
	jQuery.fn.toggler = function(options) {
		
		settings = jQuery.extend({
			 openClass: 	'open_toggle',
			 closedClass: 	'close_toggle',
			 contentClass:	'toggle_content'
		}, options);

		return this.each(function() {
								  
			var $handle = $(this);
			var $content = $(this).next('.'+settings.contentClass);
			
			if ($handle.hasClass('open_toggle')) {
				$content.show();
			} else {
				$handle.addClass('close_toggle');
				$content.hide();
			};
				
			$handle.mouseover( function() { $handle.addClass('hover'); }).mouseout( function() { $handle.removeClass('hover'); });
			$handle.click( function() { toggleContent(); });
			
			
			function toggleContent() {
				 
				if ($handle.hasClass('open_toggle')) {
					$handle.removeClass('open_toggle');
					$handle.addClass('close_toggle');
					$content.slideUp();
				} else {
					$handle.removeClass('close_toggle');
					$handle.addClass('open_toggle');
					$content.slideDown();
				};
				
			};
			
			
		});
		
		
		
		
	};
})(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:   '.slideshow_menu',
			 slidesContainer: '.slideshow_content'
		}, options);
	
		return this.each(function() {
			//												
 			var $container = $(this);
			var $slides = $(settings.slidesContainer).children();		
			var $menu = $(settings.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="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 videoContainer = relAttr[1]; 
							 var videoXml = relAttr[0];
							 var flashvars = {
									xmldata: videoXml
							 };
               var params = {
                  scale: 'noScale',
                  allowfullscreen: 'true',
									bgcolor: '#000000',
									wmode: 'opaque'
               };
               var attributes = {};
               swfobject.embedSWF("swf/videoplayer/pionplayer_western.swf", videoContainer, "640", "360", "9.0.0", "swf/videoplayer/expressInstall.swf", flashvars, params, attributes);
						 };			
					   $($slides[index]).show();
					   previousSlide = index;
						 
				 };
			};			
		});
	};
})(jQuery);



 /*
    LIGHTBOX
*/

(function($) {
	jQuery.fn.lightbox = function(options) {
		
		// defaults, override with options
		settings = 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="' + settings.overlayId + '" class="' + settings.overlayClass + '"></div>';
			lightbox += '<div id="' + settings.lightboxId + '" class="' + settings.lightboxClass + '" style="width:' + (settings.width) + 'px; height:' + (settings.height) + 'px; margin-top:-' + Math.round(settings.height/2) + 'px; margin-left:-' + Math.round(settings.width/2) + 'px;">';
			lightbox += '<div class="' + settings.closeButtonClass + '"></div><div class="' + settings.contentClass + '">';
			lightbox += '<iframe id="lightbox_frame" width="'+(settings.width+30)+'" height="'+(settings.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 ( $('.'+settings.overlayClass).size() > 0 ) {
					$('.'+settings.overlayClass).remove();
					$('.'+settings.lightboxClass).remove();				
				};
 
				$lightbox = $('body').append(lightbox);
				
				
				$('#lightbox_frame').attr('src',settings.source);
								
				
				if (ie6) {
					// position for IE6
					$('html').css({'height':'100%','overflow-y':'hidden'});
					// position background overlay
					lightboxPos = $('.'+settings.lightboxClass).offset();
					$('.'+settings.overlayClass).css({'background-position':'center '+(lightboxPos.top-150)});
					$('.'+settings.overlayClass).focus();				
					$(window).resize( function() { 
					  if ($('.'+settings.lightboxClass).length!=0) {												 
					  	lightboxPos = $('.'+settings.lightboxClass).offset();
					  	$('.'+settings.overlayClass).css({'background-position':'center '+(lightboxPos.top-150)}); 
						};
				  });
				} else { 
				  $('html').css({'height':'100%','overflow-y':'hidden'});
				}
				$('.'+settings.closeButtonClass).click(function(){$lightbox.close();});
				$('.'+settings.overlayClass).bgIframe();
				$('.'+settings.overlayClass).click(function(){$lightbox.close();});
			

				
			}	
			
			

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

	};	
})(jQuery);


/*
    ----- PLUGINS --------------------------------------------------
*/

/* BGIFRAME
 * Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-07-21 18:44:59 -0500 (Sat, 21 Jul 2007) $
 * $Rev: 2446 $
 *
 * Version 2.1.1
 */
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(b($){$.m.E=$.m.g=b(s){h($.x.10&&/6.0/.I(D.B)){s=$.w({c:\'3\',5:\'3\',8:\'3\',d:\'3\',k:M,e:\'F:i;\'},s||{});C a=b(n){f n&&n.t==r?n+\'4\':n},p=\'<o Y="g"W="0"R="-1"e="\'+s.e+\'"\'+\'Q="P:O;N:L;z-H:-1;\'+(s.k!==i?\'G:J(K=\\\'0\\\');\':\'\')+\'c:\'+(s.c==\'3\'?\'7(((l(2.9.j.A)||0)*-1)+\\\'4\\\')\':a(s.c))+\';\'+\'5:\'+(s.5==\'3\'?\'7(((l(2.9.j.y)||0)*-1)+\\\'4\\\')\':a(s.5))+\';\'+\'8:\'+(s.8==\'3\'?\'7(2.9.S+\\\'4\\\')\':a(s.8))+\';\'+\'d:\'+(s.d==\'3\'?\'7(2.9.v+\\\'4\\\')\':a(s.d))+\';\'+\'"/>\';f 2.T(b(){h($(\'> o.g\',2).U==0)2.V(q.X(p),2.u)})}f 2}})(Z);',62,63,'||this|auto|px|left||expression|width|parentNode||function|top|height|src|return|bgiframe|if|false|currentStyle|opacity|parseInt|fn||iframe|html|document|Number||constructor|firstChild|offsetHeight|extend|browser|borderLeftWidth||borderTopWidth|userAgent|var|navigator|bgIframe|javascript|filter|index|test|Alpha|Opacity|absolute|true|position|block|display|style|tabindex|offsetWidth|each|length|insertBefore|frameborder|createElement|class|jQuery|msie'.split('|'),0,{}))

/*_*/
