/****************************************
*
Bosch Main JS: Divisions OCS/CMS/ASA
Kittelberger media | 2010-12 dsp
*

e1: 	set/reset default input text from textfields with class .cdVal
e2: 	mouseover for #leftNav
e3: 	iterate each #leftNav li.active to set active state
e4: 	fadeToggle stageContainer
e5: 	tab navigation (toggleTabContainer)
e6: 	accessory toggle up/down
e7: 	lightbox functions
e8: 	submit button: hover
e9: 	custom Bosch lightbox/popup with URL param processing and header include
e10: 	location change 
e11: 	flyout navigation (2-rows)
e17:  initBlb()
e25:  	Repositioning of subnavi if it doesn't fit
*****************************************/

$(document).ready (function () {
	// START e1 
		// bind: focus
		$('.cdVal').bind ('focus', function () {
			if ($(this).val() == $(this).attr('title')) {
				$(this).removeClass('dtActive');
				$(this).val('');
			};
		});
		// bind: blur
		$('.cdVal').bind ('blur', function () {
			if ($(this).val() == '') {
				$(this).addClass('dtActive');
				$(this).val($(this).attr('title'));
			};
		});		
		// iterate elms to set default text from title
		$('.cdVal').each (function () {
			$(this).addClass('dtActive');
			$(this).val($(this).attr('title'));							  
		});
	// END e1 
	
	// START e2 
		// bind: mouseover
		$('#leftNav li a').bind ('mouseover', function () {
			$('#leftNav li *').removeClass ('lnH');
			$(this).closest('li').children('.l1, .l2, .l3, .l4, span.navE').addClass('lnH');
		});
		
		// bind: mouseout
		$('#leftNav li a').bind ('mouseout', function () {
			$('#leftNav li *').removeClass ('lnH');
		});
	// END e2 
		
	// START e3
		$('#leftNav li.active').each (function () {
			$(this).children('.l1, .l2, .l3, .l4, span.navE').addClass('lnA');
		});
	// END e3 
		
	// START e4
	function toggleStageContainer (id) {
		$('.stageContainer').css('display', 'none');
		$('#con_'+id).fadeIn('slow');
	}; 

	$('.stagePg').each (function () {
		$(this).bind ('click', function () {
			currentElmNo = $(this).attr('id').split('_')[1];
			toggleStageContainer (currentElmNo);
		});
	});
	// END e4
	
	// START e5
		function toggleTabContainer (id) {
			$('.contentTab').css('display', 'none');
			$('#ct_'+id).fadeIn('slow');
		}; 
		$('.ctn').bind ('click', function () {
			currentId = $(this).attr('id').split('_')[1];
			toggleTabContainer (currentId);
			$('.ctn').removeClass('active');
			$(this).addClass('active');
		});	
	// END e5
	
	// START e6
		$('.tabAcc li').bind ('click', function () {
			if ($(this).find('ul').hasClass('sub')) {
				$(this).toggleClass('down');
				$(this).find('ul.sub').slideToggle('slow');
			};
		});
	// END e6
	
	
	// START e7		
		
		if (typeof (nyroModal) == 'function') {		
		
			$('a.nyroModal.w400').nyroModal ({
					closeButton: '', // Adding automaticly as the first child of #nyroModalWrapper 
					minWidth: 400,
					minHeight: 300
			});	
			
			$('a.nyroModal.w600').nyroModal ({
					closeButton: '', // Adding automaticly as the first child of #nyroModalWrapper 
					minWidth: 600,
					minHeight: 400
			});
			
			$('a.nyroModal.w735').nyroModal ({
					closeButton: '', // Adding automaticly as the first child of #nyroModalWrapper 
					minWidth: 735,
					minHeight: 600
			});
			$('a.nyroModal.w858').nyroModal ({
					closeButton: '', // Adding automaticly as the first child of #nyroModalWrapper 
					minWidth: 858,
					minHeight: 400
			});
			
			$('a.nyroModal.w800').nyroModal ({
					closeButton: '', // Adding automaticly as the first child of #nyroModalWrapper 
					minWidth: 800,
					minHeight: 600
			});
			
			$('a.nyroModal.w900').nyroModal ({
					closeButton: '', // Adding automaticly as the first child of #nyroModalWrapper 
					minWidth: 900,
					minHeight: 700
			});
			
			$('a.nyroModal.w960').nyroModal ({
					closeButton: '', // Adding automaticly as the first child of #nyroModalWrapper 
					minWidth: 960,
					minHeight: 800
			});
		
		
			
		};
	// END e7
	
	// START e8
		$('.buttonSubmit').bind ('mouseover', function () {
			$(this).find ('input').addClass ('hover');
			$(this).find ('b').addClass ('hover');
		});
		$('.buttonSubmit').bind ('mouseout', function () {
			$(this).find ('input').removeClass ('hover');
			$(this).find ('b').removeClass ('hover');
		});
	// END e8
	
	// START e9 
		/* START custom Bosch lightbox URL param processing and header include */
		$('.bLightbox').each (function () {
			var nyroUrl = $(this).attr('href');	
			function getUrlVars() {
				var guVars = [], guHash;
				var guHashes = nyroUrl.slice(nyroUrl.indexOf('?') + 1).split('&');
				for(var i = 0; i < guHashes.length; i++)
				{
					guHash = guHashes[i].split('=');
					guVars.push(guHash[0]);
					guVars[guHash[0]] = guHash[1];
				}
				return guVars;
			}
			
			var urlParams = getUrlVars();
			var currentblbHeight = 0;	
			var currentblbWidth = 0;	
			
			var blbTitle = $(this).attr('title');
			if (blbTitle == '' || blbTitle == 'undefined') {
				blbTitle = '';
			}
			
			if (urlParams.length > 0) {
				for (i = 0; i < urlParams.length; i++)	 {
					if (urlParams[i] == 'blbHeight') {
						if (urlParams['blbHeight'] < 250) {
							currentblbHeight = 250;
						} else {
							currentblbHeight = urlParams['blbHeight'];
						}
					}
					
					if (urlParams[i] == 'blbWidth')  {
						if (urlParams['blbWidth'] < 250) {
							currentblbWidth = 250;
						} else {
							currentblbWidth = urlParams['blbWidth'];
						}
					}
				};
			};	
			
			if (typeof (nyroModal) == 'function') {
				$(this).nyroModal ({
					closeButton: '', // Adding automaticly as the first child of #nyroModalWrapper 
					sizes: {	// Size information
						minW: currentblbWidth,	// minimum width
						minH: currentblbHeight	// minimum height
	
					},
					header: '<div class="blbHeader" style="width:'+currentblbWidth+'px"><h3>'+blbTitle+'</h3><a href="javascript:void(0);" class="nyroModalClose"><img src="images/icon_close.gif" alt="" /></a><div style="clear:both;"></div></div>',	// header include in every modal
					title: ''
					
				});
			};
				
		});
		/* END custom Bosch lightbox URL param processing and header include */	
			
	
	
		
		
		/* START custom Bosch popup URL param processing and header include */
		$('.bPopup').each (function () {
			var nyroUrl = $(this).attr('href');	
			function getUrlVars() {
				var guVars = [], guHash;
				var guHashes = nyroUrl.slice(nyroUrl.indexOf('?') + 1).split('&');
				for(var i = 0; i < guHashes.length; i++)
				{
					guHash = guHashes[i].split('=');
					guVars.push(guHash[0]);
					guVars[guHash[0]] = guHash[1];
				}
				return guVars;
			}
			
			var urlParams = getUrlVars();
			var currentblbHeight = 0;	
			var currentblbWidth = 0;
      var scrollbar = false;
      var resizable = false;	
			
			blbTitle = $(this).attr('title');
			if (blbTitle == '' || blbTitle == 'undefined') {
				blbTitle = '';
			}
			
			if (urlParams.length > 0) {
				for (i = 0; i < urlParams.length; i++)	 {
					if (urlParams[i] == 'blbHeight') {
						currentblbHeight = urlParams['blbHeight'];
					}
					if (urlParams[i] == 'blbWidth')  {
						currentblbWidth = urlParams['blbWidth'];
					}
					if (urlParams[i] == 'blbScroll')  {
						scrollbar = true;
					}
					if (urlParams[i] == 'blbResizable')  {
						resizable = true;
					}
				};
			};	
			
			
			$(this).bind ('click', function () {
				window.open(nyroUrl, blbTitle, "width="+currentblbWidth+",height="+currentblbHeight+"" + (scrollbar ? "" : ",scrollbars=yes") + (resizable ? "" : ",resizable=yes"));
				return false;
			});
				
		});
		/* END custom Bosch popup URL param processing and header include */
		
	// END e9
	
	// START e11 
		$('.foSub').bind('mouseenter', function () {
			$('.foRow').removeClass('hover');
			$(this).find('.foRow').addClass('hover');
		});
		$('.foSub').bind('mouseleave', function () {
			$('.foRow').removeClass('hover');
		});
	// END e11
	
});

// START e10
	var wURL = false;
	var wTarget = false;
	function jsLink (wURL, wTarget) {
		if (wURL) {
			if (wTarget && wTarget == '_blank') {
				window.open(wURL);
			} else {
				window.location.href = wURL;
			};
		} else {
			alert('URL undefined or broken');	
		};
	};
// END e10
// START  e17
function blbResizeIframe (whichWidth, whichHeight) {
	if ($('.nyroModalCont iframe').length > 0) {
		howHigh = whichHeight - $('.blbHeader').height();
		
		$('.nyroModalCont iframe').css( {
			'width': whichWidth+'px',
			'height': howHigh+'px',
			'overflow-x': 'hidden'
		});
	};
};

function initBlb() {
	$('.bLightbox').each (function () {
		var nyroUrl = $(this).attr('href');
		
		var currentblbHeight = 0;	
		var currentblbWidth = 0;
		
		var blbTitle = $(this).attr('title');
		if (blbTitle == '' || blbTitle == 'undefined') {
			blbTitle = '';
		}
		
		
		var inlHead = $(this).attr('inlHead');
		if (inlHead == '' || inlHead == 'undefined') {
			inlHead = '';
		} else {
			blbTitle = inlHead;
		}
		
		var inlWidth = $(this).attr('inlWidth');
		if (inlWidth == '' || inlWidth == 'undefined') {
			inlWidth = '';
		} else {
			currentblbWidth = inlWidth;
		}
		
		var inlHeight = $(this).attr('inlHeight');
		if (inlHeight == '' || inlHeight == 'undefined') {
			inlHeight = '';
		} else {
			currentblbHeight = inlHeight;
		}
		
		
		$(this).nyroModal ({
			_loading: true,			
			closeButton: '', // Adding automaticly as the first child of #nyroModalWrapper 
			sizes: {	// Size information
				minW: currentblbWidth,	// minimum width
				minH: currentblbHeight	// minimum height
	
			},
			header: '<div class="blbHeader" style="width:'+currentblbWidth+'px"><h3>'+blbTitle+'</h3><a href="javascript:void(0);" class="nyroModalClose"><img src="http://www.bosch-professional.com/media/images/icon_close.png" alt="" /></a><div style="clear:both;"></div></div>',	// header include in every modal
			title: '',
			callbacks: {                    
				beforeShowBg: function(){
					blbResizeIframe(currentblbWidth, currentblbHeight);
					//resizeNyroBox(currentblbWidth, currentblbHeight);
				},                
				afterShowCont: function(){
					//resizePopupContent(currentblbWidth, currentblbHeight);
				}
			}			
			
		});
	});	
};

// END e17


// START e25
$(document).ready(function () {
	if ($('.foSub').length > 0) { // if subnavi exists
		var subNaviWidth = 724; // navi visible width (maybe smaller than $(this).find('.foRow').width())
		var siteMargin = ( $(document).width() - $('#site').width() ) / 2;  // left and right margin
		var siteWidth = $('#site').width();  // #site width
		
		$('.foSub').each (function (i) { // loop through navi elements
			var curNavLeft = $(this).offset().left - siteMargin; // left position of current navi element
			var curTotalWidth = curNavLeft + subNaviWidth;  // total width of left position plus subnavi width
			
			//console.log ('siteMargin: ' + siteMargin);	
			//console.log ('siteWidth: ' + siteWidth);	
			//console.log ('curNavLeft: ' + curNavLeft);	
			//console.log ('subNaviWidth: ' + subNaviWidth);	
			//console.log ('curTotalWidth: ' + curTotalWidth);
			
			if (curTotalWidth > siteWidth) {   // if subnavi doesnt fit 
				$(this).find ('.foRow').css('margin-left', '-' + (curTotalWidth - siteWidth) + 'px'); // repos subnavi width neg margin-left
			};
		});
	};
});
// END e25

