$(document).ready(function () {


  // fix css background pngs in IE6
  var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
  var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
  if (jQuery.browser.msie && (ie55 || ie6)) {

    // select all img elements with pngs and add pngfix class
    $("img[src$=.png]").each(function () {
      $(this).addClass("pngfix");
    });
    // select all elements with png bg and add pngfix class
    $("*").each(function () {
      //console.log(this + "--");
      if (!$(this).hasClass('overlay')) {
        var bgIMG = jQuery(this).css('background-image');
        if (bgIMG.indexOf(".png") != -1) {
          $(this).addClass("pngfix");
        }
      }
    });
    // run the png fix
    DD_belatedPNG.fix('.pngfix');
  }
  voFunctions.menuShow.init();
  voFunctions.dotSlider.init();

  ovr = $('.overlay_wrapper').overlay({
    mask: {
      color: '#fff',
      opacity: 0.8
    }
  });

//  $('.price_guide a[title]').tooltip({
//    events: {
//      def: "click, mouseout"
//    }
//  });


});


var voFunctions = {
	menuShow: {
		init: function () {
			$('.menu ul').hide();
			this.siteWidth = $('.container').width();
			this.showSub();
			this.attachEvents();
		},
		siteWidth: 0,
		showSub: function () {
			$('.menu li.active a + .submenu').show();
		},
		attachEvents: function () {
			$('.menu > li:not(.active)').bind('mouseenter.mainnav', function () {
				$(this).children('a').addClass('over');
				var submenu = $(this).children('ul');
				if (submenu.length != 0) {
					$('.menu > li.active ul').hide();
					submenu.show();
					//console.log(submenu);
					var pgoffset = $('.container').offset();
					var suboffset = submenu.offset();
					var subWidth = 0;
					submenu.children('li').each( function () {
						subWidth = subWidth + $(this).width();
					});
					var diff = suboffset.left - pgoffset.left + subWidth;
					//console.log("contoffset: " + pgoffset.left + ", suboff: " + suboffset.left + ", subW: " + subWidth + ", diff: " + diff + ", siteW: " + voFunctions.menuShow.siteWidth);
					if (diff > voFunctions.menuShow.siteWidth) {
						lt = parseInt(submenu.css('left'));
						var adjust = lt - diff + voFunctions.menuShow.siteWidth - 40;
						submenu.css({
							'paddingLeft': adjust
						});
						//console.log(adjust);
					}
				}
				 
			});
			$('.menu > li:not(.active)').bind('mouseleave.mainnav', function () {
				$(this).children('a').removeClass('over');
				$(this).children('ul').hide();	
				$('.menu > li.active ul').show();				
			});
		}
	},
	
	dotSlider: {
		count: 0,
		current: 0,
		timer: {},
		init: function () {
			$('.hero_cont img').filter( function (index) {
				return index > 0;
			}).hide();
			this.count = $('.hero_cont img').length;
			this.createDots();
			this.attachEvent();
			this.loop();
		},
		createDots: function () {
			for (var i = 0; i < voFunctions.dotSlider.count; ++i) {
				$('<a></a>').attr('href', '#').data('count', i).appendTo('.dots');
			}
			$('.dots a:first').addClass('active');
			$('.hero_cont img:first').addClass('active');
		},
		attachEvent: function () {
			$('.dots a').click( function (evt) {
				evt.preventDefault();
				if (!$(this).hasClass('active')) {
					clearInterval(voFunctions.dotSlider.timer);
					var cnt = $(this).data('count');
					$('.dots a.active').removeClass('active');
					$(this).addClass('active');
					$('.hero_cont img.active').removeClass('active').fadeOut(400, function () {
						$('.hero_cont img').eq(cnt).addClass('active').fadeIn();
					});
					voFunctions.dotSlider.current = cnt;
					voFunctions.dotSlider.loop();
					//console.log("click: " + cnt + ", " + dotSlider.current + ", " + dotSlider.count);
				}
				//console.log($(this).data('count'));
			});
		},
		loop: function () {
			this.timer = setInterval('voFunctions.dotSlider.imgRotate()', 10000);
		},
		imgRotate: function () {
			++this.current;
			var cnt = this.current;
			if (cnt < this.count) {
				$('.dots a.active').removeClass('active');
				$('.hero_cont img.active').removeClass('active').fadeOut(400, function () {
					$('.dots a').eq(cnt).addClass('active');
					$('.hero_cont img').eq(cnt).addClass('active').fadeIn();
				});
				//console.log("auto: " + cnt + ", " + this.current + ", " + this.count);
			} else {
				$('.dots a.active').removeClass('active');
				$('.hero_cont img.active').removeClass('active').fadeOut(400, function () {
					$('.dots a').eq(0).addClass('active');
					$('.hero_cont img').eq(0).addClass('active').fadeIn();
				});
				this.current = 0;
				//console.log("auto wrap: " + cnt + ", " + this.current + ", " + this.count);
			}
		}
	}
}
