/* -------------------------------------------------------------- 
  
   Pioneer AVR Landing Page
   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;
var ie8 = ($j.browser.msie && $j.browser.version > 7 ) ? true : false;
  
$j(document).ready(function () {

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

		// Lightboxes
		$j('.open_lightbox').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 : 887,
						 height : 459,
						 source: source
					});					
					lightbox.open() 
					e.preventDefault();
				});
			});
		});
		
		// Image map with rollovers Box page 
		$j('.carousel').carousel();		

		//
		$j('.product').switchColour();

		// Tabs
		$j('.tabs').tabs();
		
		// Tooltip
		$j('.tooltip').tooltip({width:'175px',leftOffset:-15,tooltipContainer:'tooltip'});

		// Image map with rollovers Box page 
		$j('.image_map').imageMap();

		// Solutions page combo tabs
		$j('#combo_tab_container').solutionsTabs();


});




/*
    SWITCH COLOUR
		Switch between Black & Silver products
*/

(function($) {
	jQuery.fn.switchColour = function(options) {
		
		// defaults, override with options
		settings = jQuery.extend({
			triggerSelector : '.colours a',
			contentSelector : '.detail>div',
			introSelector	: '.shortdesc'
		}, options);
		
		return this.each(function(){
	    
	      var $trigger = $(settings.triggerSelector,$(this));
			  var $content = $(settings.contentSelector,$(this));
				var $intro = $(settings.introSelector,$(this));
			  var switchIntro = 0;
				
			  if ($intro.hasClass('H')||$intro.hasClass('K')||$intro.hasClass('S')) {
					  $intro.hide();
						switchIntro = 1;
				}
			  // if more than one colour
			  if( $trigger.size() > 1 ) {
				   
			      $trigger.each( function(i) {
						    $(this).click( function(e) {
							      activateColour(i);	
										e.preventDefault();
					      });											 
					  });
					   
					  function activateColour(index) {
					  		
								$trigger.removeClass('active');
								//$content.hide();
								$content.css("left", "2000px");
								
								$($trigger[index]).addClass('active');
								//$($content[index]).show();
						    $($content[index]).css("left", "0");
								
								if(switchIntro) {
									  $intro.hide();
										$($intro[index]).show();
								};
								
					  };
						
					  activateColour(0);
        };
			 
    });
		
	};
})(jQuery);

/*
    CAROUSEL
*/

(function($) {
	jQuery.fn.carousel = function(options) {
		
		// defaults, override with options
		settings = jQuery.extend({
			
		}, options);
		
		return this.each(function(){
	     
			 var $scroller = $(this);
			 var $mover = $('ul',$scroller);
		   var $scrollerItems = $('li',$mover);
			 var $next;
			 var $prev;
			 var selectedItem = 0;
			 var currentPos = 1;
			 var nrVisibleItems = 1;
			 var itemWidth = $($scrollerItems[0]).width();
			 
			 //alert(itemWidth);
			 
			 if($scrollerItems.size()<=nrVisibleItems) {
				  //$next.addClass('next_button_disabled');
					//$prev.addClass('previous_button_disabled');
			 } else {
				  $next = $('<a class="next"></a>').appendTo($scroller); 
					$prev = $('<a class="prev"></a>').appendTo($scroller); 
			 	  $next.click( function(e) { 
					  moveScroller(currentPos+nrVisibleItems);
					  e.preventDefault(); 
				  });
				  $prev.click( function(e) { 
					  moveScroller(currentPos-nrVisibleItems);
				 	  e.preventDefault(); 
				  });					 
			 }
		  
       function moveScroller(pos) {
				 //alert('moveScroller' + pos + ' prev ' + $prev + ' next ' + $next);
         if (pos<0) {pos = 0}
		     else if ($scrollerItems.size()>nrVisibleItems && pos>$scrollerItems.size()-nrVisibleItems) {pos = $scrollerItems.size()-nrVisibleItems;}
				 
				 var left = Number($mover.css('left').replace('px',''));
			   $mover.animate({'left':(-itemWidth*pos)+'px'}, 300);
				 currentPos = pos;
				 if ($prev) {
					 if (pos==0) $prev.addClass('previous_button_disabled');
					 else $prev.removeClass('previous_button_disabled');
				 };
				 if ($next) {
					 if (pos==$scrollerItems.size()-nrVisibleItems) {$next.addClass('next_button_disabled');}
					 else {$next.removeClass('next_button_disabled');}
				 };
			 };
				 
			 function getSelectedItem() {
				 alert('getSelectedItem');
				 //$scrollerLinks.each( function(i) {
					 //if( $('img',$(this)).attr('src').indexOf(visual + '_') > 0  ) {
						 //selectedItem = i; 
					 //};
				 //});				
			 };

			moveScroller(0);
			 
		});
		
	};
})(jQuery);


/*
    IMAGE MAP with custom ROLLOVERS
*/

(function($){
	jQuery.fn.imageMap = function(options) {
		return this.each(function() { 
			var $map = $(this);
      var $areas = $('area',$(this));
			// replace map areas by transparent links
      $areas.each( function(i) {
				var $area = $(this);
				var href = $(this).attr('href');						
        var text = $(this).attr('alt');
				var pos = $area.attr('coords').split(',');
				var left = pos[0];
				var top = pos[1];				
				var width = pos[2]-pos[0];
				var height = pos[3]-pos[1];
				$map.prepend('<a href="../../avr/js/'+href+'" style="cursor:help;display:block;position:absolute;z-index:'+(i+1100)+';left:'+left+'px;top:'+top+'px;width:'+width+'px;height:'+height+'px;" title="'+text+'"><img src="/images/global/blank.gif" width="'+width+'" height="'+height+'" /></a>');
				$area.remove();
			});
			// add mouseover/out tooltip
			$('a',$map).click( function(e) {e.preventDefault();});
			$('a',$map).tooltip({width:'auto',leftOffset:'center'});
		});
	};
})(jQuery);



/*
    TOOLTIP
*/

(function($){
  jQuery.fn.tooltip = function(options) {
		
		ttSettings = jQuery.extend({
			tooltipContainer : 'tooltip',										 
			leftOffset : 0,  // nr of pixels to move tooltip to left(-) or right(+), or 'center' ( tooltip is centered tov link ) 
			width: 'auto'    // width in px or auto (width automatically takes width of tooltip content )
		}, options);
		
		return this.each(function() {

      var $a = $(this); // link
			var offset;	
			
			// change TITLE attribute of link to REL attr
			$a.attr('rel',$a.attr('title'));
			$a.attr('title','');

      // add mouseover/out event on link
      $(this).mouseover( function(e) {showTooltip();} ).mouseout( function() {hideTooltip()} );

			// show tooltip
			function showTooltip() { 
			
			  // Append #tooltip container to the page if it doesn't exist
				//if ($('#'+ttSettings.tooltipContainer).length == 0 ) { $('body').append('<div id="'+ttSettings.tooltipContainer+'" style="width:'+ttSettings.width+'"><div class="tl"><div class="tr"></div></div><div class="bg_l clearfix"><div class="bg_r"><div id="tooltip_content"></div></div></div><div class="bl"><div class="br"></div></div></div>');  
				if ($('#'+ttSettings.tooltipContainer).length == 0 ) {$('body').append('<div id="'+ttSettings.tooltipContainer+'" style="width:'+ttSettings.width+'"><div class="tl"><div class="tr"></div></div><div class="bg_l clearfix"><div class="bg_r"><div id="tooltip_content"></div></div></div><div class="bl"><div class="br"></div></div></div>');  
				}
				var $tooltip = $('#'+ttSettings.tooltipContainer);
				
				// Set tooltip text
				$('#tooltip_content',$tooltip).html($a.attr('rel'));
				
				// Position tooltip and show it
				var pos = $a.offset();			
				if (ttSettings.leftOffset == 'center' ) {
					offset = Math.round(pos.left+$a.width()/2)-20;
			  } else { 
					offset = pos.left+ttSettings.leftOffset; 
				}
				var height = $tooltip.height();
				
				$('#'+ttSettings.tooltipContainer).css({'position':'absolute','left':+offset+'px','top':Math.round(pos.top-height),'z-index':'999'}).show();
		    
				// Take care of width 'auto' in ie6
				if (ie7 ) {if(ttSettings.width=='auto') {$('#'+ttSettings.tooltipContainer).css({'width':'150px'});}}
				if (ie6) {
					if(ttSettings.width=='auto') {
						$('#'+ttSettings.tooltipContainer).css({'width':'100px'});
						$('#'+ttSettings.tooltipContainer+' .tl').css({'width': $('#tooltip_content',$tooltip).width()+33});
						$('#'+ttSettings.tooltipContainer+' .bl').css({'width': $('#tooltip_content',$tooltip).width()+47});
						$('#tooltip_content',$tooltip).css({'white-space':'nowrap'});
					}
				}
				// hide selectboxes in ie6
				if (ie6) $('select').css({'visibility':'hidden'}); 
				
			};
			function hideTooltip() { 
			  $('#'+ttSettings.tooltipContainer).hide(); 
				// show selectboxes in ie6
			  if(ie6) $('select').css({'visibility':'visible'}); 
			};
		});
  };
})(jQuery);


/* 
    TABS
*/
(function($) {
	jQuery.fn.tabs = function(options) {
		
		settings = jQuery.extend({
			tabs : '.tabs .tab_menu a',										 
			contentContainer : '.tab_content_container',
			contentItems : '.tab_content'
		}, options);
	
		return this.each(function(){
			//
			var $tabs = $(settings.tabs);
			var $contentContainer = $(settings.contentContainer);
			var $contentItems = $(settings.contentItems);
			var selectedTabIndex;
			
			// if the url does not contain a tab ID, the VSX-920 is shown by default
			var h = window.location.hash;			
			var initTab = ( h!='' && $(h).size()>0 ) ? h : '#VSX-920';
			
			// hide content items and show initial tab
			showTab(initTab);
			
			$tabs.each( function() {
				$(this).click( function(e) {
					var id = $(this).attr('href');
					showTab(id);
					//e.preventDefault(); // Do not prevent default because we need the URL fragment to be set
			});
		}); 
	
			function showTab(tabId) { 
			   // get index of tab
				 var tabIndex;
				 $tabs.each( function(i) {if($(this).attr('href') == tabId) tabIndex = i;});
				 // deselect previous tab
				 if(selectedTabIndex!= undefined) {
					 $($tabs[selectedTabIndex]).removeClass('selected');		
					 $($contentItems[selectedTabIndex]).css("z-index", "10");
				 }			 
				 // select this tab
				 $($tabs[tabIndex]).addClass('selected');	
				 $($contentItems[tabIndex]).css("z-index", "12");
				 selectedTabIndex = tabIndex;
			};
	
		});
	
	};	
})(jQuery);
 
/*
(function($) {
	jQuery.fn.tabs = function(options) {
		
		settings = jQuery.extend({
			tabs : '.tabs .tab_menu a',										 
			contentContainer : '.tab_content_container',
			contentItems : '.tab_content'
		}, options);
	
		return this.each(function(){
			//
			var $tabs = $(settings.tabs);
			var $contentContainer = $(settings.contentContainer);
			var $contentItems = $(settings.contentItems);
			var selectedTabIndex;
			
			// if the url does not contain a tab ID, the first tab is shown by default
			var h = window.location.hash;
			var initTab = ( h!='' && $(h,settings.contentContainer).length ) ? h : $($tabs[0]).attr('href');
			
			// hide content items and show initial tab
			$contentItems.each( function() { $(this).hide(); });
			showTab(initTab);
			
			$tabs.each( function() {
				//$(this).click( function(e) {
				$(this).mouseenter( function(e) {
			    var id = $(this).attr('href');
				  showTab(id); e.preventDefault();					
			  });										
		  }); 
	
			function showTab(tabId) { 
			   // get index of tab
				 var tabIndex;
				 $tabs.each( function(i) { if($(this).attr('href') == tabId) tabIndex = i; });
				 // deselect previous tab
				 if(selectedTabIndex!= undefined) {
					 $($tabs[selectedTabIndex]).removeClass('selected');		
					 $($contentItems[selectedTabIndex]).hide();					 
				 }			 
				 // select this tab
				 $($tabs[tabIndex]).addClass('selected');	
				 $($contentItems[tabIndex]).show();  
				 selectedTabIndex = tabIndex;
			};
	
		});
	
	};	
})(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 width="'+(settings.width)+'" height="'+(settings.height)+'" frameborder="0" scrolling="no" allowtransparency="true" id="lightbox_iframe" 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);
				
				
				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'});
				}

	      $('#lightbox_iframe').attr('src',settings.source);
	      $('#lightbox_iframe').load( function() {$(this.contentDocument).find(".lightbox_footer").hide();});
 
				$('.'+settings.closeButtonClass).click(function(){$lightbox.close();});
				$('.'+settings.overlayClass).click(function(){$lightbox.close();});
				
			}	
			
			

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

	};	
})(jQuery);


/*
    SOLUTIONS PAGE COMBO TABS
*/
(function($) {
	jQuery.fn.solutionsTabs = function(options) {

		settings = jQuery.extend({
			tabs : '#combo_tabs li',
			contentContainer : '#combo_tab_content',
			contentItems : '#combo_tab_content>div'
		}, options);

		return this.each(function(){
			var $tabs = $(settings.tabs);
			var $contentContainer = $(settings.contentContainer);
			var $contentItems = $(settings.contentItems);

			$tabs.each( function() {
				$(this).bind('mouseenter', function() {
					$(this).addClass("hover").siblings().removeClass("hover");
				});
				$(this).bind('mouseleave', function() {
					$(this).removeClass("hover");
				});
				$(this).bind('click', function() {
					$(this).addClass("currentTab").siblings().removeClass("currentTab");
					var index = $tabs.index($(this));
					$contentItems.eq(index).show().siblings().hide();
				});
			});

		});

	};
})(jQuery);


/*
 -----------------------------------------------------------------
*/


function isInteger(value) {
	return Math.floor(value) == value ? true : false; 
}
