var timer1;
clearTimeout(timer1);
var timer2;
clearTimeout(timer2);
$(document).ready( function()
{
	switch(page)
	{
		case 'index': ;
			break;
		case 'company': $('.menu_company').show();
			break;
		case 'products': $('.menu_products').show();
			break;
	}
    $('.main').mouseenter( function() {
        if( ! $('.menu').prev().hasClass('main_nav_' + page + 'on') || page == 'index') {
            $('.menu').prev().removeAttr('className');
        }
        $('.menu_' + page).prev().addClass('main_nav_' + page + '_on');
        $('.menu_' + page).show();
    })

    $('.header').mouseenter( function() {
        $('.menu_' + page).prev().addClass('main_nav_' + page + '_on');
	$('.menu_' + page).show();
    })
    $('.menu').mouseleave( function() { 
        $('.menu' + page).prev().addClass('main_nav_' + page + '_on');
	
    })
    $('.main_nav_list').mouseleave( function() {
        $('.menu_' + page).prev().addClass('main_nav_' + page + '_on');
	$('.menu_' + page).show();
    })
    $('.main_nav_relations').mouseleave( function() {
	$('.menu ul').css({'height':'0', 'overflow': 'hidden'});
	$('.menu').hide();
    })

    Tooltips.roll.init();
    NavProducts.roll.init();
    DropDowns.roll.init();
    ProductWin.roll.init();
    SideBarWin.roll.init();
    Right_nav.roll.init();
    contactForm.roll.init();
});

	var menu=function(){
		var t=15,z=50,s=1,a;
		function dd(n){this.n=n; this.h=[]; this.c=[]}
		dd.prototype.init=function(p,c){
			a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
			for(i;i<l;i++){
				var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
				if( $.browser.msie ) {
					h.onmouseenter=new Function(this.n+'.st('+i+',true)');
					h.onmouseleave=new Function(this.n+'.st('+i+')');
				}
				else {
					h.onmouseover=new Function(this.n+'.st('+i+',true)');
					h.onmouseout=new Function(this.n+'.st('+i+')');
				}
			}
		}
		dd.prototype.st=function(x,f){
			var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
			clearInterval(c.t); c.style.overflow='hidden';
			if(f){
				p.className+=' '+a;
				if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
				if(c.mh==c.offsetHeight){c.style.overflow='visible'}
				else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
			}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
		}
		function sl(c,f){
			var h=c.offsetHeight;
			if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
				if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible';}
				clearInterval(c.t); return
			}
			var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
			c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
			c.style.height=h+(d*f)+'px'
		}
		return{dd:dd}
	}();
	
	var menu1=function(){
		var t=15,z=50,s=1,a;
		function dd(n){this.n=n; this.h=[]; this.c=[]}
		dd.prototype.init=function(p,c){
			a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
			for(i;i<l;i++){
				var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
				if( $.browser.msie ) {
					h.onmouseenter=new Function(this.n+'.st('+i+',true)');
					h.onmouseleave=new Function(this.n+'.st('+i+')');
				}
				else {
					h.onmouseover=new Function(this.n+'.st('+i+',true)');
					h.onmouseout=new Function(this.n+'.st('+i+')');
				}
			}
		}
		dd.prototype.st=function(x,f){
			var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
			clearInterval(c.t); c.style.overflow='hidden';
			if(f){
				p.className+=' '+a;
				if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
				if(c.mh==c.offsetHeight){c.style.overflow='visible'}
				else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
			}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
		}
		function sl(c,f){
			var h=c.offsetHeight;
			if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
				if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible';}
				clearInterval(c.t); return
			}
			var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
			c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
			c.style.height=h+(d*f)+'px'
		}
		return{dd:dd}
	}();

Right_nav = {};
Right_nav.roll =
{
	init:		function()
	{
        $('.pr_categories a').each(function() {
				  $(this).mouseover( function() {
                		$('.pr_categories a.on').addClass('on_leave');
           	 		})
            	 $('.pr_categories').mouseleave( function() {
             	   	$('.pr_categories a.on').removeClass('on_leave');
           		 })
				 $('.pr_categories a.on').mouseover( function() {
             	   	$('.pr_categories a.on').removeClass('on_leave');
           		 })				 
   		});
	}
};


Tooltips = {};
Tooltips.roll =
{
	init:		function()
	{
        $('.right_item_keep_share a').each( function () {

            $(this).mouseover( function() {
                $('.white_box_link_' + $(this).attr('className')).fadeIn();
            })
            $(this).mouseout( function() {
                $('.white_box_link_' + $(this).attr('className')).fadeOut();
            })
        });
	}
};

//-- Drop Downs
NavProducts = {};
NavProducts.roll =
{
	init:		function()
	{
		$(".main_nav_company").mouseenter(function() {
			$('#' + page + '_link').removeClass('main_nav_' + page + '_on' );
			NavProducts.roll.showMenu($('.menu_company'), 'company');
			NavProducts.roll.removeShadow();
		})
        
		$(".main_nav_company").mouseleave(function() {
			NavProducts.roll.hideMenu($('.menu_company'), 'company');
			NavProducts.roll.addShadow($('.menu_company'));
		})
    
		$(".main_nav_products").mouseenter(function() {
			$('#' + page + '_link').removeClass('main_nav_' + page + '_on' );
			NavProducts.roll.showMenu($('.menu_products'), 'products');
			NavProducts.roll.removeShadow();
		})
        
		$(".main_nav_products").mouseleave(function() {
			NavProducts.roll.hideMenu($('.menu_products'), 'products');
			NavProducts.roll.addShadow($('.menu_products'));
		})
		
		$(".main_nav_center").mouseenter(function() {
			$('#' + page + '_link').removeClass('main_nav_' + page + '_on' );
			NavProducts.roll.showMenu($('.menu_center'), 'center');
			NavProducts.roll.removeShadow();
		})
		
		$(".main_nav_center").mouseleave(function() {
			NavProducts.roll.hideMenu($('.menu_center'), 'center');
			NavProducts.roll.addShadow($('.menu_center'));
		})
		
		$(".main_nav_support").mouseenter(function() {
			$('#' + page + '_link').removeClass('main_nav_' + page + '_on' );
			NavProducts.roll.showMenu($('.menu_support'), 'support');
			NavProducts.roll.removeShadow();
		})
		
		$(".main_nav_support").mouseleave(function() {
			NavProducts.roll.hideMenu($('.menu_support'), 'support');
			NavProducts.roll.addShadow($('.menu_support'));
		})
		
		$(".main_nav_careers").mouseenter(function() {
			$('#' + page + '_link').removeClass('main_nav_' + page + '_on' );
			NavProducts.roll.showMenu($('.menu_careers'), 'careers');
			NavProducts.roll.removeShadow();
		})
		
		$(".main_nav_careers").mouseleave(function() {
			NavProducts.roll.hideMenu($('.menu_careers'), 'careers');
			NavProducts.roll.addShadow($('.menu_careers'));
		})
		
		$(".main_nav_relations").mouseenter(function() {
			$('#' + page + '_link').removeClass('main_nav_' + page + '_on' );
			NavProducts.roll.showMenu($('.menu_relations'), 'relations');
			NavProducts.roll.removeShadow();
		})
		
		$(".main_nav_relations").mouseleave(function() {
			NavProducts.roll.hideMenu($('.menu_relations'), 'relations');
			NavProducts.roll.addShadow($('.menu_relations'));
		})
	},

	addShadow:	function()
	{
	        $('.shadow_bottom .shadow_bottom_internal').show();
	},

	removeShadow:	function()
	{
	        $('.shadow_bottom .shadow_bottom_internal').hide();
	},

	showMenu:	function(menu, button)
	{
	        menu.prev().addClass('main_nav_' + button + '_on');
		$('.menu').hide();
		clearTimeout(timer1);
		clearTimeout(timer2);
		menu.show();
	},

	hideMenu:	function(menu, button)
	{
		if( page != button ) {
			//alert('button');
			//timer2=setTimeout("$('.menu').prev().addClass('main_nav_" + button + "_on')",500);
			menu.prev().removeClass('main_nav_' + button + '_on');
			timer2=setTimeout("$('.menu ul').css({'height':'0px', 'overflow': 'hidden'});",500);
			//$('.menu ul').css({'height':'0px', 'overflow': 'hidden'});
		}
		timer1=setTimeout("$('.menu').hide()",500);
		//menu.hide();
	}
};

//-- Drop Downs
DropDowns = {};
DropDowns.roll =
{
	init:		function()
	{
		$(".dropDown").click(function() {
			DropDowns.roll.showMenu($(this).children(".ddMenu"))
		})
		
		$(".dropDown").children(".ddMenu").mouseleave(function() { 
			DropDowns.roll.hideMenu($(".dropDown").children(".ddMenu")) 
		})
		
		$('.dd_close_button').click(function() {
			DropDowns.roll.hideMenu($(".dropDownFooter").children(".ddMenu"))
			return false;
		})

		
		$(".dropDownFooter").click(function() {
			DropDowns.roll.showMenu($(this).children(".ddMenu"))
		})

		$(".dropDownFooter").children(".ddMenu").mouseleave(function() { 
		
			DropDowns.roll.hideMenu($(".dropDownFooter").children(".ddMenu"));
		})

		$(".dd_header").click(function() {
			DropDowns.roll.showMenu($(this).children(".ddMenu"));
		})
		

		$(".dd_header").children(".ddMenu").mouseleave(function() { 		
			DropDowns.roll.hideMenu($(".dd_header").children(".ddMenu"));	
		})
		
		
	},
	
	showMenu:	function(menu)
	{
		//-- If has class "up", make reveal above button
		if ($(menu).hasClass("up")) {
			$(menu).css({"margin-top":-$(menu).height() + $(menu).parent().children(".ddBtn").height()});
		}
		
		menu.fadeIn();
	},
	
	hideMenu:	function(menu)
	{
		menu.fadeOut();
	}
};




contactForm = {};
contactForm.roll =
{
	init:		function()
	{
		$(".c_us").click( function() { 
				contactForm.roll.showProdWin($(this), $("#contactForm"));
				ProductWin.roll.hideProdWin($("#prodWinTable"));
		});
			
		$("#contactForm").children(".close").children(".hide").click(function() {
			contactForm.roll.hideProdWin($("#contactForm"));
			 $("input").val("");  $("textarea").val(""); 
		});
		
		$(".btn_send").click(function() {
			contactForm.roll.hideProdWin($("#contactForm"));
			 $("input").val("");  $("textarea").val(""); 
		});
	
	},
	
	showProdWin:	function(target, prodWin)
	{
		contactForm.roll.positionBubble(target, prodWin);
		prodWin.fadeIn('slow');
	},

	positionBubble: function(target, prodWin) 
	{
/*		
		newY = ((offset.top + 10) - (prodWin.height() / 2) + target.children(".c1").height());
		prodWin.css({'left':newX + 'px', 'top':newY + 'px'});
		var prodWinH = prodWin.height();
		prodWin.children('.side').css({'height':prodWinH + 'px'})
		var arrowH = arrow.children('.arrow').css('height');
		arrowH = arrowH.replace("px","");
		var arrowY = (prodWinH / 2) - arrowH;
*/
		var offset = target.offset();
		var newY = (offset.top - 4) - (prodWin.height() / 2 );
		var newX = '160';
		prodWin.css({'left':newX + 'px', 'top':newY + 'px'});
		var arrow = prodWin.children('.side');
		if( $.browser.version == 7.0) {
			arrow.css({'top':(prodWin.height()/2 - 32) + 'px'});
		}
		else {
			arrow.css({'top':(prodWin.height()/2 - 32) + 'px'});
		}
		prodWin.children(".side").children(".arrow").css({'background':'transparent url(images/prod_bubble/prod_arrow_r.gif) no-repeat'});
	},
	
	hideProdWin:	function(prodWin)
	{
		prodWin.fadeOut();
	}
};

//-- Product popup bubbles
ProductWin = {};
ProductWin.roll =
{
	init:		function()
	{
		//$(".c").mouseover( function() { 
		//		ProductWin.roll.showProdWin($(this), $("#prodWinTable"));
		//		contactForm.roll.hideProdWin($("#contactForm"));
		//});

		$("#prodWinTable").hoverIntent(
			function() {},
			function() {
				ProductWin.roll.hideProdWin($("#prodWinTable"));
			}
		);		
			
		$("#prodWinTable").children('.close').children(".hide").click(function() {
			ProductWin.roll.hideProdWin($("#prodWinTable"));
		});
	
	},
	
	showProdWin:	function(target, prodWin)
	{	
		ProductWin.roll.positionBubble(target, prodWin);
		$("#prodWinTable").find(".mainContent").html("<span class=\"loading\">Loading...</span>");
		prodWin.fadeIn();
		
//		bodyContent = $.ajax({
//			url: this.getFragmentURL(target.parent().attr("id")),
//			global: false,
//			type: "GET",
//			data: ({	id 		: target.parent().attr("id"),
//						title	: target.attr("title")
//				   }),
//			dataType: "html",
//			success: function(msg){
				$("#prodWinTable").find(".mainContent").html($(target).find(".hiddenPopup").html());
				ProductWin.roll.positionBubble(target, prodWin);
//			}
//		});
	},
    
    getFragmentURL: function (id)
    {
        return id + ".html";
    },

	positionBubble: function(target, prodWin) 
	{
/*		
		newY = ((offset.top + 10) - (prodWin.height() / 2) + target.children(".c1").height());
		prodWin.css({'left':newX + 'px', 'top':newY + 'px'});
		var prodWinH = prodWin.height();
		prodWin.children('.side').css({'height':prodWinH + 'px'})
		var arrowH = arrow.children('.arrow').css('height');
		arrowH = arrowH.replace("px","");
		var arrowY = (prodWinH / 2) - arrowH;
*/
		var offset = target.children(".c1").offset();
		var newY = (offset.top + 10) - (prodWin.height() / 2 );
		var newX = '270';
		prodWin.css({'left':newX + 'px', 'top':newY + 'px'});
		var arrow = prodWin.children('.side');
		var closeBtn = prodWin.children('.close');
		if( $.browser.version == 7.0) {
			arrow.css({'top':(prodWin.height()/2 - 20) + 'px', 'right':0 + 'px'});
			closeBtn.css('right', '0');
		}
		else {
			arrow.css({'top':(prodWin.height()/2 - 20) + 'px'});
		}
		prodWin.children(".side").children(".arrow").css({'background':'transparent url(/images/prod_bubble/img_prod_arrow_left.gif) no-repeat'});
		prodWin.children(".side").css({'float':'left'});
	},
	
	hideProdWin:	function(prodWin)
	{
		prodWin.fadeOut();
	}
};


//-- Sidebar popup bubbles
SideBarWin = {};
SideBarWin.roll =
{
	init:		function()
	{
		$(".sideItemFirst").mouseover( function() {
			$('.second').hide();
			$('.third').hide();
			if( $(this).parent().hasClass('right_link_small')) {
	            $('.sideItemSecond').parent().removeClass('right_link_small_on');
	            if( $('.sideItemThird') ) {
	                $('.sideItemThird').parent().removeClass('right_link_small_on');
	            }
			}
			else {
	            $('.sideItemSecond').parent().parent().removeClass('white_box_link_on');
	            if( $('.sideItemThird') ) {
	                $('.sideItemThird').parent().parent().removeClass('white_box_link_on');
	            }
			}
        });

		$(".sideItemSecond").mouseover( function() {
			SideBarWin.roll.showProdWin($(this), $('.second'));
			$('.third').hide();
			if( $(this).parent().hasClass('right_link_small')) {
	            $('.sideItemFirst').parent().removeClass('right_link_small_on');
	            if( $('.sideItemThird') ) {
	                $('.sideItemThird').parent().removeClass('right_link_small_on');
	            }
			}
			else {
            $('.sideItemFirst').parent().parent().removeClass('white_box_link_on');
	            if( $('.sideItemThird') ) {
	                $('.sideItemThird').parent().parent().removeClass('white_box_link_on');
	            }
			}
        });
        
		$(".sideItemThird").mouseover( function() {
			SideBarWin.roll.showProdWin($(this), $('.third'));
			$('.second').hide();
			$('.sideItemFirst').parent().removeClass('right_link_small_on');
			if( $(this).parent().hasClass('right_link_small')) {
	            $('.sideItemSecond').parent().removeClass('right_link_small_on');
	            if( $('.sideItemSecond') ) {
	                $('.sideItemSecond').parent().removeClass('right_link_small_on');
	            }
			}
			else {
            $('.sideItemFirst').parent().parent().removeClass('white_box_link_on');
	            if( $('.sideItemFirst') ) {
	                $('.sideItemFirst').parent().parent().removeClass('white_box_link_on');
	            }
			}
        });

		$(".sideItem").mouseover( function() {
            SideBarWin.roll.showProdWin($(this), $("#prodWin"));
			if( $(this).parent().hasClass('right_link_small')) {
	            $(this).parent().addClass('right_link_small_on');
			}
			else {
	            $(this).parent().parent().addClass('white_box_link_on');
			}			
        });

		$(".sideItem").mouseleave( function() {
			if( $(this).parent().hasClass('right_link_small')) {
	            $('right_link_small').removeClass('right_link_small_on');
			}
			else {
	            $('white_box_link').removeClass('white_box_link_on');
			}
         });
    
    
		$("#prodWin").mouseleave( function() {
			if( $('.right_link_small')) {
	            $('.right_link_small').removeClass('right_link_small_on');
			}
			else {
            	$('white_box_link').removeClass('white_box_link_on');
			}
         });
         
		$("#prodWin").mouseover( function() {
            $('white_box_link').addClass('white_box_link_on');
         });

		$("#prodWin div.body_small").mouseleave(
			function() {SideBarWin.roll.hideProdWin($("#prodWin"));}
		);		
		
		$("#page").mouseleave(
			function() { SideBarWin.roll.hideProdWin($("#prodWin"));}
		);

		$(".pr_categories").mouseenter(
			function() { SideBarWin.roll.hideProdWin($("#prodWin"));}
		);
		
		$(".insite_title").mouseenter(
			function() { SideBarWin.roll.hideProdWin($("#prodWin"));}
		);
		
		$(".title").mouseenter(
			function() { SideBarWin.roll.hideProdWin($("#prodWin"));}
		);

		$("#contact_us").mouseenter(
			function() { SideBarWin.roll.hideProdWin($("#prodWin"));
        });
        
		$("#prodWin").mouseover( function() { 
            $("#prodWinTable").hide();
        });
	},

	showProdWin:	function(target, prodWin)
	{	
		SideBarWin.roll.positionBubble(target, prodWin);
		prodWin.show();
				
	},

	positionBubble: function(target, prodWin) 
	{	
		var offset = target.children(".top").offset();
		var newY = ((offset.top) - (prodWin.height() / 2) + target.height()/2 + 6 );
		prodWin.css({'left':0 + 'px', 'top':newY + 'px'});

	},

	hideProdWin:	function(prodWin)
	{
        $('.right_link_small').removeClass('right_link_small_on');
		$('.white_box_link').removeClass('white_box_link_on');
		$("#prodWin").hide();
		$('.second').hide();
		$('.third').hide();
	}
};

