//tooltop
$(function () {
  $('.bubbleInfo').each(function () {
    // options
    var distance = 20;
    var time = 220;
    var hideDelay = 150;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popup.css({
          top: -20,
          left: -33,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });
});


//center an element

$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}

//popup div

$(document).ready(function() {

    //select all the a tag with name equal to modalwindow
    $('.open').click(function() {

        //grab rel to see what popup to open
		var popupId = $(this).attr('rel');
		//alert(popupId);

		$('#lightbox'+popupId).centerInClient();

        //show popup
		$('#overlay').show();
        $('#lightbox'+popupId).show();
		return false;
	
    });

	//select all the a tag with name equal to modalwindow
	$('.hd_open').click(function() {
		//grab rel to see what popup to open
		var theHDimageUrl = $('#lightbox2_img').val();
		var theHDImageElement  = '<img id="bigbox" name="bigbox" src="'+theHDimageUrl+'" />';
		$('#hdLightboxImg').html(theHDImageElement);
		$('#hdlightbox').centerInClient();
		//show popup
		setTimeout(function() {
			$('#overlay').show();
			$('#hdlightbox').show();
		
			$('#hdlightbox').centerInClient();
		}, 700);
		setTimeout(function() {
			$('#hdlightbox').centerInClient();
		}, 1000);
		return false;
	});

    //if close button is clicked
    $('.close').click(function () {
       //hide popup
        $(this).parents('.popup_container').hide();
		$('#overlay').hide();
		return false;
	
    });

    //if overlay is clicked
    $('#overlay').click(function () {
        $(this).hide();
        $('.popup_container').hide();
		return false;
    });
});

//center x function

jQuery.fn.centerX = function(popup_Id) {
	return this.each(function() {
	$('#'+popup_Id).css('left', winW/2-$('#'+popup_Id).width()/2);
	});
	return false;
};

$(document).ready(function()
{
	//rel key open =0 closed =1
	var allclosed = 1;
	var Isclosed = 1;
	var inProgress = 0;
	var open_text = $('#term_openall').val();
	var close_text = $('#term_closeall').val();
	$('#default_open').slideToggle(300).siblings("div.menu_main").slideUp("slow");
	$('#default_open').attr('rel', '0');

	$(".left_menu_link").click(function()
	{
		
		window.location = $(this).attr("href"); 
		return false;
	});
	//slide element with class "menu_main" when p with class "menu_headerer" is clicked 
	$(".menu_header").click(function()
    {
				$("#menuWrap").find("span").each(function(){
					$(this).removeClass('open');
				});
				Isclosed = $(this).attr('rel');
				if (Isclosed == 1)
				{
					$(this).children('span').addClass('open');
					$("#menuWrap").find("p").each(function(){
						$(this).attr('rel', '1');
					});
					$(this).attr('rel', '0');
					$(this).next("div.menu_main").slideDown(300).siblings("div.menu_main").slideUp("slow");
					$("#open_all").text(open_text);
					allclosed = 1;
				}else if (Isclosed == 0)
				{
					$(this).children('span').removeClass('open');
					$("#menuWrap").find("p").each(function(){
						$(this).attr('rel', '0');
					});
					$(this).attr('rel', '1');
					$(this).next("div.menu_main").slideUp(300).siblings("div.menu_main").slideUp("slow");
					$("#open_all").text(open_text);
					allclosed = 1;
				}

	});
	$("#open_all").click(function()
	{


			if (allclosed == 1)
			{
				$("#menuWrap").find("p").each(function(){
					$(this).attr('rel', '0');
				});
				$("#menuWrap").find("span").each(function(){
					$(this).addClass('open');
				});
				$("#menuWrap").find(".menu_main").each(function(){
					$(this).slideDown(300);
				});
				$("#open_all").text(close_text);
				allclosed = 0;
			}else{
				$("#menuWrap").find("p").each(function(){
					$(this).attr('rel', '1');
				});
				$("#menuWrap").find("span").each(function(){
					$(this).removeClass('open');
				});
				$("#menuWrap").find(".menu_main").each(function(){
					$(this).slideUp("slow");
				});
				$("#open_all").text(open_text);
				allclosed = 1;
			}


	return false;
	});
});

//jquery_pngfix
/**
 * jQuery (PNG Fix) v1.2
 * Microsoft Internet Explorer 24bit PNG Fix
 *
 * The MIT License
 * 
 * Copyright (c) 2007 Paul Campbell (pauljamescampbell.co.uk)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @param		Object
 * @return		Array
 */
(function($) {
	
	$.fn.pngfix = function(options) {
		
		// Review the Microsoft IE developer library for AlphaImageLoader reference 
		// http://msdn2.microsoft.com/en-us/library/ms532969(VS.85).aspx
		
		// ECMA scope fix
		var elements 	= this;
		var settings 	= $.extend({
			imageFixSrc: 	false,
			sizingMethod: 	false 
		}, options);
		
		if(!$.browser.msie || ($.browser.msie &&  $.browser.version >= 7)) {
			return(elements);
		}

		function setFilter(el, path, mode) {
			var fs = el.attr("filters");
			var alpha = "DXImageTransform.Microsoft.AlphaImageLoader";
			if (fs[alpha]) {
				fs[alpha].enabled = true;
				fs[alpha].src = path; 
				fs[alpha].sizingMethod = mode;
			} else {
				el.css("filter", 'progid:' + alpha + '(enabled="true", sizingMethod="' + mode + '", src="' + path + '")');			
			}
		}
		
		function setDOMElementWidth(el) {
			if(el.css("width") == "auto" & el.css("height") == "auto") {
				el.css("width", el.attr("offsetWidth") + "px");
			}
		}

		return(
			elements.each(function() {
				
				// Scope
				var el = $(this);
				
				if(el.attr("tagName").toUpperCase() == "IMG" && (/\.png/i).test(el.attr("src"))) {
					if(!settings.imageFixSrc) {
						
						// Wrap the <img> in a <span> then apply style/filters, 
						// removing the <img> tag from the final render 
						el.wrap("<span></span>");
						var par = el.parent();
						par.css({
							height: 	el.height(),
							width: 		el.width(),
							display: 	"inline-block"
						});
						setFilter(par, el.attr("src"), "scale");
						el.remove();
					} else if((/\.gif/i).test(settings.imageFixSrc)) {
						
						// Replace the current image with a transparent GIF
						// and apply the filter to the background of the 
						// <img> tag (not the preferred route)
						setDOMElementWidth(el);
						setFilter(el, el.attr("src"), "image");
						el.attr("src", settings.imageFixSrc);
					}
					
				} else {
					var bg = new String(el.css("backgroundImage"));
					var matches = bg.match(/^url\("(.*)"\)$/);
					if(matches && matches.length) {
						
						// Elements with a PNG as a backgroundImage have the
						// filter applied with a sizing method relevant to the 
						// background repeat type
						setDOMElementWidth(el);
						el.css("backgroundImage", "none");
						
						// Restrict scaling methods to valid MSDN defintions (or one custom)
						var sc = "crop";
						if(settings.sizingMethod) {
							sc = settings.sizingMethod;
						} 
						setFilter(el, matches[1], sc);
						
						// Fix IE peek-a-boo bug for internal links
						// within that DOM element
						el.find("a").each(function() {
							$(this).css("position", "relative");
						});
					}
				}
				
			})
		);
	}

})(jQuery)


			//png fix
			$('.fixpng').each( function() {
				$(this).pngfix();
			});
			$('img').pngfix();
			// $.miseAlphaImageLoader("sdsd");

