//jQ = jQuery.noConflict();

//###########################################################################
function isNum (elem) {
	if(isNaN(elem.value)) {
		elem.style.backgroundColor = '#ff0000';
	} else {
		elem.style.backgroundColor = 'transparent';
	}
};

//###########################################################################
function isIE6() {
	var isIE = false;
	jQuery.each(jQuery.browser, function(i, val) {
	  if(i=="msie" && jQuery.browser.version.substr(0,3)=="6.0")
	     isIE = true;
	});
	return isIE;
}

//###########################################################################
function getWindowSize () {
	var size = new Array();

	if (window.innerHeight) {
		size = [window.innerWidth,window.innerHeight];
		return size;
		
	} else if (window.document.documentElement.clientHeight) {
		size = [window.document.documentElement.offsetWidth,window.document.documentElement.offsetHeight];
		return size;
		
	} else if (window.document.body.clientHeight) {
		size = [window.document.body.offsetWidth,window.document.body.offsetHeight];
		return size;
	}
}

//###########################################################################
function showProfileNav() {
	//set min-width 135px for ie6
	if(isIE6()) {
		if(jQuery(".showProfile").width() < 135) {
			jQuery(".showProfile").css("width", "11.25em");
		}
	}
	
	jQuery(".showProfile").hover(function() {
		var ulHeight = ((jQuery("#profileNav").height() + jQuery(this).height())/12)+"em";
		jQuery(this).css({height:ulHeight, backgroundColor:"#ffffff"});
		jQuery("#profileNav").show();
		
		//IE Hover Sublevel
		jQuery("#profileNav > li").hover(function() {
			jQuery(this).css("backgroundColor","#dcdcdc")
		},
		function(){
			jQuery(this).css("backgroundColor","#e8e8e8")
		});
	},
	function() {
		jQuery(this).css("height","auto");
		jQuery("#profileNav").hide();
	});
}

//###########################################################################
function getBtnSize() {
	if(document.getElementById("btn")) {
		var btn = document.getElementById("btn");
		var btnWrapper = jQuery(".button_wrapper")[0];
		var btnShadowR = jQuery(".button-shadow-r")[0];
		
		var btnWidth = (btn.offsetWidth+12)/11;
		var shadowPos = (btn.offsetWidth+4)/11;
	
		btnWrapper.style.width = btnWidth+"em";
	}
}

//###########################################################################
/*function initMainNav() {
	//alert("initMainNav");
	if(jQuery("#main_ul")) {
		//initialisation hover images
		jQuery(".main_img_hover").hide();
	
		//initialisation 2nd level navigation
		jQuery("#main_ul > li ul").hide();
		
		jQuery("#main_ul > li").mouseover(function(){
			if(jQuery(this).hasClass("on")) {
				//alert("Hover");
			} else {
				//show hover image; hide inactive image
				jQuery(".main_img_hover",this).show();
				jQuery(".main_img_inactive",this).hide();
				
				//show 2nd level navigation
				jQuery("ul",this).css("width",jQuery(this).width());
				jQuery("ul",this).show();
				
				//IE Hover Sublevel
				jQuery("li",this).hover(function() {
					jQuery(this).css("backgroundColor","#dcdcdc")
				},
				function(){
					jQuery(this).css("backgroundColor","#e8e8e8")
				});
			}
		});
		
		jQuery("#main_ul > li").mouseout(function(){
			//hide hover image; show inactive image
			jQuery(".main_img_hover",this).hide();
			jQuery(".main_img_inactive",this).show();
			
			//hide 2nd level navigation
			jQuery("ul",this).hide();
			jQuery("ul",this).css("width","auto");
		});
	}
}*/

// Creating namespaces
var coca = {}

coca.createNavigation = function() {
	jQuery(document.body).append('<div id="Submenu"></div>');
	
	var objNavi = jQuery('#main_ul');
	var objSubNavi = jQuery('#Submenu');
	var blnMouseOverSubNavi = false;
	var timer;
	
	jQuery('a.childs', objNavi).parent().hover(
		function() {
			if(jQuery(this).hasClass('off')) {
				thisElem = this;
				
				clearTimeout(timer);
				
				var objSubNaviClone = jQuery('ul', this).clone(true);
				objSubNavi.empty().append('<!--[if lte IE 6]><iframe></iframe><![endif]-->');
				objSubNavi.append(objSubNaviClone);
				var offsets = jQuery(this).offset();
				var height = jQuery(this).height() + 4; //padding
				
				//Breite Subnav anpassen, falls kleiner als Elternelement
				if(objSubNavi.width() <= jQuery('img', this).width()) {
					objSubNavi.css("width", jQuery('img', this).width());
				} else {
					objSubNavi.css("width", "");
				}
				
				objSubNavi.css("top", (offsets.top + height));
				objSubNavi.css("left", offsets.left);
				
				//show hover image; hide inactive image
				jQuery('.main_img_hover', this).show();
				jQuery('.main_img_inactive', this).hide();
				
				//Hover Submenu
				jQuery('li', objSubNavi).hover(
					function() {
						jQuery(this).css("backgroundColor", "#dcdcdc");
					},
					function() {
						jQuery(this).css("backgroundColor", "#e8e8e8");
					}
				);
				
				objSubNavi.show();
			};

		},
		function() {
			timer = window.setTimeout("coca.closeSubNavi()", 250);
			//hide hover image; show inactive image
			jQuery('.main_img_hover', this).hide();
			jQuery('.main_img_inactive', this).show();
		}
	);
	
	jQuery(objSubNavi).hover(
		function() {
			clearTimeout(timer);
			
			//show hover image; hide inactive image
			jQuery('.main_img_hover', thisElem).show();
			jQuery('.main_img_inactive', thisElem).hide();
		},
		function() {
			timer = window.setTimeout("coca.closeSubNavi()", 250);
			//hide hover image; show inactive image
			jQuery('.main_img_hover', thisElem).hide();
			jQuery('.main_img_inactive', thisElem).show();
		}
	);
}

coca.closeSubNavi = function () {
	jQuery("#Submenu").removeAttr("style");
}

//###########################################################################
function clearSearch(elem) {
	var defaultValue = document.getElementById('metasearch:defaulttext').firstChild.nodeValue;
	if (elem.value == defaultValue) {
		elem.value = "";
	}
}

function clearSearchB2B(elem) {
	var defaultValue = document.getElementById('searchForm:stichwort').firstChild.nodeValue;
	if(elem.value == defaultValue) {
		elem.value = "";
	}
}

//###########################################################################
function styleTables() {
	var tables = jQuery('.textParagraph table');
	tables.wrap('<div class="user_table"></div>');
	tables.after('<div class="corner corner-tl"></div><div class="corner corner-tr"></div><div class="corner corner-bl"></div><div class="corner corner-br"></div>');
	
	var th = jQuery('.textParagraph table th');
	//th.wrapInner('<span></span>');
	th.after('<th class="tbl_separator"></th>');
	
	var td = jQuery('.textParagraph table td');
	td.after('<td class="tbl_separator"></td>');
	
	var last_td = jQuery('.textParagraph table tbody tr:last');
	last_td.addClass('last');
}

function initFlashVideos() {
	
	var videos = jQuery('.ccbvideomedia');
	jQuery.each(videos, function(index, value) {
		
		//value = jQuery(value);
		var s1 = new SWFObject("/flash/player.swf","ply", value.getAttribute("flvWidth"), value.getAttribute("flvHeight"),"9","#FFFFFF");
		var autoplay = "false";
		var controlbar = "";
		
		// Autoplay for flipteasers = true
		if(jQuery('#flip')){
			if(jQuery(value).parent("#flip").length > 0){
				autoplay = "true";
			}
		}
		
		// check if controllbar recommended 
		if(value.getAttribute("flvWidth") < 180){
			controlbar = "&controlbar=none";
		}
		
		s1.addParam("flashvars","type=video&fullscreen=true&repeat=always"+controlbar+"&autostart="+autoplay+"&file="+(value.getAttribute("flvSrc").replace(/\+/g, "%2b")));
		
		s1.addParam("allowfullscreen","true");
		s1.addParam("wmode","transparent");
		
		
		
		s1.write(value.id);
	});

}

function initialiseScrollPane() {
	jQuery('.scroll-pane').jScrollPane({fontFactor: 11, maxHeight: 287, scrollbarWidth: 9, scrollbarMargin: 0, showArrows:true, arrowSize: 9});
}

//###########################################################################
function openOverlay () {
	//add event handler
	
	wrapperElem = document.getElementById("overlay_wrapper");
	wrapperElem.style.display = "block";
	contentElem = document.getElementById("overlay_content");
	contentElem.removeAttribute("style");
	contentElem.style.visibility = "visible";
	
	var browserSize = getWindowSize();
	var left = (browserSize[0]-contentElem.offsetWidth)/2;
	contentElem.style.left = left+"px";
	
	if(isIE6()) {
		if(window.document.documentElement.offsetHeight > window.document.body.offsetHeight) {
			layerHeight = window.document.documentElement.offsetHeight;
		} else {
			layerHeight = window.document.body.offsetHeight;
		}
		
		wrapperElem.style.height = layerHeight+"px";
		
		//create a clickable div to close overlay
		clicker = document.createElement("div");
		clicker.style.width = browserSize[0]+"px";
		clicker.style.height = browserSize[1]+"px";
		wrapperElem.appendChild(clicker);
		clicker.onclick = closeOverlay;
		
		//create iframe for selectbox fix
		/*iframe = document.createElement("iframe");
		wrapperElem.appendChild(iframe);*/
		
	} 
	
	window.onresize = openOverlay;
	wrapperElem.onclick = closeOverlay;
}

function closeOverlay() {
	wrapperElem.style.display = "none";
	if(isIE6()) {
		contentElem.style.visibility = "hidden";
	} else {
		//BEGIN IE7 HACK FOR VISIBILITY BUG//
		contentElem.style.visibility = 'hidden';
		contentElem.style.height = '1px';
		contentElem.style.overflow = 'hidden';
		//END HACK//
	}
	
	window.onresize = null;
}

//###########################################################################

function checkMessage(elem) {
	str = elem.value;
	//alert(str);
	if(str.length > 57) {
		elem.value = str.substring(0, 57);
		elem.style.backgroundColor = '#ff0000';
	} else {
		elem.style.backgroundColor = '#ffffff';
	}
}
//###########################################################################
function sendOnEnter() {
	//Main Nav Suche
	jQuery("#metasearch\\:searchtext").keyup(function(e) {
	    if(e.keyCode == 13) {
	        jQuery("#metasearch\\:searchsubmit").trigger('click');
	    }
	});
	
	//B2B Produktsuche
	jQuery("#searchForm\\:query").keyup(function(e) {
	    if(e.keyCode == 13) {
	        jQuery("#searchForm\\:searchButton").trigger('click');
	    }
	});
	
	//MD Finder Suche
	jQuery("#mdFinderPara\\:mdfinder_text").keyup(function(e) {
	    if(e.keyCode == 13) {
	        jQuery("#mdFinderPara\\:mdfinder_submit").trigger('click');
	    }
	});
}

//###########################################################################

jQuery(document).ready(function(){
	//initMainNav();
	coca.createNavigation();
	showProfileNav();
	if(jQuery(".scroll-pane").length > 0) {
		initialiseScrollPane();
	}
	styleTables();
	initFlashVideos();
	sendOnEnter();
	

	if (document.getElementById('yellowpayPopup')) {
		document.getElementById('paymentInfoForm').submit();
	}
});

