
$(function() {
	sideMenu(); 
    $('a.external').click( function() {
        window.open(this.href);
        return false;
    });
});


 
function sideMenu()
{
  $('#menu ul').hide();
  //$('#menu ul:first').show();
 	$('.cat_product #menu ul#menu_products').show();
	$('.cat_science #menu ul#menu_science').show();
	$('.cat_home #menu ul#menu_products').hide();
/*	$('#corporate_home #menu ul#menu_science').hide();*/
  $('#menu li a:not(".menu_perform a")').click(
    function() {
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        return false;
        }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('#menu ul:visible').slideUp(100);
        checkElement.slideDown(100);

		$('li .expand').each(function(index) {
			$(this).css("backgroundPosition","0 0");
		});
 
		$(this).css("backgroundPosition","-150px 0");
		
        return false;
        }
      }

    );
}


function expandCollapse()
{
	$('.expandmenu ul').hide();
	$('.expandmenu li a.heading_main').click(function(){
		//alert('x');


		$('.expandmenu li a span').each(function(index) {
    		//alert(index + ': ' + $(this).text());
			//$(this).css("backgroundPosition","0px 0");
		});

			$(this).next().slideToggle('normal');
	
	});

/*
$('.expandmenu li a.heading_main').toggle(function () {
    //$("#user_button").css({borderBottomLeftRadius: "0px"});
	//$(this).next().css("backgroundPosition","8px 0");
	//alert($(this).attr('id'));

	var tempid = $(this).attr('id') + ' span';
	$('#'+tempid+'').css("backgroundPosition","8px 0");
//alert(tempid);



//$(this).css("color","white");
	//$(this).children().css("backgroundPosition","0 0");

}, function () {

	var tempid =  $(this).attr('id') + ' span';
	$('#'+tempid+'').css("backgroundPosition","0 0");

});*/

}

function productMouse()
{

	var opened = false;
	var rolled = false;

	$('#products_content .product a').mouseover(
	function() {
		$('#products_content .product a').each(function(index) {
			$(this).addClass("active");
			//alert($(this).parent().attr('id'));
		});
			$(this).removeClass("active");

			var visibleid = $(this).parent().attr('id');
			//alert("#" + visibleid + "_popup");
			$('#products_popup').children().hide();
			$("#" + visibleid + "_popup").show();
	});


	$('#products_popup .popup a').mouseover(
	function() {
		$('#products_content').children().each(function(index) {
			var targets = $(this).parent().attr('id');
			//alert(targets);
			//targets.addClass("active");
			$(this).children().addClass("active");

		});
		
		var applyname = $(this).parent().attr('id')
		applynum = applyname.indexOf('pop') -1;
		applyname = applyname.substring(0, applynum);
		
		//alert(applyname); //product_1_popup

		//$("#product_1 a").removeClass("active");
		$("#" + applyname + " a").removeClass("active");
	
	});

	$('#products').mouseout(
	function(e) {

		var orX, orY;
		var ep = $('#products').offset();
		orX = ep.left;
		orY = ep.top;
		var ew = $('#products').width();
		var eh = $('#products').height();
	
		if((e.pageX < orX || e.pageX > orX+ew) || (e.pageY < orY || e.pageY > orY+eh)) {
			$('#products .product a').each(function(index) {
				$(this).removeClass("active");
				$('.popup').hide();
			});
		}

		$('#products_content .product a').each(function(index) {
			
			if(!opened)
			{
				$(this).removeClass("active");
			}
		});

		if(rolled)
			$('.popup').hide();

	});	
	
	
	// click on 'bottle'
	$('#products_content').children().click(function() {
		var idname = $(this).attr('id');
		$('.elemId').attr('value', idname);
		$('#' + idname + '_modal').jqm({onShow:setEvent,onHide:resetOpened}).jqmShow();
		opened = true;
		//$('#products_popup').children().hide();
		$('#' + idname + ' a').removeClass("active");
		DimImage();
	});

	// click on 'information'
	$('#products_popup,#btn-nutritional-information').children().click(function() {
		var idname = $(this).attr('id');
		idname = idname.replace("_popup", "");
		$('#' + idname + '_modal').jqm({onShow:setEvent,onHide:resetOpened}).jqmShow();
		opened = true;
		//$('#products_popup').children().hide();
		$('#' + idname + ' a').removeClass("active");
		DimImage();
	});
	
	//small popups
	$('.small-pop').click(function() {
		var idname = $(this).attr('id');
		idname = idname.replace("_popup", "");
		$('#' + idname + '_modal').jqm({onShow:setEvent,onHide:resetOpened}).jqmShow();
		opened = true;
		//$('#products_popup').children().hide();
		$('#' + idname + ' a').removeClass("active");
		DimImage();
	});

	var setEvent = function(hash){
		hash.w.show();
		var orX, orY;
		var ep = $('#products').offset();
		orX = ep.left;
		orY = ep.top;
		var ew = $('#products').width();
		var eh = $('#products').height();
		$('.jqmOverlay').click(function(e){
			//$('#status').html(e.pageX +', '+ e.pageY + ', origin: ' + orX + ', ' + orY + ', DIM: '+(1*orX+ew)+', '+(1*orY+eh));
			if((e.pageX < orX || e.pageX > orX+ew) || (e.pageY < orY || e.pageY > orY+eh)) {
				$('#products .product a').each(function(index) {
					$(this).removeClass("active");
					
/*					rolled = false;
					if(!rolled)*/
						$('.popup').hide();
				});
			}
	   });

	}
	
	var resetOpened = function(hash){
		idname = $('.elemId').attr('value');
		hash.w.hide(0, function(){ hash.o.remove(); }); 
		opened=false;
		$('.info_product a').removeClass('active');
		$('.info_nutrition a').addClass('active');
		$('.jqMiddle_product').hide();
		$('.jqMiddle_nutrition').show();
		$('#' + idname + '_modal').jqmHide(); 
		rolled = false;

	}
	
	function DimImage()
	{
		$('#products_content .product a').each(function(index) {
			
			if(rolled)
			$(this).addClass("active");
			//DimImage($(this));
			//alert($(this).parent().attr('id'));
		});
		idname = $('.elemId').attr('value');
		//alert('#'+idname+'_popup');
		$('#'+idname+'_popup').show();
		//alert( $('#'+idname+'_popup').is(':visible') );

		//elem.addClass("active");
	}

	$('.jqmWindow').click(function(){
		opened = true;
	});


	// switching pane between the product information
	$('.info_nutrition a').click(function(){
		$(".info_nutrition a").addClass("active");
		$(".info_product a").removeClass("active");
		
		$(".jqMiddle_nutrition").show();
		$(".jqMiddle_product").hide();
	});	

	$('.info_product a').click(function(){
		$(".info_nutrition a").removeClass("active");
		$(".info_product a").addClass("active");

		$(".jqMiddle_product").show();
		$(".jqMiddle_nutrition").hide();

	});	

}


$(function(){ 
	expandCollapse();
	productMouse();
});


