

$(document).ready(function(){	
	
	if($('video').length > 0){
		$('video').each(function(i,el) {	
			$('.tabBox').append('<div class="clearance">&nbsp;</div>');
			var player = new bVideoPlayer(el);			
		});
	}
	
	
	/* set the tab heights of many things */
	var thingsToMakeTheSameHeight = ['.productList > li', '.softwareCategory li', '.relatedList li', '#dynamicTabBox .tabBox'];	
	
	jQuery.each(thingsToMakeTheSameHeight, function(index, val) {
		setUniformHeights(val);
	});
	
		
	/* Implement smooth scrolling on page anchors */
	$('a[href*=#]').click(function() { 
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        && location.hostname == this.hostname) { 
            var $target = $(this.hash); 
			
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']'); 
            if ($target.length) {
                var targetOffset = $target.offset().top - 40; // allowing for 20px of margin above the element
				// Only scroll if you have to	
				// if the target is >100px past the top of the window or >100px above the top of the window
				if(targetOffset - $(window).scrollTop() > $(window).height()/3 || $(window).scrollTop() - targetOffset  > $(window).height()/3 || $(this).parent().hasClass('linkPagetop')){
					$('html,body').animate({scrollTop: targetOffset, "easing": "easeout"}, 1000); 
				}
				return false;            
            } 
        } 
    });
	
	
	/*
	// Hacks for cross browser consistency
	*/
	
	$('.tableCmn01.alternate tr td img').parent('td').width('120');
	$('.tableCmn01.alternate tr td').not(':first-child').find('h6').width('auto');
	
	if($('#country-selector-button').length > 0){
		$('#country-selector-button').css('visibility', 'visible');
	}
	
	if($('.linkPrint').length > 0){
		$('.linkPrint').each(function(){
			$(this).css('display', 'inline-block');
		});
	}
	
	if($('.containsRadioBtns').length > 0){
		$('.containsRadioBtns span input:nth-child(4)').after('<br />');
	}
	
	if($('.quotebtn .linkExt').length > 0){
		$('.quotebtn .linkExt').removeClass('linkExt');
	}
	
	if($('.sideSub ul').length == 1){
		$('.sideSub ul').removeClass('lastOne');
	}
	
	if($('.checkArea').length > 0){
		$('.checkArea').closest('table').addClass('noborder');		
	}
	
	
	$('.dynamicButton').hover(
	  function () {
		$(this).addClass("hover");
	  },
	  function () {	
		$(this).removeClass("hover");
	  }
	);
	
	if($('#goalArea .wrap .contact02').length > 0){
		$('#goalArea .wrap .contact02').css('width', '100%').css('background', 'none');		
	}
	
	if($('.section .subSectionGrid > li').length > 0){
		var thisHeight = 0;
		var nextHeight = 0;
		$('.section .subSectionGrid > li:nth-child(odd)').each(function(){
			if($.browser.msie && $.browser.version == 8){		
				$(this).addClass('clear');
			}
			else {
				thisHeight = $(this).outerHeight();
				nextHeight = $(this).next().addClass('noRightMargin').outerHeight();	
				
				if(nextHeight  > thisHeight){
					$(this).height(nextHeight);
				}	
				else if(nextHeight != null){
					$(this).next('li').height(thisHeight);	
				}	
				else {
					$(this).css('margin-bottom', '0');
					
				}
			}
			
				
		});
		
	}
	
	
	if($('.tableCmn01.alternate').length > 0){	
		cols = $('.tableCmn01.alternate tr th').length;		
		if(cols){
			var borderRow = '<tr class="rule"><td colspan="' + cols + '"><hr noshade="noshade" size="1" /></td></tr>';
			$('.tableCmn01.alternate tbody tr:last-child').after(borderRow);
		}
	}
	
	/* deal with the imported product spec table
	if($('.productSpec > table').length > 0){
		tidyUpProductSpecTable();		
	}
	*/
	
	/* deal with news title replacement	*/
	if($("#breadCrumb li[rel=" + dictionary.news_section_id + "]").length > 0){
		$('#breadCrumb li:last-child strong, h1').text(dictionary.news_article_title);
	}
	
		
	// deal with change link hiding
	var urlsToHideTheChangeLinkFor =[
		'http://www.ricoh.local/index_home.html', 
		'http://eu-tridion-dev-web.eu.risenet.eu/index.aspx', 
		'http://eu-tridion-test-web.eu.risenet.eu/index.aspx', 
		'http://eu-tridion-iacc-web.eu.risenet.eu/index.aspx', 
		'http://www.ricoh-europe.com/index.aspx',
		'http://www.ricoh-europe.com/index.aspx',
		'http://www.ricoh.local/', 
		'http://eu-tridion-dev-web.eu.risenet.eu/', 
		'http://eu-tridion-test-web.eu.risenet.eu/', 
		'http://eu-tridion-iacc-web.eu.risenet.eu/', 
		'http://www.ricoh-europe.com/'
		];
		
	jQuery.each(urlsToHideTheChangeLinkFor, function(index, val) {
		if(window.location['href'] == val){
			$('#localeIdent a').remove();
		}
	});
	
	// bind print handling	
	$('span.linkPrint a').click(function(e){
		e.preventDefault();
		ricohPrint();
	});

	
	// Dynamic tab box behavior handler
	if($('#dynamicTabBox').length > 0){
		
		
		
		var dynamicTabBoxWidth = $('#dynamicTabBox').width();
		var tabWidths = 0;
		var tabCount = 0;		
		$('#dynamicTabBox .tabBoxNav li').each(function(){
			if(!$(this).hasClass('empty')){
				tabCount++;
				tabWidths += $(this).outerWidth(true);				
			}
		});		
		if(tabWidths > (dynamicTabBoxWidth - 30)){
			
			$('#dynamicTabBox .tabBoxNav li.empty').remove();			
			newWidth = Math.floor((dynamicTabBoxWidth/tabCount)) - 12;	
			
			$('#dynamicTabBox li a').each(function(){
				$(this).width(newWidth);
			});
			
			if(newWidth * tabCount < tabWidths){
				adjustedWidth = dynamicTabBoxWidth - ((newWidth + 16) * tabCount) - (tabCount + 1);			
				$('.tabBoxNav li:last-child a').width(newWidth + adjustedWidth);
			}
			
			$('#dynamicTabBox .tabBoxNav li:last-child').addClass('borderRight');
			setUniformHeights('#dynamicTabBox .tabBoxNav li a');
			$('#dynamicTabBox .tabBoxNav li ').height($('#dynamicTabBox .tabBoxNav li').height() );
		}
		else {	
			if($.browser.msie && $.browser.version == 7){
				setUniformHeights('#dynamicTabBox .tabBoxNav li a');
			}
			setUniformHeights('#dynamicTabBox .tabBoxNav li');
			$('#dynamicTabBox .tabBoxNav li ').height($('#dynamicTabBox .tabBoxNav li').height()-1);			
			// set the empty list item to the remaining width of 
			//  tab box and take away another 1px for the left border			
			$('li.empty').width(dynamicTabBoxWidth - tabWidths - 1);	
		}
		
	}	
	
	
	/* Populate the sideNav with the tertiary navigation */
	if($('.snavListTxt').length > 0){		
		$('.sideNav .hasChildren li.hasChildren').append($('.snavListTxt').clone().removeClass('snavListTxt'));		
	}
	
	if($('ul.sideNav > li.hasChildren').length > 0){
		$('ul.sideNav > li:last-child').addClass('lastOne');
	}
	
	
	
	
	/* check for splash areas or brand panels to animate */
	if($('#mainSplashArea').length > 0){
		objectToCycle = $('#mainSplashArea');
	}
	else if($('#brandPanel').length > 0){
		objectToCycle = $('#brandPanel');
	}
	else {
		objectToCycle = false;
	}
	
	if(objectToCycle){
		setUpSlides(objectToCycle);
		if($('#gNav.belgium').length > 0){
			$('#mainSplashArea div.slide p.strapline').css('position', 'absolute').css('top', '140px');
		}
		$(objectToCycle).find('.slide').click(function(event){	
			event.preventDefault();
			slideAnchor = $(this).find('a');			
			if(slideAnchor.length > 0){		
				if($(slideAnchor).attr('target') && $(slideAnchor).attr('target') != '_self'){
					window.open($(slideAnchor).attr('href'));
				}
				else {
					window.location = $(slideAnchor).attr('href');
				}
				
			}
		});
		
		try{
			slideDelay = brand_panel_slide_transition_delay;
		}
		catch(err) {
			log('brand_panel_slide_transition_delay not set');
			slideDelay = 4000;
		}
		
		// check me!

		if($(objectToCycle).find('.slide').length > 1){
			$(objectToCycle).cycle({
				fx:     'fade',
				speed:  'slow',
				timeout: slideDelay,
				pager:  '#splashNav',
				slideExpr: 'div.slide'
			});
		}
		else {
			$('.splashNav').remove();
			$('.slide').show();
		}
	}
	
	
	
	
	/* tabBoxArea */
	if($('#indexTabMenu').length == 1){
		$('.tabBoxNav li:first a, #dynamicTabBox li:first a').addClass('on');
	}
	else {
		$('.tabBoxNav li:first a, #dynamicTabBox li:first a').addClass('on').parent('li').css('background-color', '#fff');
	}
	$('.tabBox').hide();
	$('.tabBox01').show();
	if($('.tabBoxNav li').length == 1){
		$('.tabBoxNav li:first').addClass('tabGapBg');
		$('#indexTabMenu .tabBoxNav').addClass('tabGapBg');
		$('#indexTabMenu .tabBox').addClass('tabGapBg');
	}
	else if($('#indexTabMenu .tabBoxNav li').length == 2) {
		$('#indexTabMenu .tabBoxNav li.middleTab').removeClass('middleTab').addClass('lastTab');		
		$('#indexTabMenu .tabBoxNav').addClass('tabGapBg');
		$('#indexTabMenu .tabBox').addClass('tabGapBg');
	}
	
	if($('.tabBoxArea').length > 0){
		setUpTabBoxAreas();	
	}
	
	
	
	// product comparator handlers
	if($('#comparator').length > 0){
		
		var savedListValues = false;
		
		if(savedComparatorProducts = getCookie('comparator_products')){
			
			var savedProducts = new Array();
			
			savedProducts = savedComparatorProducts.split('|~|');			
		
			for(x=0, max = savedProducts.length; x < max; x++){
				var product = new Array();				
				product = savedProducts[x].split(",");
				addComparatorProduct(product[0],product[2],product[1]);				
			}
			bindComparatorButtons();
				
			
		}
		
		
		cleanUpComparatorPage();
		
			
		
		$('.compareButton').css('visibility', 'visible').show().unbind('click').bind('click', function (event){
		
			event.preventDefault();
			if($(this).parents('tbody').attr('id')){				
				var productId = $(this).parents('tbody').attr('id');
				var compareListId = productId + 'comp';
				var title = $(this).parents('tbody').find('h6').html();	
			}
			else {					
				var productId = $(this).parents('.section').attr('id');
				var compareListId = productId + 'comp';
				var title = '<a href="' + window.location['pathname'] + '">' + $('h1').text() + '</a>';		
			}			
			// if the item is already in the compare list, remove it	
			if (productId && $('#' + compareListId).length >= 1){	
				removeComparatorProduct(compareListId);	
				updateCountContainer();				
			}
			else if($('#' + compareListId).length == 0){
				if($('#comparator > .productList li.placeholder').length > 0){					
									
					addComparatorProduct(productId, title, $('#' + productId + ' img.thumbnail').attr("src"));
					
					// add the binding for removing items from the list
					// the reason for the late binding is that there are 
					// no remove buttons when the document originally loads.
					bindComparatorButtons();
				
				};	
				
			}
			
			cleanUpComparatorPage();	
			setComparatorCookie();	
			
		});
		if(!($.browser.version == 6 && $.browser.msie)){
			var padding = 100;
			if($.browser.msie){
				padding = padding * 2;
			}
			$('#gFooterArea').css('padding-bottom', padding);
		}
		
		$('#toggler').click(function(){
			toggleComparatorContainer();			
		});		
		$('#emptier').click(function(){
			removeAllComparatorProducts();
			setComparatorCookie();	
		});		
		updateCountContainer();		
	}
	
	
		
	/*
	jquery.fixSelect

	Fixes an whereby IE chops long options in a select box with fixed width. It does this by
	surrounding the element within a span of the same width with overflow-x set to hidden,
	and setting the select width to auto on a mousedown or keyup event then back to its
	previous value on blur.

	Syntax:

		$jQueryCollection.fixSelect([minWidth]);

	The minWidth parameter is optional, but can be used to hard set a width for the elements.
	If the result doesn't look quite right, you can use CSS to fix the result: the added span
	wrapping the select element has the class "selectFix" to make this possible.

	Two custom events are exposed:

		- "initfix" will recompute the natural width of the element; and
		- "applyfix" will resize the select element (e.g. after assigning focus programmatically).

	(Merciful heavens, IE should just be taken outside and *shot*. This apparently simple code
	involved more obscure bug workarounds than I have time or space to document.)
	*/
	if($.browser.msie){
		
		$.fn.fixSelect = function(minWidth) {
			/* Fix only applies to IE 8 and below. */
			return (!($.browser.msie && $.browser.version < 9)) ? this : this.each(function() {
				if (this.tagName.toLowerCase() == 'select') { // Also only applies to select elements.

					var el = this,
					$this = $(this),
					minWidth = minWidth ? minWidth : el.offsetWidth, // Current width used as minimum.
					elementWidth = $this.outerWidth(),
					$wrapper = $('<span class="selectFix"></span>').css({ // Wraps the select element.
						display: ($this.css('display') == 'block') ? 'block' : 'inline-block',
						cssFloat: $this.css('float'),
						overflowX: 'hidden',
						overflowY: 'visible',
						width: minWidth
					}),
					naturalWidth;

					/* Set up element margins on wrapper instead. (For more complex styles, use CSS.) */
					$.each('marginTop marginRight marginBottom marginLeft'.split(' '), function(i, prop) {
						$wrapper.css(prop, $this.css(prop));
						$this.css(prop, 0);
					});

					if ($this.is(':visible')) { // Doesn't work for invisible elements, they have zero width!
						/* Determine what the "natural" (i.e. automatic) width would be. */
						$this.width('auto');
						naturalWidth = $this.outerWidth();
						$this.width(minWidth);

						$this
						.wrap($wrapper)
						.bind('mousedown keyup applyfix', function() {
							/* Use "auto" or fixed width, whichever is biggest. */
							$this.width((naturalWidth < elementWidth) ? minWidth : 'auto');
							/* Horribly, IE 6 will ignore the overflow anyway unless some part of the
							select element is already hidden before the options list is displayed. */
							if ($.browser.version == 6) $this.css('marginLeft', 1);
						})
						.blur(function() {
							/* Reset the element to fixed width. */
							$this.width(minWidth);
							if ($.browser.version == 6) $this.css('marginLeft', 0);
						})
						.bind('initfix', function() {
							/* Recalculate "natural" width. */
							$this.width('auto');
							naturalWidth = $this.outerWidth();
							$this.width(minWidth);
						});
					}
				}
			});
		}
	
				
		if($.browser.version <= 8){		
		//	$('.productList li:even, #gHdrLangNav li:last-child a, .subSectionGrid li:even').addClass("noRightMargin");			
			$('select').fixSelect([$("select").css("width")]);		
		}
		
		if($.browser.version <= 8 && $('.indexList').length > 0){
			$('ul.indexList li:nth-child(odd)').css('clear', 'left');
		}	
		if($.browser.version == 7 && $('#mainContentWrap').length > 0){				
			$('#mainContentWrap').after('<div class="clearance">&nbsp;</div>');						
		}
					
	}
	
	$('#gHdrSearchSubmitBtn, .searchSubmitBtn').click(function(e){
		e.preventDefault();
		var searchField = $(this).closest('form').find('input[type=text]');		
		if($(searchField).val().length == 0){
			$(searchField).focus();
		}
		else {
			$(this).unbind('click').trigger('click');
		}
	});
	
	if(!($.browser.msie && $.browser.version <= 6)){
		if($('#socialPointContainer').length > 0){		
			var popUpDelay = 5000;
			if($('#socialPointContainer').hasClass('shortDelay')) {
				popUpDelay = 3500;
			}
			else if($('#socialPointContainer').hasClass('longDelay')) {
				popUpDelay = 6500;
			}		
			t=setTimeout('initSocialPointPopUp()',popUpDelay);			
			
			$(window).resize(function(){				
				var leftPosition = Math.ceil(($(window).width() - $('#container').width()) / 2);	
				leftPosition = (leftPosition < 0 ? 0:leftPosition) + 'px';				
				var css = {
					'left': leftPosition			
				}
				$('#socialPointContainer').css(css);
			});				
		}
	}
});

$(window).load(function() {
	/* check for secondary promo and tabox on homepage to set heights */
	if($('#secondaryPromo').length > 0 && $('.tabBox01').length > 0){		
		setHomepageContentHeights();
	}	
	
	if($.browser.msie && $.browser.version <= 8 && $('#mainContentWrap').length > 0){			
	
		var videoHeight = 0;
	
		if($('video').length > 0){
			//alert('here');
			setUniformHeights('#dynamicTabBox .tabBox')
			videoHeight = parseInt($('video').attr('height')) + 30;				
		}
		
		if($('#mainContentWrap').outerHeight(true) < $('#sideContentLeft').outerHeight(true)){
			var newHeight = $('#sideContentLeft').outerHeight(true) + 80 + videoHeight;
			$('#mainContentWrap').height(newHeight);
		}
		else if($('video').length > 0) {	
			var newHeight = $('#sideContentLeft').outerHeight(true) + videoHeight;
			$('#mainContentWrap').height($('#mainContentWrap').outerHeight(true) + videoHeight);
		}
	}
	
	
	
});
