jQuery.noConflict();

(function($){
	
	
	$.fn.siteFeatures = function(options){
		var isMethodCall = (typeof options == "string"),
			args = Array.prototype.slice.call(arguments, 1);
			
		if (isMethodCall) {
			var instance = $(this[0]).data("siteFeatures");
			if (instance && $.isFunction(instance[options])) {
				return instance[options].apply(instance, args);
			} else {
				return undefined;
			}
		}
		
		return this.each(function() {
			var instance = $(this).data("siteFeatures");
			if (isMethodCall && instance && $.isFunction(instance[options])) {
				instance[options].apply(instance, args);
			} else if (!instance) {
				$(this).data("siteFeatures", new $.siteFeatures(this, options));
			}
		});
	};
	
	var SiteFeatures = $.siteFeatures = function(element, options) {
		this.element = $(element);
		this.options = $.extend(true, {},$.siteFeatures.defaults, options);
		this.init();
	};
	
	$.extend($.siteFeatures.prototype, {
		init: function() {
			var elem = this.element,
				self = this,
				opt = this.options;
			
			this.navs = $(".nav>li", elem);
			this.current = null;
			this.autoTimeout = 0;
			this.paused = false;
			
			this.navs
				.filter(function(index){
					var link = $("a:first", this);
					if (link.length > 0) {
						var href = $(link).attr("href").replace("#", "");
						var target = (href != "") ? $("#" + href) : undefined;
						
						if (href == "" || target == undefined || target.length < 1) {
							$(this).addClass("nav-disabled");
							return false;
						} else {
							if ($(this).is(".nav-active")) {
								if (self.current == null) {
									self.current = this;
								} else {
									$(this).removeClass("nav-active");
								}
							} else {
								SiteFeatures.hideSlide.call(target, this, false);
							}
							$(this).data("siteFeature_target", target).addClass("nav-tab");
							if ($.browser.msie &&  ($.browser.msie && $.browser.version > 7)) {
								target.children().attr("contenteditable", "true").css("filter","inherit");
							}
							target.css({
								visibility: "visible",
								opacity:0
							});
							return true;
						}
					} else {
						$(this).addClass("nav-disabled");
						return false;
					}
				});
				
			if (this.navs.length < 1) {
				return;
			}
				
			if (this.current == null) {
				this.current = $(this.navs[0]).addClass("nav-active");
			}
			
			
			SiteFeatures.showSlide.call($(this.current).data("siteFeature_target"), this.current, false);
				
			$(this.navs)
				.unbind(".siteFeatures")
				.bind({
					"click.siteFeatures": function(event) {
						if ($(this).is(".nav-active") || $(this).is(".nav-disabled")) {
							return false;
						}
						
						self.clearAutoNext();
						
						var toShow = $(this).data("siteFeature_target");
						var toHide = $(self.current).data("siteFeature_target");
						
						SiteFeatures.hideSlide.call(toHide, self.current);
						SiteFeatures.showSlide.call(toShow, this);
						
						self.current = this;
						
						if (!self.paused) {
							self.setAutoNext(opt.pauseAfterInteraction + opt.nextSlideDelay, opt.nextSlideDelay);
						}
						
						return false;
					}
				});
				
			$(".nav-pause", this.element).bind("click", function(event) {
				self.clearAutoNext();
				self.paused = true;
				$(".nav-pause", this.element).hide();
				$(".nav-resume", this.element).show();
				return false;
			});
			
			$(".nav-resume", this.element).bind("click", function(event) {
				self.setAutoNext(opt.pauseAfterInteraction + opt.nextSlideDelay, opt.nextSlideDelay);
				self.paused = false;
				$(".nav-pause", this.element).show();
				$(".nav-resume", this.element).hide();
				return false;
			});
			
				
			self.setAutoNext(opt.nextSlideDelay, opt.nextSlideDelay);
		},
		
		nextSlide: function(nextTimeout) {
			var idx = this.navs.index(this.current);
			idx ++;
			if (idx > this.navs.length - 1) {
				idx = 0;
			}
			
			var toShow = $(this.navs[idx]).data("siteFeature_target");
			var toHide = $(this.current).data("siteFeature_target");
			
			SiteFeatures.hideSlide.call(toHide, this.current);
			SiteFeatures.showSlide.call(toShow, this.navs[idx]);
			
			this.current = this.navs[idx];
			
			this.setAutoNext(nextTimeout, this.options.nextSlideDelay);
		},
		
		setAutoNext: function(timeout, nextTimeout) {
			var self = this;
			this.clearAutoNext();
			this.autoTimeout = window.setTimeout(function(){
				self.nextSlide(nextTimeout);
			}, timeout)
		},
		
		clearAutoNext: function() {
			if (this.autoTimeout > 0) {
				window.clearTimeout(this.autoTimeout);
				this.autoTimeout = 0;
			}
		}
	});	

	$.extend($.siteFeatures, {
		defaults: {
			nextSlideDelay: 5000,
			pauseAfterInteraction: 5000,
			startUpAuto: true
		},
		hideSlide: function(tab, anim){
			var elem = $(this);
			anim = (anim == undefined) ? true : anim;
			elem.stop();
			$(tab).removeClass("nav-active");
			
			if (!anim) {
				$(elem).css({
					display:"none",
					opacity:""
				});
				return;
			}
			
			
			if (elem.css("display") != "none") {
				elem.animate({opacity:0},{queue:false, duration:600, complete:function(){
					$(this).css({
						display:"none",
						opacity:""
					});
				}});
			}
		},
		showSlide: function(tab, anim) {
			var elem = $(this), opacity, display;
			
			anim = (anim == undefined) ? true : anim;
			$(tab).addClass("nav-active");
			
			opacity = parseFloat(elem.css("opacity") || 1, 10) || 0;
			display = elem.css("display");
			
			elem.stop();
			if (!anim) {
				$(elem).css({
					display:"block",
					opacity:""
				});
				return;
			}
			
			if (display == "none" || opacity < 1) {
				elem.css({
					opacity:(display == "none") ? 0 : opacity,
					display:"block"
				});
				elem.animate({opacity:1},{queue:false, duration:800, complete:function(){
					$(this).css({
						display:"block",
						opacity:""
					});
				}});
			}
		}
	});	
})(jQuery);;

(function($){
	$.fn.tooltip = function(options){
		var isMethodCall = (typeof options == "string"),
			args = Array.prototype.slice.call(arguments, 1);
			
		if (isMethodCall) {
			var instance = $(this[0]).data("tooltip");
			if (instance && $.isFunction(instance[options])) {
				return instance[options].apply(instance, args);
			} else {
				return undefined;
			}
		}
		
		return this.each(function() {
			var instance = $(this).data("tooltip");
			if (isMethodCall && instance && $.isFunction(instance[options])) {
				instance[options].apply(instance, args);
			} else if (!instance) {
				$(this).data("tooltip", new $.tooltip(this, options));
			}
		});
	};
	
	var SiteFeatures = $.tooltip = function(element, options) {
		this.element = $(element);
		this.options = $.extend(true, {},$.tooltip.defaults, options);
		this.init();
	};
	
	$.extend($.tooltip.prototype, {
		init: function() {
			var elem = this.element,
				self = this,
				opt = this.options;
			
			if ($("#tooltip").length < 1) {
				$.tooltip.initControl();
			}
			
			/*
			elem.bind({
				"mouseover": function(event){
					$.tooltip.show.apply($.tooltip, [self, event]);
				},
				"mouseout": function(event){
					$.tooltip.hide.apply($.tooltip, [self, event]);
				},
				"mousemove": function(event) {
					$.tooltip.move.apply($.tooltip, [self, event]);
				}
			});
			*/
			
			elem.bind({
				"click": function(event){
					$.tooltip.show.apply($.tooltip, [self, event]);
					
					$("html").unbind("mousedown.tooltip");
					$("html").bind("mousedown.tooltip", function(e){
						if ($(e.target).closest("#tooltip").length < 1) {
							$("html").unbind("mousedown.tooltip");
							$.tooltip.hide.apply($.tooltip, [self, e]);
							e.stopPropagation();
							e.preventDefault();
							return false;
						}
					});
					
					return false;
				}
			});
		}
	});	

	$.extend($.tooltip, {
		defaults: {
			arrowWidth:50,
			arrowNarrowSpace: 20
		},
		
		initControl: function() {
			var html = "<div id='tooltip'>" +
				"<table>" + 
					"<tr><td class='btl'></td><td class='bt1'></td><td class='bt'></td><td class='bt2'></td><td class='btr'></td></tr>"+
					"<tr><td class='bl'></td><td class='tooltip-content' colspan='3'></td><td class='br'></td></tr>"+
					"<tr><td class='bbl'></td><td class='bb1'></td><td class='bb'></td><td class='bb2'></td><td class='bbr'></td></tr>"+
				"</table></div>";
				
			$("body").append(html);
			
			$.tooltip.control = $("#tooltip");
			$.tooltip.control.hide();
			$.tooltip.current = undefined;
		},
		
		show: function(obj, event) {
			var elem = obj.element;
			var href = elem.attr('href').replace('#', '');
			var content = $("#" + href);
			var pageSize, pageScroll, offset, w, h, l, t, wb1, wb2, wa, sl, a = 1, b = 1;
			
			if (content.length < 1) {
				return;
			}
			
			if ($.tooltip.current != undefined) {
				$.tooltip.current.appendTo( $("body") );
				$.tooltip.current = undefined;
			}
			
			$.tooltip.control.show();
			pageSize = $.tooltip.getPageSize();
			pageScroll = $.tooltip.getPageScroll();
			offset = elem.offset();
			
			$.tooltip.currentPageSize = pageSize;
			$.tooltip.currentPageScroll = pageScroll;
			$.tooltip.currentOffset = offset;
			
			$("td", $.tooltip.control).css({width:""});
			
			$(".tooltip-content", $.tooltip.control).append( content.show() );
			w = $.tooltip.control.outerWidth();
			h = $.tooltip.control.outerHeight();
			
			$.tooltip.currentW = w;
			$.tooltip.currentH = h;
			
			l = event.pageX;
			t = event.pageY - h - 5;
			
			l -= $(".bbl", $.tooltip.control).outerWidth() + obj.options.arrowNarrowSpace + (obj.options.arrowWidth/2);
			
			if ( (l + w) > pageSize.windowWidth ) {
				l = event.pageX - w + ($(".bbl", $.tooltip.control).outerWidth() + obj.options.arrowNarrowSpace + (obj.options.arrowWidth/2));
				a = -1;
			}
			
			w -= $(".bbl", $.tooltip.control).outerWidth();
			w -= $(".bbr", $.tooltip.control).outerWidth();
			
			if ( a > 0 ) {
				wb1 = obj.options.arrowNarrowSpace;
				wb2 = w - obj.options.arrowWidth - wb1;
			} else {
				wb2 = obj.options.arrowNarrowSpace;
				wb1 = w - obj.options.arrowWidth - wb2;
			}
			
			if ((offset.top - pageScroll.yScroll) > (pageSize.windowHeight/2)) {
				b = 1;
				$(".bt", $.tooltip.control).removeClass("bta");
				$(".bb", $.tooltip.control).addClass("bba");
			} else {
				b = 2;
				$(".bt", $.tooltip.control).addClass("bta");
				$(".bb", $.tooltip.control).removeClass("bba");
			}
			
			
			if (b == 1) {
				$(".bb1", $.tooltip.control).css({
					width:wb1+'px'
				});
				
				$(".bb2", $.tooltip.control).css({
					width:wb2+'px'
				});
			} else {
				$(".bt1", $.tooltip.control).css({
					width:wb1+'px'
				});
				
				$(".bt2", $.tooltip.control).css({
					width:wb2+'px'
				});
				t = event.pageY + 5;
			}
			
			$.tooltip.control.css({
				left:l+'px',
				top:t+'px'
			});
			
			$.tooltip.current = content;
		},
		
		hide: function(elem, event) {
			if ($.tooltip.current != undefined) {
				$.tooltip.current.appendTo( $("body") );
				$.tooltip.current.hide();
				$.tooltip.current = undefined;
			}
			$.tooltip.control.hide();
		},
		
		move: function(obj, event) {
			var elem = obj.element,
				pageSize, offset, w, h, l, t, wb1, wb2, a = 1, b = 1, pageScroll;
				
			if ($.tooltip.current != undefined) {
				pageSize = $.tooltip.currentPageSize;
				pageScroll = $.tooltip.currentPageScroll;
				offset = $.tooltip.currentOffset;
				w = $.tooltip.currentW;
				h = $.tooltip.currentH;
				
				l = event.pageX;
				t = event.pageY - h - 5;
				
				l -= $(".bbl", $.tooltip.control).outerWidth() + obj.options.arrowNarrowSpace + (obj.options.arrowWidth/2);
				
				if ( (l + w) > pageSize.windowWidth ) {
					l = event.pageX - w + ($(".bbl", $.tooltip.control).outerWidth() + obj.options.arrowNarrowSpace + (obj.options.arrowWidth/2));
					a = -1;
				}
				
				w -= $(".bbl", $.tooltip.control).outerWidth();
				w -= $(".bbr", $.tooltip.control).outerWidth();
				
				if ( a > 0 ) {
					wb1 = obj.options.arrowNarrowSpace;
					wb2 = w - obj.options.arrowWidth - wb1;
				} else {
					wb2 = obj.options.arrowNarrowSpace;
					wb1 = w - obj.options.arrowWidth - wb2;
				}
				
				if ((offset.top - pageScroll.yScroll) > (pageSize.windowHeight/2)) {
					b = 1;
					$(".bt", $.tooltip.control).removeClass("bta");
					$(".bb", $.tooltip.control).addClass("bba");
				} else {
					b = 2;
					$(".bt", $.tooltip.control).addClass("bta");
					$(".bb", $.tooltip.control).removeClass("bba");
				}
				
				
				if (b == 1) {
					$(".bb1", $.tooltip.control).css({
						width:wb1+'px'
					});
					
					$(".bb2", $.tooltip.control).css({
						width:wb2+'px'
					});
				} else {
					$(".bt1", $.tooltip.control).css({
						width:wb1+'px'
					});
					
					$(".bt2", $.tooltip.control).css({
						width:wb2+'px'
					});
					t = event.pageY + 5;
				}
				
				$.tooltip.control.css({
					left:l+'px',
					top:t+'px'
				});
			}
		},
		
		getPageSize: function() {
			var xScroll, yScroll;

			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}

			var windowWidth, windowHeight;

			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	

			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else {
				pageHeight = yScroll;
			}


			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			
			//
			var largestWidth;
			var largestHeight;
			var smallestWidth;
			var smallestHeight;
			//
			if ( pageWidth >= windowWidth )
			{	largestWidth = pageWidth; smallestWidth = windowWidth;	}
			else
			{	largestWidth = windowWidth; smallestWidth = pageWidth;	}
			//
			if ( pageHeight >= windowHeight )
			{	largestHeight = pageHeight; smallestHeight = windowHeight;	}
			else
			{	largestHeight = windowHeight; smallestHeight = pageHeight;	}
			
			// Return
			var arrayPageSize = {'pageWidth':pageWidth,'pageHeight':pageHeight,'windowWidth':windowWidth,'windowHeight':windowHeight,'largestWidth':largestWidth,'largestHeight':largestHeight};
			return arrayPageSize;
		},
		
		getPageScroll: function ( ) {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			var arrayPageScroll = {'xScroll':xScroll,'yScroll':yScroll};
			return arrayPageScroll;
		}
	});	
})(jQuery);;

//
(function($){
	$.fn.msgbox = function(options){
		var isMethodCall = (typeof options == "string"),
			args = Array.prototype.slice.call(arguments, 1);
			
		if (isMethodCall) {
			var instance = $(this[0]).data("msgbox");
			if (instance && $.isFunction(instance[options])) {
				return instance[options].apply(instance, args);
			} else {
				return undefined;
			}
		}
		
		return this.each(function() {
			var instance = $(this).data("msgbox");
			if (isMethodCall && instance && $.isFunction(instance[options])) {
				instance[options].apply(instance, args);
			} else if (!instance) {
				$(this).data("msgbox", new $.msgbox(this, options));
			}
		});
	};
	
	$.msgbox = function(element, options) {
		//
	};
	
	$.extend($.msgbox, {
		initControl: function() {
			var html = "<div id='msgbox'>" +
				"<div class='msgbox-content'></div>" +
				"<span class='btl'></span><span class='bt'></span><span class='btr'></span>" +
				"<span class='bl'></span><span class='br'></span>" +
				"<span class='bbl'></span><span class='bb'></span><span class='bbr'></span>" +
				"</div>";
				
			$("body").append(html);
			
			$.msgbox.control = $("#msgbox");
			$.msgbox.content = ($(".msgbox-content", $.msgbox.control));
			$.msgbox.control.hide();
			$.msgbox.current = undefined;
		},
		
		show: function(html, event) {
			var pageSize, pageScroll, w, h, x, y;
			
			if (typeof html == "string") {
				$.msgbox.content.html(html);
			} else if (html.length > 0) {
				$.msgbox.content.append(html);
			} else {
				return;
			}
			
			
			$.msgbox.control.show();
			pageSize = $.msgbox.getPageSize();
			pageScroll = $.msgbox.getPageScroll();
			
			w = $.msgbox.control.outerWidth();
			h = $.msgbox.control.outerHeight();
			
			$.msgbox.currentW = w;
			$.msgbox.currentH = h;
			
			$(".bl", $.msgbox.control).css({
				height: (h - 60),
				top: 30
			});
			$(".br", $.msgbox.control).css({
				height: (h - 60),
				top: 30
			});
			$(".bt", $.msgbox.control).css({
				width: (w - 60),
				left: 30
			});
			$(".bb", $.msgbox.control).css({
				width: (w - 60),
				left: 30
			});
			
			x = (pageSize.windowWidth - w) / 2;
			y = parseInt(window.scrollY) + parseInt(((pageSize.windowHeight - h) / 2));
			console.log(window.scrollY, (pageSize.windowHeight - h) / 2);
			
			$.msgbox.control.css({
				left:x+'px',
				top:y+'px'
			});
			
			$.msgbox.current = html;
			
			
			$("html").unbind(".msgbox")
				.bind("mousedown.msgbox", function(e){
					if ($(e.target).closest("#msgbox").length < 1) {
						$("html").unbind("mousedown.msgbox");
						$.msgbox.hide.apply($.msgbox, [self, e]);
						e.stopPropagation();
						e.preventDefault();
						return false;
					}
				});
		},
		
		hide: function(elem, event) {
			$.msgbox.control.hide();
			$.msgbox.content.html("");
		},
		
		getPageSize: function() {
			var xScroll, yScroll;

			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}

			var windowWidth, windowHeight;

			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	

			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else {
				pageHeight = yScroll;
			}


			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			
			//
			var largestWidth;
			var largestHeight;
			var smallestWidth;
			var smallestHeight;
			//
			if ( pageWidth >= windowWidth )
			{	largestWidth = pageWidth; smallestWidth = windowWidth;	}
			else
			{	largestWidth = windowWidth; smallestWidth = pageWidth;	}
			//
			if ( pageHeight >= windowHeight )
			{	largestHeight = pageHeight; smallestHeight = windowHeight;	}
			else
			{	largestHeight = windowHeight; smallestHeight = pageHeight;	}
			
			// Return
			var arrayPageSize = {'pageWidth':pageWidth,'pageHeight':pageHeight,'windowWidth':windowWidth,'windowHeight':windowHeight,'largestWidth':largestWidth,'largestHeight':largestHeight};
			return arrayPageSize;
		},
		
		getPageScroll: function ( ) {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			var arrayPageScroll = {'xScroll':xScroll,'yScroll':yScroll};
			return arrayPageScroll;
		}
	});
	
	$(function(){
		$.msgbox.initControl();
	});
})(jQuery);;

function msgbox(html, autoHide) {
	jQuery.msgbox.show(html);
	
	if (typeof autoHide !== "undefined") {
		window.setTimeout(function() {
			jQuery.msgbox.hide();
		}, autoHide);
	}
}

(function($){
	function init_main_nav() {
		var num = function(el, props) {
			var r = 0;
			$.each(props.split(/\s+/) || [], function(i,v){
				r += parseInt($(el).css(v)) || 0;
			});
			return r;
		}
		
		function initSubmenu(subMenu, li) {
			var offset = li.offset(),
				w = li.outerWidth(),
				h = li.outerHeight(),
				ul,
				bl, br, bbl, bbr, btr, bb, bt, btir,
				ibbl, ibbr, ibtl, ibtr, ibl, ibr, ibb, ibt,
				R, L, W, H,
				pl, pr, pb, pt, ml, mr, mb, bt, table, p, po;
			
			subMenu.prepend(
				/*
				bl = $("<span class='bl'></span>").show(),
				br = $("<span class='br'></span>").show(),
				bbl = $("<span class='bbl'></span>").show(),
				bbr = $("<span class='bbr'></span>").show(),
				bb = $("<span class='bb'></span>").show(),
				*/
				bt = $("<span class='bt'></span>").show(),
				btr = $("<span class='btr'></span>").show(),
				btir = $("<span class='btir'></span>").show(),
				bttr = $("<span class='bttr'></span>").show(),
				bttl = $("<span class='bttl'></span>").show(),
				ibbl = $("<span class='ibbl'></span>").show(),
				ibbr = $("<span class='ibbr'></span>").show(),
				ibtl = $("<span class='ibtl'></span>").show(),
				ibtr = $("<span class='ibtr'></span>").show(),
				ibl = $("<span class='ibl'></span>").show(),
				ibr = $("<span class='ibr'></span>").show(),
				ibt = $("<span class='ibt'></span>").show(),
				ibb = $("<span class='ibb'></span>").show()
			);
			
			ul = $(".page-subnav", subMenu);
			
			table = $("<table>"+
				"<tr><td class='bl'></td><td class='bm'></td><td class='br'></tr>" +
				"<tr><td class='bbl'></td><td class='bb'></td><td class='bbr'></tr>" +
				"</table>");
				
			table.prependTo(subMenu);
			ul.appendTo(  $("td.bm", table) );
			
			
			pl = parseInt(subMenu.css("paddingLeft") || 0, 10) || 0;
			pr = parseInt(subMenu.css("paddingRight") || 0, 10) || 0;
			pt = parseInt(subMenu.css("paddingTop") || 0, 10) || 0;
			pb = parseInt(subMenu.css("paddingBottom") || 0, 10) || 0;
			ml = parseInt(subMenu.css("marginLeft") || 0, 10) || 0;
			mr = parseInt(subMenu.css("marginRight") || 0, 10) || 0;
			mt = parseInt(subMenu.css("marginTop") || 0, 10) || 0;
			mb = parseInt(subMenu.css("marginBottom") || 0, 10) || 0;
			
			//L = offset.left - bl.outerWidth(true) - pl;
			
			p = li.parent().parent();
			po = p.offset();
			W = p.width() - (offset.left - po.left);
			w = ul.width();
			if (W > 0) {
				if ((ul.outerWidth() + 20) > W) {
					w = W - 19;
				}
			}
			
			L = offset.left - 15 - pl;
			H = ul.outerHeight() + pt + pb;
			W = ul.outerWidth() + pl + pr;
			
			ul.css("width", w);
			
			W = ul.outerWidth() + pl + pr;
			w = li.outerWidth()
			/*
			bl.css({
				//top:-h+"px",
				top:(btr.outerHeight()-9)+"px",
				left:-9,
				//height:(H + h - 6)+"px"
				height:(H - 12)+"px"
			});
			*/
			/*
			bbl.css({
				bottom:-9,
				left:-9
			});
			*/
			
			/*
			br.css({
				top:(btr.outerHeight()-9)+"px",
				right:-9,
				height:(H - 12)+"px"
			});
			*/
			/*
			bbr.css({
				bottom:-9,
				right:-9
			});
			*/
			/*
			bb.css({
				width: (W - 12)+"px",
				left: (bbl.outerWidth()-9)+"px",
				bottom:-9
			});
			*/
			
			btr.css({
				right:13,
				top:-9
			});
			bt.css({
				top:-9,
				left:(w+9),
				width:(W-w-15)
			});
			btir.css({
				top:-9,
				left:w-6
			});
			bttr.css({
				top:-h,
				left:w-6,
				height:h-9
			});
			bttl.css({
				top:-h+"px",
				left:-9,
				height:h+6
			});
			
			
			ibtl.css({
				top:-h,
				left:pl
			});
			ibtr.css({
				top:-h,
				left:(w-ibtr.outerWidth()-pr)
			});
			ibbl.css({
				top:0+2,
				left:pl
			});
			ibbr.css({
				top:0+2,
				left:(w-ibbr.outerWidth()-pr)
			});
			ibl.css({
				left:pl,
				top:-h+ibtl.outerHeight(),
				height:h-ibtl.outerHeight()+2
			});
			ibr.css({
				left:(w-ibtr.outerWidth()-pr),
				top:-h+ibtl.outerHeight(),
				height:h-ibtl.outerHeight()+2
			});
			ibt.css({
				left:ibtl.outerWidth()+pl,
				top:-h,
				width:w-(ibtl.outerWidth() + ibtr.outerWidth()+4)
			});
			ibb.css({
				left:(w-2),
				top:2,
				width:(W-w)
			});
			
			if ($(".page-subnav-flatten", subMenu).length > 0) {
				bt.remove();
				btir.remove();
				btr.remove();
				bttr.css({
					height:h
				});
				
				ul.css({
					width:w - 4
				});
			}
			
			/****/
			//expand / collapse submenu
			$(">li>ul", ul).each(function(){
				var submenu = $(this);
				var parent = $(this).parent();
				var a = $("a:eq(0)", parent);
				
				if (!parent.is(".active")) {
					a.bind("click", function(){
						//o.slideToggle();
						$(this).parent().parent().find("li.active").each(function (){
							$(this).toggleClass("active");
							$(this).find("ul").slideUp();
							$(this).find(">a").blur();
						})
					
						submenu.slideToggle();
						parent.toggleClass("active");
						this.blur();
						return false;
					});
					submenu.hide();
				} else {
					a.bind("click", function(){
						this.blur();
						return false;
					});
				}
			});
			
			/****/
			subMenu.css({
				left: offset.left+"px",
				top: (offset.top + h)+"px",
				visibility:"visible",
				display:"none"
			});
		};
		
		function showSubmenu(subMenu, li) {
			var offset = li.offset(),
				h = li.outerHeight();
				
			h = li.outerHeight();
				
			subMenu.css({
				left: offset.left+"px",
				top: (offset.top + h)+"px",
				visibility:"visible",
				display:"block"
			});
		}
		
		function hideSubmenu(subMenu, li) {
			subMenu.hide();
		}
		
		function init_nav() {
			var elem = $(this),
				siblings = elem.siblings(),
				hasSubmenu = ($(".page-subnav", elem).length > 0),
				subMenu = null, hideTimeout = 0;
			
			var clearHideTimeout = function() {
				if (hideTimeout > 0) {
					window.clearTimeout(hideTimeout);
					hideTimeout = 0;
				}
			};
			
			var startHideTimeout = function() {
				clearHideTimeout();
				hideTimeout = window.setTimeout(function(){
					elem.trigger("hideMenu.pagenav");
				}, 200);
			};
			
			if (hasSubmenu) {
				subMenu = $("<div />").addClass("nav-submenu").append($(".page-subnav", elem)).appendTo("body");
				subMenu.show();
				initSubmenu(subMenu, elem);
				
				//subMenu.hide();
				hideSubmenu(subMenu);
			}
			
			elem.bind({
				"mouseover.pagenav": function(event) {
					$(this).trigger("showMenu.pagenav");
				},
				"mouseout.pagenav": function(event) {
					startHideTimeout();
				},
				"showMenu.pagenav": function() {
					clearHideTimeout();
					siblings.trigger("hideMenu.pagenav");
					elem.addClass("active");
					if (subMenu && subMenu.length > 0) {
						//subMenu.show();
						showSubmenu(subMenu, elem);
					}
				},
				"hideMenu.pagenav": function() {
					elem.removeClass("active");
					if (subMenu && subMenu.length > 0) {
						//subMenu.hide();
						hideSubmenu(subMenu);
					}
				}
			});
			
			if (hasSubmenu) {
				elem.addClass("nav-hasSubmenu");
				
				$(subMenu).bind("mouseover.pagenav", function(){
					elem.trigger("mouseover.pagenav");
					clearHideTimeout();
				});
				$(subMenu).bind("mouseout.pagenav", function(){
					elem.trigger("mouseout.pagenav");
					startHideTimeout();
				});
			}
			
			
		};
		
		
		$(".page-nav").each(function(){
			var ul = $(">ul",this),
				li = $(">ul>li",this),
				availWidth = 0, usedWidth = 0, space, d;
			
			li.filter(":first").addClass("first").end().filter(":last").addClass("last");
			li.each(function() {
				usedWidth += $(this).outerWidth(true);
			});
			
			availWidth = ul.width();
			space = parseInt(availWidth - usedWidth,10) || 0;
			while (space != 0) {
				li.each(function() {
					var a = $("a:first>span", this);
					var p = parseInt(a.css("marginRight") || 0, 10) || 0;
					if (space > 0) {
						p++;
						space--;
					} else if (space < 0) {
						p--;
						space++;
					}
					a.css("marginRight",p+'px');
					
					if (space == 0) {
						return false;
					}
				});
			};
			
			li.each(function(){
				init_nav.call(this);
			});
		});
	};
	
	function init_left_nav() {
		$(".nav-left").each(function(){
			var el = $(this);
			var ul = $("ul:eq(0)", el);
			
			$("ul:first>li:first",el).addClass("first");
			
			$(">li>ul", ul).each(function(){
				var o = $(this);
				var li = $(this).closest("li");
				var a = $("a:eq(0)", li);
				if (!li.is(".active")) {
					
					a.bind("click", function(){
						//o.slideToggle();
						$(this).parent().parent().find("li.active").each(function (){
							$(this).toggleClass("active");
							$(this).find("ul").slideUp();
							$(this).find(">a").blur();
						})
						li.find("ul").slideToggle();
						li.toggleClass("active");

						this.blur();
						return false;
					});
				} else {
					a.bind("click", function(){
						this.blur();
						return false;
					});
				}
			});
		});
	};
	
	function make_box() {
		var elem = $(this),
			bT = $(".bT", elem), bB = $(".bB", elem),
			bL = $(".bL", elem), bR = $(".bR", elem),
			bTL = $(".bTL", elem), bBL = $(".bBL", elem),
			bTR = $(".bTR", elem), bBR = $(".bBR", elem),
			w, h,
			borders = {
				bT: $(".bT", elem),
				bB: $(".bB", elem),
				bL: $(".bL", elem),
				bR: $(".bR", elem),
				bTL: $(".bTL", elem),
				bTR: $(".bTR", elem),
				bBL: $(".bBL", elem),
				bBR: $(".bBR", elem)
			};
			
		w = elem.innerWidth();
		h = elem.innerHeight();
		
		$.each(borders, function(k, v){
			if (v.length < 1) {
				borders[k] = $("<span />").addClass(k).appendTo(elem);
			}
		});
		
		borders.bT.css({
			width:(w - borders.bTL.outerWidth() - borders.bTR.outerWidth())+'px'
		});
		borders.bB.css({
			width:(w - borders.bBL.outerWidth() - borders.bBR.outerWidth())+'px'
		});
		borders.bL.css({
			height:(h - borders.bTL.outerHeight() - borders.bBL.outerHeight())+'px'
		});
		borders.bR.css({
			height:(h - borders.bTR.outerHeight() - borders.bBR.outerHeight())+'px'
		});
		
		
	};
	
	init_footer_links = function() {
		$(".footer_links").each(function(){
			var el = $(this);
			
			$(">li:first", el).addClass("first");
			
			var h=0, maxH=0;
			$(">li",el).each(function(){
				h = $(this).height();
				if (h > maxH) {
					maxH = h;
				}
			});
			
			$(">li",el).each(function(){
				$(this).css("height", maxH);
			});
		});
	};
	
	init_text_resize = function(normal, big, bigger) {
		$(".util-textSize").each(function(){
			var p = $(this).closest(".grid-midcontent, .grid-full");
			var initialTextSize = parseInt(p.css("font-size"), 10) || 0;
			var normalSize = normal * initialTextSize;
			var bigSize = big * initialTextSize;
			var biggerSize = bigger * initialTextSize;
			
			$(".textSize-normal:eq(0)", this).bind("click", function(){
				p.css("font-size", normalSize+"px");
				return false;
			});
			$(".textSize-big:eq(0)", this).bind("click", function(){
				p.css("font-size", bigSize+"px");
				return false;
			});
			$(".textSize-bigger:eq(0)", this).bind("click", function(){
				p.css("font-size", biggerSize+"px");
				return false;
			});
		});
	};
	
	/* DOM READY */
	$(function(){
		//add grid-widecontent class to grid-midcontent class if there is no grid-rightcol next to it
		$(".grid-layout>.grid-midcontent").each(function(){
			if ($(this).next().length < 1) {
				$(this).addClass("grid-widecontent");
			}
		});
		
		//skin tools
		$(".productsList").each(function(){
			$(".product", this).filter(":first").addClass("product-first");
			if (this.scrollHeight > $(this).height()) {
				$(".product", this).filter(":last").addClass("product-last");
			}
		});	
		
		$(".box-right").each(function(){
			make_box.call(this);
		});
		
		//grid view
		$(".productsListGridView").each(function(){
			$("table:first tr:first>td", this).css("borderTopWidth",0);
			if ($("table:first", this).outerHeight() > $(this).height()) {
				$("table:first tr:last>td", this).css("borderBottomWidth",0);
			}
			$("table:first tr", this).each(function(){
				$("td:first", this).css("borderLeftWidth",0);
				$("td:last", this).css("borderRightWidth",0);
			});
		});
		
		//footer links
		init_footer_links();
		
		//main nav
		init_main_nav();
		
		//left nav
		init_left_nav();
		
		//init text resize util
		//parameter: normal size scale, big size scale, bigger size scale
		init_text_resize(1, 1.1, 1.2);
		
		//tooltip
		$(".tooltip").tooltip();
		
		//ie 7 fix
		if ($.browser.msie && $.browser.version < 8) {
			(function(){
				var scrollbarWidth = function() {
					var div = $('<div style="width:100px;height:100px;overflow:hidden;position:absolute;top:-300px;left:-300px;"><div style="height:200px;"></div>');
					$('body').append(div);
					var w1 = $('div', div).innerWidth();
					div.css('overflow', 'auto');
					var w2 = $('div', div).innerWidth();
					div.remove();
					return Math.max(w1 - w2, 17);
				}.call();
			
				$(".productsListGridView table").each(function(){
					var p = $(this).parent(),
						w = p.width();
						
					if (p[0].scrollHeight > p[0].offsetHeight) {
						$(this).css("width", w-scrollbarWidth);
					} else {
					}
				});
			})();
		}
		
		//site features
		$(".siteFeatures").siteFeatures({
			nextSlideDelay: 7000,	//delay between each slide
			pauseAfterInteraction: 7000,	//added delay after user interaction
			startUpAuto: true
		});
		
		//product review
		$(".productReviews-reviews").each(function(){
			$(".productReviews-review:even", this).addClass("productReviews-review-alt");
		});
		
		//tabs
		$('.tabs').tabs();
		
		//if (!$("body").is(".checkout-cart-index")) {
			//msgbox("Product Name has been added to Compare", 3000);
		//}
		
		if (!$("body").is(".checkout-cart-index")) {
			msgbox($(".messages"));
		}
		
		$('.default-value').each(function() {
			var default_value = this.value;
			$(this).focus(function() {
				if(this.value == default_value) {
					this.value = '';
				}
			});
			$(this).blur(function() {
				if(this.value == '') {
					this.value = default_value;
				}
			});
		});
	});
})(jQuery);


	function getChecked() {
		msgbox('Loading product comparison...')
	var checked = jQuery("input:checkbox[name=compare]:checked");
	//console.log(checked);
	if (checked.length > 0) {
		var prodToCompare = new Array; 
			for (var i = checked.length - 1; i >= 0; i-- ) {
				prodToCompare[i] = checked[i].value; //new json object
					
			}
				//console.log(jQuery("input:checkbox[name=compare]:checked"));
				//console.log(prodToCompare);
				jQuery.post('/compareproducts/index/addmany', {products: prodToCompare} ,function(data) {
				 	window.location = '/catalog/product_compare/index/';
				});
		}
	}
function bindTopCart() {
    Enterprise.TopCart.initialize('topCartContent');
  	jQuery(".top-cart .block-title > a").click(function(e) {
  		e.preventDefault();
  	});
}



jQuery(document).ready(function($) {


		bindTopCart();
  	$("#privacy").click(function (e) {
  	
  		e.preventDefault();
  		privacy = window.open("/privacy", "mywindow", "location=1,status=1,scrollbars=1,  width=820,height=600");
		privacy.scrollTo(200,150);
		
  	
  	});
  	
  	$(".productsList-paging select").change(function(e) {
			window.location = jQuery(".productsList-paging select option:selected").attr('data-url');
//  		window.location = jQuery(e.target).attr('data-url');
  	
  	});
  	

  
});

function testvalidate()
{
	var productAddToCartForm = new VarienForm('product_addtocart_form');

	if (productAddToCartForm.validator.validate()) {
		productAddToCartForm.form.submit();
	}
}




