/*----------------------------------------------------------------------
[Global JavaScript ] 
Desc: site wide javascript

Copyright:		Great Fridays 2010
Client:			Bowers &amp; Wilkins
Project:		Bowers &amp; Wilkins . com
Created by:		Adam Foster
Last Updated:	01 - 03 - 10			
-----------------------------------------------------------------------*/

/* Global > replace fonts ---------------------------------------------*/

//Cufon.replace('#productScroller strong')('#seriesScroller strong', { ignore: {span: 1} })('h1:not(#logo)')('h2')('h3', { hover: true })('.productNav ul li:not(.gallery) a', { hover: true })('.contentWrapper h4');


/* Not looking for any trouble here -----------------------------------*/

var $j = jQuery.noConflict();


/* When the dom is ready... -------------------------------------------*/

$j(document).ready(function() {
	init();
});


/* Global > run some functions  ---------------------------------------*/

function init(){
	
	$j('body').addClass('js');
	if($j.browser.msie) { 
		$j('body').addClass('ie');
	};
	
	/* temp */
	navigation();
	
	hotSpotLinks();
	speakersSlider();
	productListSlider();
	scrollerSetUp();
	showOverlay();
	starRating();
	galleryImages();

	tabContent();
	inputValueReplace();
	
};

/* Global > nav --------------------------------------------------------------------------------------*/

function navigation() {
	
	//$j('#bandw').append("<div id='overlayNav'></div>");
	$j('#siteNav').each(function() { var o = new nav($j(this)); });
	
	function nav(e) {
		
		$j('#countrySelector').hover(
			function () {
				$j(this).find('#selectList').css({display: 'block', opacity: 0}).stop().animate({opacity: 1}, 100, 'easeInOutQuad');
			},
			function () {
				$j(this).find('#selectList').stop().animate({opacity: 0}, 300, 'easeInOutQuad', function() {
					$j(this).css({display: 'none'})
				});
		});

		var o = {
		    init: function() {
		        o.nav = $j(e);
		      // if (!$j.browser.msie) {
		            //o.overlay = $j("#overlayNav").css({ "opacity": 0 });
		            o.nav.children('ul').hover(
						function() {
						    //o.overlay.stop().fadeTo(150, 0.5);
						    if (videoPlayer) videoPlayer.pause();
						   
						    
						},
						function() {
						   // o.overlay.stop().fadeTo(150, 0, function() { o.overlay.css('display', 'none') });
						    if (videoPlayer) {
						        //check if it is playing so the video does not restart each time you leave the navigation
						        if (!videoPlayer.isPlaying()) videoPlayer.play(); 
						    }
						}
					);
		            o.overNav = false;
		      //  }; //if end
		        o.subMenus();
		    },
		    subMenus: function() {

		        o.nav.find('.siteNavSub').each(function() {
		            $j(this).find('> ul > li').each(function(e) {
						if(e > 5) { 
							var i = $j(this);
							i.addClass('bottomOver')
							.find('.info').prependTo(i.find('.siteNavSubSub > div'));
						};
		            });
		        });

		       // if (!$j.browser.msie) {

		            o.nav.find('.cLink').each(function() {

		                var sn = {}
		                sn.li = $j(this);
		              
		                sn.sub = sn.li.children('.siteNavSubSub');
		    		             
		               //test line

                      var timer;
                         sn.li.mouseenter(function(){
					      clearTimeout(timer);
					      timer = setTimeout(mainOver, 300);
					     });
					     
					     sn.li.mouseleave(function (){  
		             	
		             		timer = setTimeout(mainOut, 500);
							
							});
							
					                         
		                //sn.li.hover(mainOver, mainOut);

		                function mainOver() {
		                    		                    
		                    sn.sub.css('z-index', 1000).stop().fadeTo(150, 1); //800 with 150
		                };

		                function mainOut() {
		                    sn.sub.css('z-index', 10).stop().fadeTo(400, 0, function() { sn.sub.css('display', 'none') }); 
		                };

		            });

		       // } //if end
		    }
		};

		if ($j('.homePage').html()) o.init();
		if ($j('.greyTheme').html()) o.init();
		if ($j('.blackTheme').html()) o.init();
		if ($j('.whiteTheme').html()) o.init();
		

};
	
};

/* Global > replace the default value of input fields ------------------------------------------------*/
/* Dependencies - none */

function inputValueReplace() {
  
	$j('input.valueReplace').each(function() { var o = new valueReplace($j(this)); });
	
	function valueReplace(e) {
		
		var o = {
		  init : function() {
				o.field = $j(e); 
				o.isSet = false;
				o.defaultValue = o.field.val();
				o.monitor();
			},
			monitor : function() {
				o.field.focus(function(){
					if( $j(this)[0].value == o.defaultValue) { $j(this)[0].value = ''; }
				});
				o.field.blur(function(){
					if($j(this)[0].value == '') { $j(this)[0].value = o.defaultValue;}
				});
			}
		};
		o.init();
		return o;
		
	};

}; 


/* Global > hotspot link rollovers -------------------------------------------------------------------*/
/* Dependencies - none */

function hotSpotLinks(){
	
	$j('.row').find('.hotLink').each(function() { var o = new linkMe($j(this)); });
	
	function linkMe(e) {

	    var o = {
	        init: function() {
	            o.link = $j(e);
	            o.href = o.link.attr('href');
	            o.title = o.link.attr('title');
	            o.col = $j(e).parents('.col');
	            o.col2 = $j(e).parents('.col2');
	            o.monitor();
	        },
	        monitor: function() {
	            o.link.css('display', 'none');
	            o.col.attr('title', o.title);
	            o.col.addClass('linkCol').click(function() {
	                window.location = o.href;
	            });
	            o.col2.attr('title', o.title);
	            o.col2.addClass('linkCol').click(function() {
	                window.location = o.href;
	            });
	            //.hover(function() { window.status = o.href }, function() { window.status = '' });
	        }
	    };
		o.init();
		return o;
		
	};
	
};



/* Series > hoizontal scroller -----------------------------------------------------------------------*/
/* Dependencies - jquery-ui */

function speakersSlider(){


	$j('#seriesScroller').each(function() { var o = new scrollMe($j(this)); });
	
	function scrollMe(e) {
		
		/* Set vars */
		
		var handleLeft = 0;
		var scrollSnapTo = 0;
		var handleSnapTo = 0;
		var selectedItem = null;
	
		var o = {
			
			/* INIT: Run scroller functions & selector caching */
			
			init : function() {
				o.viewer = $j('.viewer', e);							// Obj - scroller mask
				o.scroller = $j('ul', o.viewer);						// Obj - list of scrolling items
				o.slider = $j('#slider', e);							// Obj - slider
				o.scrollItems = $j('li', o.viewer);						// Objs - array of items to be scrolled
				o.itemsWidth = (o.scrollItems.length * parseInt(o.scrollItems.css('width'))) - parseInt(o.viewer.css('width'));
				o.enableRolls = true;									// Boolean - are we scrolling?
				o.addControls();										// Func - adds additional markup
				if(o.scrollItems.length > 4) o.addScrollFunction();		// Func - adds scroll functionality
				o.addRollovers();										// Func - add hover functionality to items	
				
				// Prev/next buttons for touch devices.
				o.currentIndex = 0;
				o.oldValue = 0;
				o.fromState = null;
				o.overrider = false;
				o.prevButton = $j('.viewer_prev > a', e);
				o.nextButton = $j('.viewer_next > a', e);				
				o.prevButton.bind('click', o.prev);
				o.nextButton.bind('click', o.next);
				
				
				// Swipe left/right gestures for iPhone/iPad.
				o.slider.addTouch();
				o.viewer.multiswipe({
					fingers: 2,
					threshold: 50,
					swipeLeft: function() {
						o.next();	
					},
					swipeRight: function() {
						o.prev();
					},
					swipeDown: function() {
							
					},
					swipeUp: function() {
						
					}
				});
				/*
				$j('.viewer li').swipe({
					threshold: {
						x: 1,
						y: 10
					},
					swipeLeft: function() {
						o.next();	
					},
					swipeRight: function() {
						o.prev();	
					}
				});*/
				
			},
			
			/* ADDCONTROLS: any enhancements ? */
			
			addControls : function() {
				o.viewer.append("<span class='overlayLeft'></span><span class='overlayRight'></span>");
				o.scroller.css('width',o.scrollItems.length * parseInt(o.scrollItems.css('width')));
			},
			
			/* ADDSCROLL: adds scroll functionality */
			
			addScrollFunction : function() {
				e.addClass('scrollTime');
				o.slider.slider({
					startValue: 0,
					min: 0,
					max: o.itemsWidth,
					animate: true,
					slide: function(event, ui) {
						var value = ui.value;
						o.scroller.css('left', value * -1);	
						o.currentIndex = value / o.itemsWidth;
					},
					value: 0
				}).bind('slidestart', o.onSliderStart)
				.bind('slidestop', o.onSliderStop)
				.bind('slidechange', o.onSliderChange);
				
			}, 
			
			/* ONSLIDERCHANGE */
			
			onSliderChange: function(event, ui) {
				
				if(o.overrider) {					
					value = o.overrider;	
					scrollSnapTo = Math.round(parseInt(value * -1))*235;
					handleSnapTo = Math.abs(Math.round((( scrollSnapTo/o.itemsWidth )*100)*10000) / 10000);
					o.slider.children('a').stop(true).css('left', o.fromState).animate({left: handleSnapTo + "%"}, 500, 'easeInOutQuad', function() {
						o.fromState =  o.slider.children('a').css('left');	
						o.oldValue = o.fromState;						
					});		
				}
				
				o.overrider = false;
					
			},
						
			/* ONSLIDERSTART: fired when the slider starts */		
			
			onSliderStart: function(event, ui, valueOverride) {
				o.enableRolls = false;
				
				
				if(!valueOverride && valueOverride !== 0) { 
					o.fromState = o.slider.children('a').css('left');
					
				}
				else {
					o.overrider = valueOverride;
					o.fromState = o.slider.children('a').css('left');
				}
				
				selectedItem = o.scroller.children('.selected');
				selectedItem.find('strong').stop().animate({bottom: -34}, 300, 'easeInOutQuad').end().find('span,em').stop().animate({opacity: 0}, 300, 'easeInOutQuad').end().removeClass('selected');
				
			},
			
			/* ONSLIDERSTOP: fired when the slider stops */
			
			onSliderStop: function(event, ui, valueOverride) {
				o.enableRolls = true;
				if(!valueOverride && valueOverride !== 0) {
					value = ui.value;
					handleLeft = parseFloat(o.slider.children('a').css('left'));
					scrollSnapTo = Math.round(parseInt(value * -1)/235)*235;
					handleSnapTo = Math.abs(Math.round((( scrollSnapTo/o.itemsWidth )*100)*10000) / 10000);	
					o.scroller.stop().animate({ left: scrollSnapTo }, 150, 'easeInOutQuad');
					o.slider.children('a').stop().animate({left: handleSnapTo + "%"}, 100, 'easeInOutQuad', function() {
						
						o.fromState =  o.slider.children('a').css('left');	
						
						/****************************/
						//// UPDATE CURRENT INDEX.
						/****************************/
						
						o.currentIndex = (scrollSnapTo*-1) / 235;
						
						o.testDisabled();
						
					});
				}
				else {
					value = valueOverride;	
					scrollSnapTo = Math.round(parseInt(value * -1))*235;
					o.scroller.stop().animate({ left: scrollSnapTo }, 500, 'easeInOutQuad');
				}	
				
				
				//setTimeout(function() { console.debug("CURRENT INDEX: " + o.currentIndex); }, 1000);
				
			},
						
			/* PREV: snap to the previous item by moving the scrollbar */
			
			prev: function() {
				if(o.currentIndex > 0) {
					o.currentIndex--;
					if(o.currentIndex < 0) o.currentIndex =0;
					o.slider.trigger('slidestart', [this, o.currentIndex]);
					o.slider.slider('value', o.currentIndex);
					o.slider.trigger('slidestop', [this, o.currentIndex]);			
					if((o.currentIndex+1) < o.scrollItems.length - 3) {
						o.nextButton.css('display','block').stop().animate({opacity: 1}, 400, 'easeInOutQuad', function() {
							o.nextButton.removeClass('disabled');																		 
						});	
					}
					else {
						o.nextButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
							o.nextButton.addClass('disabled');																		 
						});
					}
					if(o.currentIndex > 0) {
						o.prevButton.css('display','block').stop().animate({opacity: 1}, 100, 'easeInOutQuad', function() {
							o.prevButton.removeClass('disabled');																		 
						});
					}
					else {
						o.prevButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
							o.prevButton.addClass('disabled');																		 
						});	
					}
				}	
				return false;
			},
			
			/* NEXT: snap to the next item by moving the scrollbar */
			
			next: function() {	
				if((o.currentIndex+1) < o.scrollItems.length - 3) {
					o.currentIndex ++;			
					o.slider.trigger('slidestart', [this, o.currentIndex]);
					o.slider.slider('value', o.currentIndex);
					o.slider.trigger('slidestop', [this, o.currentIndex]);			
					if((o.currentIndex+1) < o.scrollItems.length - 3) {
						o.nextButton.css('display','block').stop().animate({opacity: 1}, 100, 'easeInOutQuad', function() {
							o.nextButton.removeClass('disabled');																		 
						});	
					}
					else {
						o.nextButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
							o.nextButton.addClass('disabled');																		 
						});
					}
					if(o.currentIndex > 0) {
						o.prevButton.css('display','block').stop().animate({opacity: 1}, 400, 'easeInOutQuad', function() {
							o.prevButton.removeClass('disabled');																		 
						});
					}
					else {
						o.prevButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
							o.prevButton.addClass('disabled');																		 
						});	
					}
				}	
				return false;
			},
			
			/* ADDROLLOVERS: add hover animations to scroll items */
			
			addRollovers : function() {
				
				o.scrollItems.each(function() {
					var scrollItem = $j(this);
					scrollItem.find('strong').prepend('<em></em>');
					if(!scrollItem.hasClass('selected')) {
						scrollItem.find('span,em').css({'display' : 'block', 'opacity' : 0});
					}else {
						scrollItem.find('span,em').css({'display' : 'block', 'opacity' : 0.8});
					}
					scrollItem.hover(
						function () {
							if (!scrollItem.hasClass('selected')&&o.enableRolls==true) {
								selectedItem = scrollItem.parent().find('.selected');
								selectedItem.find('strong').stop().animate({bottom: -34}, 300, 'easeInOutQuad').end().find('span,em').stop().animate({opacity: 0}, 300, 'easeInOutQuad').end().removeClass('selected');
								scrollItem.find('span,em').stop().animate({opacity: 0.8}, 200, 'easeInOutQuad').end().find('strong').stop().animate({bottom: 0}, 300, 'easeInOutQuad').end().addClass('selected');
							}
						},
						function () {});
				});
				
			}, 
			
			testDisabled : function() {
			
				if((o.currentIndex+1) < o.scrollItems.length - 3) {
						o.nextButton.css('display','block').stop().animate({opacity: 1}, 400, 'easeInOutQuad', function() {
							o.nextButton.removeClass('disabled');																		 
						});	
					}
					else {
						o.nextButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
							o.nextButton.addClass('disabled');																		 
						});
					}
					if(o.currentIndex > 0) {
						o.prevButton.css('display','block').stop().animate({opacity: 1}, 400, 'easeInOutQuad', function() {
							o.prevButton.removeClass('disabled');																		 
						});
					}
					else {
						o.prevButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
							o.prevButton.addClass('disabled');																		 
						});	
					}

			}
			
		};
		
		o.init();
		return o;
	
	};
	
};


/* Product listing > hoizontal scroller ---------------------------------------------------------------*/
/* Dependencies - jquery-ui */

function productListSlider(){
	
	$j('#seriesProductScroller').each(function() { var o = new scrollMe($j(this)); });
	
	function scrollMe(e) {
		
		/* Set vars */
		
		var handleLeft = 0;
		var scrollSnapTo = 0;
		var handleSnapTo = 0;
		
		var o = {
			
			/* INIT: Run scroller functions & selector caching */
			
			init : function() {
				o.scroller = $j('ul', e);								// Obj - list of scrolling items
				o.slider = $j('#slider', e);							// Obj - slider
				o.scrollItems = $j('li', o.scroller);					// Objs - array of items to be scrolled
				o.viewer = $j('.viewer', e);							// Obj - scroller mask
				o.itemsWidth = (o.scrollItems.length * parseInt(o.scrollItems.css('width'))) - parseInt(o.viewer.css('width'));
				o.enableRolls = true;									// Boolean - are we scrolling?
				o.addControls();
				if(o.scrollItems.length > 4) o.addScrollFunction();										// Func - adds additional markup
				//o.addScrollFunction();									// Func - adds scroll functionality
				o.addRollovers();										// Func - add hover functionality to items
				
				
				// Prev/next buttons for touch devices.
				o.currentIndex = 0;
				o.oldValue = 0;
				o.fromState = null;
				o.overrider = false;
				o.prevButton = $j('#prevNextButtons .products_prev > a', e);
				o.nextButton = $j('#prevNextButtons .products_next > a', e);				
				o.prevButton.bind('click', o.prev);
				o.nextButton.bind('click', o.next);
				
				o.slider.addTouch();
				o.viewer.multiswipe({
					fingers: 2,
					threshold: 50,
					swipeLeft: function() {
						o.next();	
					},
					swipeRight: function() {
						o.prev();
					},
					swipeDown: function() {
							
					},
					swipeUp: function() {
						
					}
				});
				
				/*
				// Swipe left/right gestures for iPhone/iPad.
				$j('.viewer').swipe({
					swipeLeft: function() {
						o.next();	
					},
					swipeRight: function() {
						o.prev();	
					}
				});
				*/
				
			},
			
			/* ADDCONTROLS: any enhancements ? */
			
			addControls : function() {
				o.viewer.append("<span class='overlayLeft'></span><span class='overlayRight'></span>");
				o.scroller.css('width',o.scrollItems.length * parseInt(o.scrollItems.css('width')));
			},
			
			/* ADDSCROLL: adds scroll functionality */
			
			addScrollFunction : function() {
				o.slider.slider({
					min: 0,
					max: o.itemsWidth,
					animate: true
				}).bind('slide', function(event, ui) { o.scroller.css('left', ui.value * -1); })
				.bind('slidestart', o.onSliderStart)
				.bind('slidestop', o.onSliderStop)
				.bind('slidechange', o.onSliderChange);
				
				
			},		
			
			/* ONSLIDERCHANGE */
			
			onSliderChange: function(event, ui) {
				
				if(o.overrider) {
					
					value = o.overrider;	
					scrollSnapTo = Math.round(parseInt(value * -1))*235;
					handleSnapTo = Math.abs(Math.round((( scrollSnapTo/o.itemsWidth )*100)*10000) / 10000);
					o.slider.children('a').stop(true).css('left', o.fromState).animate({left: handleSnapTo + "%"}, 500, 'easeInOutQuad', function() {
						o.fromState =  o.slider.children('a').css('left');	
						o.oldValue = o.fromState;
					});		
				}
				
				o.overrider = false;
					
			},
			
			/* ONSLIDERSTART: fired when the slider starts */		
			
			onSliderStart: function(event, ui, valueOverride) {
				o.enableRolls = false;					
				
				if(!valueOverride && valueOverride != 0) { 
					o.fromState = o.slider.children('a').css('left');
				}
				else {
					o.overrider = valueOverride;
					o.fromState = o.slider.children('a').css('left');
				}

			},
			
			/* ONSLIDERSTOP: fired when the slider stops */
			
			onSliderStop: function(event, ui, valueOverride) {
				o.enableRolls = true;
				if(!valueOverride && valueOverride !== 0) {
					
					value = ui.value;
					handleLeft = parseFloat(o.slider.children('a').css('left'));
					scrollSnapTo = Math.round(parseInt(value * -1)/235)*235;
					handleSnapTo = Math.abs(Math.round((( scrollSnapTo/o.itemsWidth )*100)*10000) / 10000);
					o.scroller.stop().animate({ left: scrollSnapTo }, 150, 'easeInOutQuad');
					o.slider.children('a').stop().animate({left: handleSnapTo + "%"}, 100, 'easeInOutQuad', function() {
						
						o.fromState =  o.slider.children('a').css('left');	
						
						/******************/
						/// UPDATE CURRENT INDEX
						/*******************/
										
						o.currentIndex = (scrollSnapTo*-1) / 235;
						
						if((o.currentIndex+1) < o.scrollItems.length - 3) {
							o.nextButton.css('display','block').stop().animate({opacity: 1}, 400, 'easeInOutQuad', function() {
								o.nextButton.removeClass('disabled');																		 
							});	
						}
						else {
							o.nextButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
								o.nextButton.addClass('disabled');																		 
							});
						}
						if(o.currentIndex > 0) {
							o.prevButton.css('display','block').stop().animate({opacity: 1}, 400, 'easeInOutQuad', function() {
								o.prevButton.removeClass('disabled');																		 
							});
						}
						else {
							o.prevButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
								o.prevButton.addClass('disabled');																		 
							});	
						}
						
					});
				}
				else {
					value = valueOverride;	
					// console.info('onSliderStop', o.slider.children('a').css('left'));
					
					scrollSnapTo = Math.round(parseInt(value * -1))*235;
					o.scroller.stop().animate({ left: scrollSnapTo }, 500, 'easeInOutQuad');
				
					handleSnapTo = Math.abs(Math.round((( scrollSnapTo/o.itemsWidth )*100)*10000) / 10000);
					// o.slider.children('a').stop().animate({left: handleSnapTo + "%"}, 500, 'easeInOutQuad');
				}	
				
				
				// setTimeout(function() { console.debug("CURRENT INDEX: " + o.currentIndex); }, 1000);
				
			},
			
			/* PREV: snap to the previous item by moving the scrollbar */
			
			prev: function() {
				if(o.currentIndex > 0) {
					
					o.currentIndex --;
					o.slider.trigger('slidestart', [this, o.currentIndex]);
					o.slider.slider('value', o.currentIndex);
					o.slider.trigger('slidestop', [this, o.currentIndex]);			
					if((o.currentIndex+1) < o.scrollItems.length - 3) {
						o.nextButton.css('display','block').stop().animate({opacity: 1}, 400, 'easeInOutQuad', function() {
							o.nextButton.removeClass('disabled');																		 
						});	
					}
					else {
						o.nextButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
							o.nextButton.addClass('disabled');																		 
						});
					}
					if(o.currentIndex > 0) {
						o.prevButton.css('display','block').stop().animate({opacity: 1}, 400, 'easeInOutQuad', function() {
							o.prevButton.removeClass('disabled');																		 
						});
					}
					else {
						o.prevButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
							o.prevButton.addClass('disabled');																		 
						});	
					}
				}	
				return false;
			},
			
			/* NEXT: snap to the next item by moving the scrollbar */
			
			next: function() {	

				if((o.currentIndex+1) < o.scrollItems.length - 3) {
					o.currentIndex ++;			
				
					o.slider.trigger('slidestart', [this, o.currentIndex]);
					o.slider.slider('value', o.currentIndex);
					o.slider.trigger('slidestop', [this, o.currentIndex]);			
					if((o.currentIndex+1) < o.scrollItems.length - 3) {
						o.nextButton.css('display','block').stop().animate({opacity: 1}, 400, 'easeInOutQuad', function() {
							o.nextButton.removeClass('disabled');																		 
						});	
					}
					else {
						o.nextButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
							o.nextButton.addClass('disabled');																		 
						});
					}
					if(o.currentIndex > 0) {
						o.prevButton.css('display','block').stop().animate({opacity: 1}, 400, 'easeInOutQuad', function() {
							o.prevButton.removeClass('disabled');																		 
						});
					}
					else {
						o.prevButton.stop().animate({opacity: 0}, 100, 'easeInOutQuad', function() {
							o.prevButton.addClass('disabled');																		 
						});	
					}
				}	
				return false;
			},
			
			/* ADDROLLOVERS: add hover animations to scroll items */
			
			addRollovers : function() {
				
				o.scrollItems.each(function() {
					var scrollItem = $j(this);
					scrollItem.find('a').append('<span></span>').find('span').css('opacity',0).end().hover(
						
						function () {
							if(o.enableRolls==true) {
								scrollItem.find('img').stop().animate({ 
									width: '232px',
									height: 'auto',
									marginLeft: '-116px'
									}, 100).end().find('span').stop().animate({opacity: 1}, 100, 'easeInOutQuad');
							}
						},
						function () {
							if(o.enableRolls==true) {
								scrollItem.find('img').stop().animate({ 
									width: '220px',
									height: 'auto',
									marginLeft: '-110px'
									}, 100).end().find('span').stop().animate({opacity: 0}, 300, 'easeInOutQuad');
							}
						});
				});
				
			}
			
		};
		
		o.init();
		return o;
		
	};
		
};


/* Homepage > scrolling content ------------------------------------------------------------------------*/
/* Dependencies - jquery tools */

function scrollerSetUp() {
	
	$j('#productScroller').each(function() { var o = new scrollMe($j(this)); });	
	
	function scrollMe(e) {
		
		var o = {
			
			/* INIT: Run scroller functions & selector caching */
			
			init : function() {
				o.scroller = e;											// Obj - wrapper (#productScroller)
				o.scroller.children('.viewer').after("<a href='javascript:void(0);' class='scrollLeft'>Left</a><a href='javascript:void(0);' class='scrollRight'>Right</a>");
				o.leftButton = $j('.scrollLeft', o.scroller);			// Obj - left button
				o.rightButton = $j('.scrollRight', o.scroller);			// Obj - left button
				o.addScroll();											// Func - set up scroller functionality
				o.addItemRollovers();									// Func - add rollovers to scrolling items
				o.addRollovers();										// Func - add rollovers to left/right buttons
			},
			
			/* ADDSCROLL: setup scroller */
			
			addScroll : function() {
				o.scroller.scrollable({ 
					clickable : false,
					items : 'ul.contentConveyor',
					keyboard : false,
					next : '.scrollRight',
					prev : '.scrollLeft',
					loop : false,
					speed : 600,
					easing : 'quad',
					size : 5
				}).circular();
			}, 
			
			/* ADDITEMROLLOVERS: add hover animations to scroll items */
			
			addItemRollovers :function() {
				$j('li', o.scroller).each(function () {
					var scrollItem = $j(this);
					scrollItem.hover(
						function() {
							scrollItem.children('a').stop().animate({top: 0}, 300, 'easeInOutQuad');
							scrollItem.children('small').stop().animate({opacity: 1}, 200, 'easeInOutQuad');
						},
						function() {
							scrollItem.children('a').stop().animate({top: 40}, 250, 'easeInOutQuad');
							scrollItem.children('small').stop().animate({opacity: 0}, 150, 'easeInOutQuad');
						}
					).append("<small></small>").find("small").css('opacity', 0);
				});
			},
			
			/* ADDROLLOVERS: add hover animations to left/right buttons */
			
			addRollovers :function() {
				o.rightButton.hover(
					function(){o.rightButton.stop().animate({right: -10}, 100, 'easeInOutQuad');},
					function(){o.rightButton.stop().animate({right: -6}, 100, 'easeInOutQuad');}
				);
				o.leftButton.hover(
					function(){o.leftButton.stop().animate({left: -10}, 100, 'easeInOutQuad');},
					function(){o.leftButton.stop().animate({left: -6}, 100, 'easeInOutQuad');}
				);
			}
			
		};
		
		o.init();
		return o;
		
	};
	
};


/* Product > overlays --------------------------------------------------------------------------------*/
/* Dependencies - none */

function showOverlay() {
	
	$j('#bandw').after("<div id='overlay'><span id='loader'><img src='/Styles/GF/Images/ajax-loader.gif'/></span></div><div class='overlayBoxWrapper'></div>");
	$j('.overlayLink').each(function() { var o = new showMyOverlay($j(this)); });
	
	function showMyOverlay(e) {
	
		var o = {
			init : function() {
				o.button = e;													// Obj - trigger
				o.overlay = $j("#overlay").css({"opacity":0.8});				// Obj - overlay shield
				o.loader = o.overlay.children('#loader').css({"opacity":1});	// Obj - ajax loader
				o.box = $j('.overlayBoxWrapper');								// Obj - box holder
				o.closeButton = "<a href='javascript:void(0);' class='close'>Close</a>";			// String - close button
				o.location = o.button.attr('href');								// String - content location
				o.monitor();													// Func - to show/hide overlays
			},
			
			/* MONITOR: show/hide overlays */
			
			monitor : function(){
				o.button.click(function(e) {
					e.preventDefault();
					o.overlay.fadeIn(100);
					o.loader.fadeIn(100);
					// load overlay content
					o.box.load(o.location + ' .overlayBoxInner', function() { o.update(); });
				});
				o.overlay.click(function(){o.distroy()});
			}, 
			
			/* UPDATE: once external HTML loaded */
			
			update : function(){
				o.loader.fadeOut(100);
				// vertically center box
				o.box.css('marginTop', - o.box.height() * 0.5 ).show();
				// add functionality to close button
				o.box.children('.overlayBoxInner').prepend(o.closeButton).find('.close').click(function(e) {
					e.preventDefault();
					o.distroy();
				});
				// cufon for dynamic content
				Cufon.refresh();
				// AF? - .load() does not inject scripts...
				customScrollBar();
				starRating();
				validateForm();
				videoPopUpPlayer();
			},
			
			/* DESTROY: remove the overlays / empty the HTML */
			
			distroy : function () {
				o.overlay.fadeOut(200);
				o.box.hide().empty();
			}
		};
		
		o.init();
		return o;
		
	};
};


/* Product > Star rating rollovers ---------------------------------------------------------------------*/
/* Dependencies - none */

function starRating() {
	
	$j('.rateStarForm .rateStar').each(function() { var o = new showRating($j(this)); });
	function showRating(e) {
		var o = {
			init : function() {
				o.star = e;
				o.rate = $j('#rate');
				o.prev = o.star.prevAll('.rateStar').andSelf();
				o.next = o.star.nextAll('.rateStar');
				o.monitor();
			}, 
			monitor : function() {
				o.star.hover(
					function(){
						o.prev.addClass('sHover');
					},
					function(){
						o.prev.removeClass('sHover');
					}
				);	
				o.star.click(function (evt) { 
					o.prev.addClass('sel');
					o.next.removeClass('sel');
					o.rate.attr('value',o.star.val());
				});
			}
		};
		o.init();
		return o;
	};
	
};

/* Product > custom scroll bar -----------------------------------------------------------------------*/
/* Dependencies - jquery scroll */

function customScrollBar() {
    
        $j('.scrolBox').each(function() {
            var o = new scrollMe($j(this));
               try{ $j('.jScrollPaneDrag').addTouch();}catch(Error){ } 
                $j('.jScrollArrowUp, .jScrollArrowDown').live('click', function(event) { event.preventDefault(); });
        });
        function scrollMe(e) {
            e.jScrollPane({ scrollbarWidth: 5, showArrows: true });
        };
  	
};

/* Product > gallery ---------------------------------------------------------------------------------*/
/* Dependencies - flowplayer */

function galleryImages() {

	$j('#galleryFeature, #seriesBannerFeature').each(function() { var o = new setupGallery($j(this)); });
	function setupGallery(e) {
		var o = {
			
			init : function() {
				o.feature = e;											//Obj - gallery feature
				o.media = $j('#galleryMedia', o.feature);				//Obj - img/video holder
				o.video = $j('#player', o.feature);				//Obj - video holder galleryVideo on series template
				o.imgItems = $j('.imgThumbs li', o.feature);			//Objs - image thumbs
				o.videoItems = $j('.videoThumbs li', o.feature);		//Objs - video thumbs
				o.href = '';											//String - media location
				o.selected = $j('.selected', o.feature);				//Obj - selected thumb
				o.selected.children('a').children('span').css('opacity', 0.1);
				//o.media.children('div.img').css('opacity', 1);
				if(o.imgItems) o.showImages();
				  
				if(o.videoItems) {
				o.showVideos();
					o.media.children('div.img').css('opacity', 1);
					
					$f('player').play(o.selected.children('a').attr('href'));
					o.isPlaying = true;
					
				}else {
					o.video.css('opacity', 0);
				o.isPlaying = false;
				};
				if($j("#seriesBannerFeature").html()) o.hideShowControls();
			},
			
			showImages : function() {
				// Pre load images
				$j.preloadImages(o.imgItems.children('a').attr('href'));
				// Show images
				o.imgItems.children('a').each(function() { var o = new monitorImages($j(this)); });
				function monitorImages(i){
					i.after('<span></span>').append('<span></span>');
					i.children('span').css({'opacity': 0, 'display' : 'block'});
					o.selected.children('a').children('span').css('opacity', 0.1);
					i.click(function (evt) {
						evt.preventDefault();
						if(i.parent('li').hasClass('selected')) return false;
						o.href = i.attr("href");
						o.selected.removeClass("selected").children('a').children('span').css('opacity', 0);
						if(o.isPlaying || ! o.video.is(":visible")) {
							$f('player').pause();
							o.video.stop().animate({ 'opacity': 0}, 200);
							o.isPlaying = false;
			            }
						o.media.children('div.img').fadeOut('fast', function() {
							o.media.empty().append("\<div class='img'><img src='"+o.href+"'\/><\/div>").children('img').css({opacity: 0}).stop().animate({opacity: 0},800).animate({opacity: 1},300);
						});
						i.parent().addClass("selected");
						o.selected = i.parent();
					});
					i.hover(
						function () {
							i.children('span').stop().animate({opacity: 0.1},200);
						},
						function () {
							if(!i.parent().hasClass('selected')) i.children('span').stop().animate({opacity: 0},200);
					});
				};
			},
			
			showVideos : function() {
				o.videoItems.children('a').each(function() { var o = new monitorVideos($j(this)); });
				function monitorVideos(j){
					j.after('<span></span>').append('<span></span>');
					j.click(function (evt) {
						evt.preventDefault();
						//if(j.parent('li').hasClass('selected')) return false;
						o.isPlaying = true;
						o.href = j.attr('href');
						o.media.children('div.img').fadeOut('fast', function() {
							$f('player').play(o.href);	
							o.video.stop().animate({ 'opacity': 1}, 200, function(){
												
							});
						});
						o.selected.removeClass("selected").children('a').children('span').css('opacity', 0);	
						j.parent().addClass("selected");
						o.selected = j.parent();
					});
					j.hover(
						function () {
							j.children('span').css('opacity', 1);
						},
						function () {
					});
				};
			},
			
			hideShowControls : function() {
				
				o.feature.children('.seriesOverlay').css('bottom', '-61px').end().hover(
					function () {
						o.feature.children('.seriesOverlay').stop().animate({bottom: 0},200);
					},
					function () {
						o.feature.children('.seriesOverlay').stop().animate({bottom: -61},400, function(){});
						o.controls.stop().animate({ 'opacity': 0}, 200);
				});
			}
			
		};
		o.init();
	};
	
};


/* Global > form validation --------------------------------------------------------------------------*/
/* Dependencies - jquery validation */

function validateForm() {

	$j('.overlayBoxInner form').each(function() { var o = new valadateThis($j(this)); });
	
	function valadateThis(e) {
		
		jQuery.validator.messages.required = "";
		var pError = $j("p.error");
		
		$j(e).validate({
			invalidHandler: function(e, validator) {
				var errors = validator.numberOfInvalids();
				if (errors) {
					var message = errors == 1
						? 'You missed 1 field. It has been highlighted above'
						: 'You missed ' + errors + ' required fields. They have been highlighted above';
					pError.html(message);
					pError.show();
				} else {
					pError.hide();
				};
			},
			onkeyup: false
		});
	
	};

};


/* Global > tab content areas ------------------------------------------------------------------------*/
/* Dependencies - none */

function tabContent(){
	
	$j('#tabTriggers').each(function() { var o = new tabThis($j(this)); });
	function tabThis(e) {
		var o = {
			init : function() {
				o.triggerList = e;
				o.selected = o.triggerList.children('.selected');
				o.selectedTab = $j(o.selected.children('a').attr('href'));
				$j('.tabContent:not(:first-child)').hide();
				o.monitor();
			}, 
			monitor : function() {
				
				o.triggerList.children('.tabTrigger').children('a').each(function() { var o = new monitorClicks($j(this)); });
				function monitorClicks(i){
					i.click(function (evt) {
						evt.preventDefault();
						o.selected.removeClass("selected");	
						o.selectedTab.hide();
						o.selectedTab = $j(i.attr('href'));
						o.selectedTab.show();
						i.parent().addClass("selected");
						o.selected = i.parent();
						Cufon.refresh();
					});
				};
				
			}
		};
		o.init();
	};
	
};


/* Global > video pop up player ----------------------------------------------------------------------*/
/* Dependencies - flowplayer */

function videoPopUpPlayer(){
	
	$j('#videoOverlay').each(function() { var o = new videoSetup($j(this)); });
	function videoSetup(e){
		var o = {
			init : function() {
				o.wrapper = e;
				o.player = $j('#videoPlayer',o.wrapper);
				o.controls = $j('#videoControls',o.wrapper);
				o.thumbs = $j('.videoThumbs li',o.wrapper);
				o.selected = o.thumbs.filter('.selected');
				o.play();
				o.monitor();
			}, 
			play : function () {
				
				//play first video
					$f("player", { src: "/resources/flowplayer/flowplayer.tradeit-3.1.1.swf", wmode: 'transparent'}, { 
					
					"clip": { 
						"scaling": "fit"
					},	 
				
					/*"play": { 
						"url": "_Client/Styles/Images/videoPlayButton.png", width: 84, height: 84,opacity: 1, label: null, replayLabel: null, fadeSpeed: 500, rotateSpeed: 50 
					},*/
				
					"screen":{
						"height":"100pct",
						"top":0
			},
				
					"canvas":{  
						"backgroundGradient": "none" 
					},
					
					"plugins":{
						"controls":{ "borderRadius":13,"timeColor":"#ffffff","slowForward":true,"bufferGradient": [0.5,0.2,0],"backgroundColor":"rgba(0, 0, 0, 1)","volumeSliderGradient":"none","slowBackward":false,
							"timeBorderRadius":20,"time":true,"progressGradient":"none","height":30,"volumeColor":"#ffffff","tooltips":{
								"marginBottom":5,"volume":false,"scrubber":true,"buttons":false},"opacity":1,"fastBackward":false,"timeFontSize":12,"volumeSliderColor":"#ffffff","border":"0px","bufferColor":"rgba(0, 0, 0, 1)","buttonColor":"rgba(255, 255, 255, 1)","mute":false,"autoHide":{"enabled":true,"hideDelay":1000,"hideStyle":"fade","mouseOutDelay":2500,"hideDuration":400,"fullscreenOnly":false},"backgroundGradient":[0.5,0.2,0],"width":"40pct","display":"block","sliderBorder":"1px solid rgba(128, 128, 128, 0.7)","buttonOverColor":"#ffffff","fullscreen":true,"timeBgColor":"rgba(0, 0, 0, 0)","borderWidth":0,"scrubberBarHeightRatio":0.1,"bottom":"13pct","stop":false,"sliderColor":"#000000","zIndex":1,"scrubberHeightRatio":0.5,"tooltipTextColor":"#ffffff","spacing":{"time":6,"volume":8,"all":2},"sliderGradient":"none","timeBgHeightRatio":0.7, "volumeSliderHeightRatio":0.4,"name":"controls","timeSeparator":" ","volumeBarHeightRatio":0.1,"left":"50pct","tooltipColor":"#000000","playlist":false,"durationColor":"#a3a3a3","play":true,"fastForward":true,"progressColor":"#ffffff","timeBorder":"0px solid rgba(0, 0, 0, 0.3)","volume":false,"scrubber":true,"builtIn":false,"volumeBorder":"1px solid rgba(128, 128, 128, 0.7)","margins":[2,6,2,12]
								
						}
					}
				});
				
			},
			monitor : function() {
				
				//Add thumbnail function
				o.thumbs.children('a').each(function() { var o = new playMe($j(this));});
				
				function playMe(e) {
					var link = $j(e);
					link.after('<span></span>').append('<span></span>');
					link.children('span').css({'opacity': 0, 'display' : 'block'});
					o.selected.children('a').children('span').css('opacity', 0.1);
					link.click(function (evt) { 
						evt.preventDefault();
						o.selected.removeClass("selected").children('a').children('span').css('opacity', 0);
						$f("player").play(link.attr('href'));
						link.parent().addClass("selected");
						o.selected = link.parent();
					});
					link.hover(
						function () {
							link.children('span').stop().animate({opacity: 0.1},200);
						},
						function () {
							if(!link.parent().hasClass('selected')) link.children('span').stop().animate({opacity: 0},200);
					});
				};
				
			}
		};
		o.init();
	};
	
};


/* Global > preload images ---------------------------------------------------------------------------*/
/* Dependencies - none */

jQuery.preloadImages = function() {
	var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
	for(var i = a.length -1; i >= 0; i--) {
		jQuery("<img>").attr("src", a[i]);
	}
}


/* Global > custom easing ----------------------------------------------------------------------------*/
/* Dependencies - none */

$j.easing.quad = function (x, t, b, c, d) {
	if ((t/=d/2) < 1) return c/2*t*t + b;
	return -c/2 * ((--t)*(t-2) - 1) + b;
};

$j.easing.easeInOutQuad = function (x, t, b, c, d) {
	if ((t/=d/2) < 1) return c/2*t*t + b;
	return -c/2 * ((--t)*(t-2) - 1) + b;
};



function test()

{
 ($j("#player_api").length > 0)
 
 {
   $j("#player_api").width(470);
   $j("#player_api").height(8000);
   
  alert ("yes");
 
  }
}
