
function ChangeCheckBoxState(id, checkState) {
    var cb = document.getElementById(id);
    if (cb != null)
        cb.checked = checkState;
}

function ChangeAllCheckBoxStates(checkState) {
    // Toggles through all of the checkboxes defined in the CheckBoxIDs array
    // and updates their value to the checkState input parameter
    if (CheckBoxIDs != null) {
        for (var i = 0; i < CheckBoxIDs.length; i++)
            ChangeCheckBoxState(CheckBoxIDs[i], checkState);
    }
}

function ChangeHeaderAsNeeded() {
    // Whenever a checkbox in the GridView is toggled, we need to
    // check the Header checkbox if ALL of the GridView checkboxes are
    // checked, and uncheck it otherwise
    if (CheckBoxIDs != null) {
        // check to see if all other checkboxes are checked
        for (var i = 1; i < CheckBoxIDs.length; i++) {
            var cb = document.getElementById(CheckBoxIDs[i]);
            if (!cb.checked) {
                // Whoops, there is an unchecked checkbox, make sure
                // that the header checkbox is unchecked
                ChangeCheckBoxState(CheckBoxIDs[0], false);
                return;
            }
        }

        // If we reach here, ALL GridView checkboxes are checked
        ChangeCheckBoxState(CheckBoxIDs[0], true);
    }
}

function checkKeyDown(eventObj, txtId) {
    var charCode;

    if (eventObj && eventObj.which) {
        charCode = eventObj.which;
    }
    else {
        charCode = eventObj.keyCode;
    }

    if (charCode == 13) {
        window.document.getElementById(txtId).click();
        return false;
    }
    else {
        return true;
    }
}


function doPostBack(element) {
      tb_remove();
      setTimeout('__doPostBack(\'' + element.name + '\',\'\')', 500);  // 500ms seems to give ThickBox enough time to remove itself
    }


function onUpdating(grd) {

    
    // get the update progress div
    var updateProgressDiv = $get('updateProgressDiv');
    // make it visible
    updateProgressDiv.style.display = '';

    //  get the gridview element        
    var gridView = $get('<%= this.' + grd + '.ClientID %>');

    // get the bounds of both the gridview and the progress div
    var gridViewBounds = Sys.UI.DomElement.getBounds(gridView);
    var updateProgressDivBounds = Sys.UI.DomElement.getBounds(updateProgressDiv);

    //    do the math to figure out where to position the element (the center of the gridview)
    var x = gridViewBounds.x + Math.round(gridViewBounds.width / 2) - Math.round(updateProgressDivBounds.width / 2);
    var y = gridViewBounds.y + Math.round(gridViewBounds.height / 2) - Math.round(updateProgressDivBounds.height / 2);

    //    set the progress element to this position
    Sys.UI.DomElement.setLocation(updateProgressDiv, x, y);
}

function onUpdated() {
    // get the update progress div
    var updateProgressDiv = $get('updateProgressDiv');
    // make it invisible
    updateProgressDiv.style.display = 'none';
}


function updateTextBoxValue(mes, txtid) {
    var txt = document.getElementById(txtid);
    txt.value = mes;
} 

//Takes an arugment for the number of products in stock.
function inStock($num, mes, elem){
	var p = $(elem).parent();
	while(1) {
		if ( $(".product_availability", p).length > 0 ) {
			break;
		} else {
			if ( p.length < 1 || $(p).is("BODY") || $(p).is("HTML") ) {
				break;
			} else {
				p = $(p).parent();
			}
		}
	}
	
	if (p.length > 0) {
		if ( $(".product_availability", p).length > 0 ) {
			if($num >=1){
				$(".product_availability", p).replaceWith('<span class=\"product_availability\" style=\"color:green;\">'+ mes +' </span>');
			} else {
				$(".product_availability", p).replaceWith('<span class=\"product_availability\" style=\"color:red;\">'+ mes +' </span>');
			}
		}
	}
	
	return $num;
}

function same_height_function(args) {
	var elems = [];
	
	var maxh = 0;
	for (var i=0,n=args.length; i < n; i++) {
		var elem = {};
		
		if (typeof args[i] == "string") {
			if (m = args[i].match(/^(.+)\[(.+)\]$/)) {
				elem.id = m[1];
				elem.modif = m[2];
			} else {
				elem.id = args[i]
				elem.modif = "";
			}
		} else {
			elem.id = $(args[i]);
			elem.modif = "";
		}
		
		elem.h = $(elem.id).innerHeight();
		if (maxh < elem.h) maxh = elem.h;
		
		elems.push(elem);
	}
	
	for (var i=0,n=elems.length; i < n; i++) {
		h = $(elems[i].id).innerHeight();
		if (h < maxh) {
			h = maxh;
			
			pt = parseInt($(elems[i].id).css("paddingTop")) || 0;
			pb = parseInt($(elems[i].id).css("paddingBottom")) || 0;
			
			h -= pt + pb;
			
			if (elems[i].modif == "") {
				$(elems[i].id).css({height:h+"px"});
			} else {
				pt = parseInt($(elems[i].modif).css("paddingTop")) || 0;
				pb = parseInt($(elems[i].modif).css("paddingBottom")) || 0;
				mt = parseInt($(elems[i].modif).css("marginTop")) || 0;
				mb = parseInt($(elems[i].modif).css("marginBottom")) || 0;
				
				et = 0;
				$(elems[i].modif, elems[i].id).nextAll().each(function(){
					if ( $(this).css("position") != "absolute" && $(this).css("position") != "fixed" ) {
						et += $(this).outerHeight(true);
					}
				});
				$(elems[i].modif, elems[i].id).prevAll().each(function(){
					if ( $(this).css("position") != "absolute" && $(this).css("position") != "fixed" ) {
						et += $(this).outerHeight(true);
					}
				});
				
				h -= pt + pb + mt + mb + et;
				
				$(elems[i].modif, elems[i].id).css({height:h+"px"});
			}
		}
	}
}
function same_height() {
	var args = arguments;
	
	if ($.browser.safari) {
		$(window).bind("load", function(){
			same_height_function(args);
		});
	} else {
		$().ready(function(){
			same_height_function(args);
		});
	}
	
	
}

(function($){
	$.fn.carousel = function(options) {
		var isMethodCall = (typeof options == "string") || false;
		var args = arguments;
		
		if (isMethodCall) {
			var carousel = $(this[0]).data("carousel");
			if (carousel) {
				if ( carousel[options] && $.isFunction(carousel[options]) ) {
					return carousel[options].apply(carousel, $.makeArray(args).slice(1));
				}
			}
		}
			
		$(this).each(function() {
			var carousel = $(this).data("carousel");
			
			if (isMethodCall && carousel) {
				if ( carousel[options] && $.isFunction(carousel[options]) ) {
					return carousel[options].apply(carousel, $.makeArray(args).slice(1));
				}
			} else if (!isMethodCall) {
				if ( !carousel ) {
					carousel = new $.carousel(this, options);
					$(this).data("carousel", carousel);
				} else {
					carousel.setOptions(options);
				}
			}
		});
		
		return this;
	}
	
	$.carousel = function(elem, options) {
		this.elem = elem;
		this.options = $.extend( {}, $.carousel.defaults, options );
		this.init();
	};
	
	$.extend( $.carousel, {
		defaults: {
			activeIndex: 0,
			slideDuration: 1200
		},
		prototype: {
			init: function() {
				this.clipper = $(".carousel_items", this.elem);
				this.clipper_width = this.clipper.innerWidth();
				this.slides_container = $(".carousel_items>ul", this.elem);
				this.slides = $(">li", this.slides_container);
				this.pages = [];
				
				if (this.slides.length > 0) {
					this.next = $(".carousel_nav_next", this.elem);
					this.prev = $(".carousel_nav_prev", this.elem);
					
					if ( this.next || this.prev ) {
						this.setupNav();
					}
					
					$(this.slides).filter(":first").addClass("carousel_item_first");
					$(this.slides).filter(":last").addClass("carousel_item_last");
					
					this.pages.push(0);
					this.slides_width = 0;
					this.slides_height = 0;
					for (var i=0,n=this.slides.length,w=0,ww=0,j=0,l=0; i < n; i++) {
						w = $(this.slides[i]).outerWidth(true);
						h = $(this.slides[i]).outerHeight(true);
						this.slides_width += w;
						ww += w;
						j++;
						if (ww > this.clipper_width) {
							l = this.slides_width - w;
							this.pages.push(l);
							ww = w;
						}
						
						if ( this.slides_height < h ) {
							this.slides_height = h;
						}
					}
					
					if (this.pages.length > 1) {
						this.pages[this.pages.length-1] = this.slides_width - this.clipper_width;
					}
					
					this.slides.css({height:this.slides_height+"px"});
					this.slides_container.css({height:this.slides_height+"px"});
					this.clipper.css({height:this.slides_height+"px"});
					
					this.slides_container.css({
						width: (this.slides_width)+"px"
					});
					
					if ( this.options.activeIndex < 0 || this.options.activeIndex > this.pages.length-1 ) {
						this.options.activeIndex = 0;
					}
				
					
					this.moveTo(this.options.activeIndex);
				}
			},
			
			setupNav: function(){
				var self = this;
				if ( this.next ) {
					this.next.bind("click", function(){
						this.blur();
						self.moveTo(self.current_index+1);
						return false;
					});
				}
				if ( this.prev ) {
					this.prev.bind("click", function(){
						this.blur();
						self.moveTo(self.current_index-1);
						return false;
					});
				}
			},
			
			initNavState: function(){
				if ( this.next ) {
					if (this.isLast()) {
						$(this.next).addClass("carousel_nav_next_disabled");
					} else {
						$(this.next).removeClass("carousel_nav_next_disabled");
					}
				}
				
				if ( this.prev ) {
					if (this.isFirst()) {
						$(this.prev).addClass("carousel_nav_prev_disabled");
					} else {
						$(this.prev).removeClass("carousel_nav_prev_disabled");
					}
				}
			},
			
			moveTo: function(idx) {
				if ( idx < 0 || idx > this.pages.length-1 ) return;
				
				this.slides_container.stop();
				this.slides_container.animate({left: (-1 * this.pages[idx]) + "px" }, {
					queue:false,
					easing:"easeInOutExpo",
					duration:this.options.slideDuration
				});
				
				this.current_index = idx;
				this.initNavState();
			},
			
			moveNext: function(){
				var idx = this.current_index;
				this.moveTo(++idx);
			},
			
			movePrev: function(){
				var idx = this.current_index;
				this.moveTo(--idx);
			},
			
			isFirst: function(){
				return (this.current_index <= 0);
			},
			
			isLast: function(){
				return (this.current_index >= this.pages.length-1);
			}
		}
	});
	
	function init_carousel() {
		$(".carousel").carousel();
	};
	
	function init_headernav() {
		$(".header_main_menu").each(function(){
			var self = this,
				current_sub = undefined,
				hideTimeout = 0;
			
			var clearHideTimeout = function(){
				if (hideTimeout > 0) {
					window.clearTimeout(hideTimeout);
				}
				hideTimeout = 0;
			};
			var setHideTimeout = function(){
				clearHideTimeout();
				hideTimeout = window.setTimeout(function(){
					hideCurrent();
				},100);
			};
			var hideCurrent = function(){
				if (current_sub != undefined) {
					$("ul", current_sub).hide();
					$(">a", current_sub).removeClass("current");
					
					current_sub = undefined;
				}
			};
			var onMouseover = function(a) {
				if (a != current_sub)  {
					hideCurrent();
					var s = $("ul", a);
					if (s.length) {
						s.show();
						current_sub = a;
						$(">a", current_sub).addClass("current");
					}
				}
			};
			var onMouseout = function(a) {
				hideCurrent();
			};
			
			$(">li", self).each(function(){
				$(this).bind("mouseover", function(){
					onMouseover(this);
				});
				$(this).bind("mouseout", function(){
					onMouseout(this);
				});
				
				if ( $("ul", this).length ) {
					$(this).addClass("hasSubmenu");
				}
				
				if ($.browser.msie && $.browser.version < 8) {
					if ( $("ul", this).length ) {
						$(this).addClass("hasSubmenu");
						$("ul", this).each(function(){
							$(this).show();
							var w = $(this).innerWidth(), ww=0;
							$("a", this).each(function(){
								ww = parseInt($(this).css("paddingLeft")) || 0;
								ww += parseInt($(this).css("paddingRight")) || 0;
								$(this).css({width: (w-ww)+"px"});
							});
							$(this).hide();
						});
					}
				}
			});
		});
	};
	
	function init_sidemenu() {
		$(".sidemenu").each(function(){
			var hideSubmenu = function(p) {
				$("ul", p).hide();
				$("ul li", p).removeClass("selected");
				$(p).removeClass("selected");
			};
			
			var showSubmenu = function(p) {
				var myparent = $(p).parents("ul")[0];
				
				if (myparent) {
					var mysiblings = $("li", myparent).not(p);
					if (mysiblings) {
						$(mysiblings).removeClass("selected");
						$(mysiblings).find("ul").hide();
					}
				}
				
				if ($("ul", p).length) {
					$($("ul", p)[0]).show();
				}
				$(p).addClass("selected");
			};
			
			$(">li>a, li>ul>li>a", this).bind("click", function(){
				this.blur();
				if ( $(this).parent().find("ul").length > 0 ) {
					if ( $(this).parent().is(".selected") ) {
						hideSubmenu( $(this).parent() );
					} else {
						showSubmenu( $(this).parent() );
					}
					return false;
				} else {
					return true;
				}
			});
			
			if ($(">li.selected", this).length) {
				$($(">li.selected", this)[0]).each(function(){
					var c = null, cc=null;
					if ( $(">ul>li.selected", this).length ) {
						c = $(">ul>li.selected", this)[0];
						if ( $(">ul>li.selected", c).length ) {
							cc = $(">ul>li.selected", c)[0];
						}
					}
					
					showSubmenu(this);
					if (c) showSubmenu(c);
					if (cc) showSubmenu(cc);
				});
			}
		});
	};
	
	function init_showcase() {
		$("#featured_products_showcase").each( function() {
			var elem  = this;
			var items_container = $(".showcase_items>ul", elem);
			var items = $(">li>a", items_container);
			var handle = $(".showcase_slider-handle", elem);
			
			var current_idx = 0;
			
			var btn_prev = $(".showcase_slider .prev", elem);
			var btn_next = $(".showcase_slider .next", elem);
			
			var page = 1;
			var page_count = Math.ceil(items.length / 5);
			
			var unSelectCurrent = function(){
				$("li.selected>a, li.selecting>a", items_container).each(function(){
					unSelectItem(this);
				});
			}
			
			var unSelectItem = function(a) {
				var li = $(a).parents("li");
				
				var idx = $("li", items_container).index(li);
				var p = Math.floor(idx / 5) + 1;
				
				$(li).removeClass("selecting");
				$(li).removeClass("selected");
				
				if ( p != page ) {
					var x = 0;
					for (i=0,n=(page-1)*5; i < n; i++) {
						x += $(items[i]).parents("li").is(".selected, .selecting") ? 154 : 132;
					}
					
					$(items_container).stop();
					$(items_container).animate({left:-x+"px"},{
						queue:false,
						duration:800,
						easing:"easeOutExpo"
					});
				}
				
				if (!$(li).is(".unselecting")) {
					$("img",a).stop();
				}
				$("img",a).animate({width:120+"px",height:120+"px"}, {
					queue:false,
					duration:800,
					easing:"easeOutExpo"
				});
				
				if (!$(li).is(".unselecting")) {
					$(li).stop();
				}
				$(li).animate({top:"0px"}, {
					queue:false,
					duration:800,
					easing:"easeOutExpo",
					complete:function(){
						$(this).removeClass("unselecting");
					}
				});
				
				$(li).addClass("unselecting");
				
				var href = a.hash;
				if (href != "" && href.substr(0,1) == "#" && href.length > 1) {
					if ( $(href).length ) {
						$(href).hide();
					}
				}
			}
			
			var moveBar = function(cx, cxx){
				var x = 0; d = 1000;
				var idx = current_idx;
				//var idx = $("li:eq("+current_idx+")", items_container).index(li);
				for (i=0; i < idx; i++) {
					x += $(items[i]).parents("li").is(".selected") ? 154 : 132;
				}
				
				if (cx != undefined) {
					x += cx || 0;
				} else {
					x += parseInt($(items_container).css("left")) || 0;
				}
				x += 5;
				
				$(handle).stop();
				if (cxx != undefined) {
					$(handle).css({left:-cxx+"px"});
				}
				$(handle).animate({left:x+"px"}, {
					queue:false,
					easing:"easeInOutExpo",
					duration:d
				});
			}
			
			var selectItem = function(a, move_bar) {
				var li = $(a).parents("li");
				
				unSelectCurrent();
				
				if ( $(li).is(".selected") ) return;
				//if ( $(li).is(".selecting") ) return;
				
				$(li).removeClass("unselecting");
				
				if (!$(li).is(".selecting")) {
					$("img",a).stop();
				}
				$("img",a).animate({width:142+"px",height:142+"px"}, {
					queue:false,
					duration:800,
					easing:"easeOutExpo"
				});
				if (!$(li).is(".selecting")) {
					$(li).stop();
				}
				$(li).animate({top:-12+"px"}, {
					queue:false,
					duration:800,
					easing:"easeOutExpo",
					complete:function(){
						$(this).addClass("selected");
						$(this).removeClass("selecting");
					}
				});
				
				$(li).addClass("selecting");
				
				var old_page = Math.floor(current_idx / 5) + 1;
				current_idx = $("li", items_container).index(li);
				var new_page = Math.floor(current_idx / 5) + 1;
				if ( !(move_bar != undefined && move_bar == false) ) {
					if ( old_page < new_page ) {
						moveBar((parseInt($(items_container).css("left")) || 0) + 154-132);
					} else {
						moveBar();
					}
				}
				
				var href = a.hash;
				if (href != "" && href.substr(0,1) == "#" && href.length > 1) {
					if ( $(href).length ) {
						$(href).show();
					}
				}
			}
			
			var selectNothing = function(inv){
				if (inv != undefined && inv == 1) {
					$(handle).stop().animate({left:685+"px"},{
						queue:false,
						duration:1000,
						easing:"easeInOutExpo"
					});
				} else {
					$(handle).stop().animate({left:-150+"px"},{
						queue:false,
						duration:1000,
						easing:"easeInOutExpo"
					});
				}
			}
			
			for (var i=0,n=items.length; i < n; i++) {
				$(items[i]).bind("click", function(){
					this.blur();
					selectItem(this);
					return false;
				});
			}
			
			$(btn_next).bind("click", function(){
				this.blur();
				
				if (page < page_count) {
					page++;
					var x = 0;
					
					for (i=0,n=(page-1)*5; i < n; i++) {
						x += $(items[i]).parents("li").is(".selected, .selecting") ? 154 : 132;
					}
					
					$(items_container).stop();
					$(items_container).animate({left:-x+"px"},{
						queue:false,
						duration:900,
						easing:"easeInOutExpo"
					});
					
					if ( current_idx >= (page-1)*5 && current_idx < page*5 ) {
						moveBar(-x, parseInt($(items_container).css("left")) - x - 180);
					} else {
						selectNothing();
					}
				}
				
				return false;
			});
			
			$(btn_prev).bind("click", function(){
				this.blur();
				
				if (page > 1) {
					page--;
					var x = 0;
					for (i=0,n=(page-1)*5; i < n; i++) {
						x += $(items[i]).parents("li").is(".selected, .selecting") ? 154 : 132;
					}
					
					$(items_container).stop();
					$(items_container).animate({left:-x+"px"},{
						queue:false,
						duration:900,
						easing:"easeInOutExpo"
					});
					
					if ( current_idx >= (page-1)*5 && current_idx < page*5 ) {
						moveBar(x, x - parseInt($(items_container).css("left")));
					} else {
						selectNothing(1);
					}
				}
				
				return false;
			});
			
			var handleMoved = function(v) {
				
				v += 154/2;
				
				var x = parseInt($(items_container).css("left"));
				var x1 = 0,x2;
				for (i=(page-1)*5,n=page * 5; i < n; i++) {	
					x2 = x1 + ( $(items[i]).parents("li").is(".selected, .selecting") ? 154 : 132 );
					
					if ( v > x1 && v < x2 ) {
						if (i != current_idx) {
							selectItem(items[i], false);
						}
						break;
					}
					
					x1 = x2;
				}
			}
			
			$(handle).each(function(){
				var $handle = $(this);
				var mdown = false;
				var lastPos = 0;
				var value = 0;
				var max = $handle.parent().innerWidth() - 145 -5;
				
				var getAbsoluteLeft = function(){
					return $handle.parent().offset().left;
				}
				
				var handle_mousedown = function(e) {
					mdown = true;
					lastPos = e.pageX;
					
					var move = parseInt(lastPos - getAbsoluteLeft() - (145 / 2));
					if (move > max) move = max;
					if (move < 5) move = 5;
					
					value = move;
					$handle.stop();
					$handle.css("left", move+"px");
					
					$(document).bind("mousemove", handle_mousemove);
					$(document).bind("mouseup", handle_mouseup);
					
					e.preventDefault();
				}
				
				var handle_mousemove = function(e) {
					
					if (mdown) {
						
						var move = e.pageX - lastPos;
						lastPos = e.pageX;
						move += parseInt($handle.css("left")) || 0;
						if (move > max) move = max;
						if (move < 5) move = 5;
						value = move;
						$handle.stop();
						$handle.css("left", move+"px");
						
						handleMoved(value);
						
						e.preventDefault();
					}
				}
				
				var handle_mouseup = function(e) {
					mdown = false;
					
					$(document).unbind("mousemove", handle_mousemove);
					$(document).unbind("mouseup", handle_mouseup);
					
					e.preventDefault();
					
					moveBar();
				}
				
				$handle.bind("mousedown", handle_mousedown);
				
				$handle.css("MozUserSelect","none").attr("unselectable","on");
			});
			
			if ($(">li.selected", items_container).length) {
				var a = $(">li.selected:eq(0)>a", items_container)[0];
				$(">li", items_container).removeClass("selected");
				selectItem(a);
			} else {
				selectItem($(">li:eq(0)>a", items_container)[0]);
			}
		});
	};
	
	function init_grids_listing() {
		$(".grids_listing").each(function(){
			var elem = this;
			var items = $(".grid_item", elem);
			if (items.length < 1) return;
			
			var init_grids = function(){
				var c = 0, r = 0;
				var sh = [];
				$(items).each(function(){
					if (c % 2 != 0) {
						$(this).addClass("grid_br");
					}
					
					if (r > 0) {
						$(this).addClass("grid_bt");
					}
					
					c++;
					sh.push(this);
					if (c > 0 && c % 2 == 0) {
						r++;
						same_height_function(sh);
						sh = [];
					}
				});
			};
			
			init_grids();
		});
	};
	
	function init_cart_dropdown() {
		$(".shopping_cart").each(function(){
			if (!$(this).parent().next().is(".shopping_cart_dropdown")) {
				return;
			}
			
			var elem = this;
			var parent = $(this).parent().parent();
			var borders = undefined;
			var dropdown = $(".shopping_cart_dropdown", parent);
			var hideTimeout = 0;
			var mover = false;
			
			$(elem).addClass("shopping_cart_hover");
			
			if ($(".shopping_cart_border", parent).length < 1){
				borders = $("<div />").addClass("shopping_cart_border").append( $("<div />") ).hide();
				borders.appendTo(parent);
			} else {
				borders = $(".shopping_cart_border", parent);
			}
			borders.hide();
			
			var clearHideTimeout = function(){
				if (hideTimeout > 0) {
					window.clearTimeout(hideTimeout);
					hideTimeout = 0;
				}
				hideTimeout = 0;
			};
			
			var setHideTimeout = function(){
				clearHideTimeout();
				hideTimeout = window.setTimeout(function(){
					if (mover) {
						$(dropdown).hide();
						borders.hide();
						mover = false;
					}
				}, 200);
			};
			
			var onMouseOver = function() {
				clearHideTimeout();
				if (!mover) {
					mover = true;
					$(dropdown).show();
					borders.show();
				}
			};
			
			var onMouseOut = function() {
				setHideTimeout();
			};
			
			$(elem).bind("mouseover", onMouseOver);
			$(borders).bind("mouseover", onMouseOver);
			$(dropdown).bind("mouseover", onMouseOver);
			
			$(elem).bind("mouseout", onMouseOut);
			$(borders).bind("mouseout", onMouseOut);
			$(dropdown).bind("mouseout", onMouseOut);
			
		});
	};
	
	function init_pricelist(visibleItems) {
		visibleItems = visibleItems || 6;
		$(".pricelist").each(function(){
			var elem  = this;
			var table = $("table", elem);
			var door = $(".pricelist_door", elem);
			var doorDisabled = false;
			var door_close = $(".pricelist_door .close", elem);
			var door_open = $(".pricelist_door .open", elem);
			
			if (table.length < 1 || door.length < 1) {
				return;
			}
			
			if ( $(elem).parents(".featured_product_content").length) {
				
				var pV = $(elem).parents(".featured_product_content").is(":visible");
				$(elem).parents(".featured_product_content").show();
			}
			
			var table_height = 0, i = 0, closed_height=0;
			$("tr", table).each(function(){
				table_height += $(this).outerHeight();
				
				$(this).data("pricelist_h", table_height);
				i++;
				if (i > visibleItems && closed_height==0) {
					closed_height = table_height;
				}
			});
			
			if (closed_height == 0) {
				closed_height = table_height;
			}
			if (closed_height == table_height) {
				doorDisabled = true;
			}
			
			if ($.browser.msie && $.browser.version > 7 && $.browser.version < 9) {
				table_height += 4;
				closed_height += 2;
			}
			
			if (!$(table).parent().is("div.pricelist_room")) {
				$(table).wrap( $("<div />").addClass("pricelist_room") );
			}
			
			var room = $(".pricelist_room", elem);
			room.css({
				padding:0,
				margin:0,
				overflow:"hidden",
				height:closed_height+"px"
			});
			
			if ($.browser.msie && $.browser.version < 7) {
				room.css({
					display:"inline-block"
				});
			}
			
			if (!doorDisabled) {
				door_close.hide();
				door_open.show();
				
				door_close.bind("click", function(){
					this.blur();
					room.stop();
					room.animate({height: closed_height+"px"}, {duration:300, complete:function(){
						door_close.hide();
						door_open.show();
						$(this).css({
							overflow:"hidden",
							height:closed_height+"px"
						});
					}});
					return false;
				});
				
				door_open.bind("click", function(){
					this.blur();
					room.stop();
					room.animate({height: table_height+"px"}, {duration:600, complete:function(){
						door_open.hide();
						door_close.show();
					}});
					return false;
				});
			} else {
				door.hide();
			}
			
			if ( $(elem).parents(".featured_product_content").length && !pV) {
				$(elem).parents(".featured_product_content").hide();
			}
		});
	};
	
	function init_report_table() {
		
		$(".report_table_container").each(function(){
			$(".report_table", this).each(function(){
				
				var p = $(this).parent();
				if (!$(p).is(".report_table_container")) {
					$(this).wrap($("<div />").addClass("report_table_container"));
					p = $(this).parent();
				}
				
				$(this).hide();
				pWidth = $(p).innerWidth();
				pWidth -= parseInt($(p).css("paddingLeft"));
				pWidth -= parseInt($(p).css("paddingRight"));
				
				
				
				$(this).show();
				$(this).css("width","");
				
				$(p).css({
					"overflow":"hidden",
					width:pWidth+"px"
				});
				if (pWidth >= $(this).outerWidth(true)) {
					$(this).css({
						"width":(pWidth)+"px"
					});
					if (pWidth < $(this).outerWidth(true)) {
						$(p).css({
							"overflow":"auto"
						});
					};
					
				} else {
					$(p).css({
						overflow:"auto"
					});
				}
				
				$(p).css("height", ($(this).outerHeight()+32)+"px");
			});
		});
		
		$(".report_table").each(function(){
			$("tr:even", this).not(".total").not(".header").addClass("even");
			
			if ($.browser.msie && $.browser.version < 8) {
				$("tr", this).each(function(){
					var tr = this;
					
					maxH = 0;
					
					$("td, th", tr).each(function(){
						if (maxH < $(this).innerHeight()) {
							maxH = $(this).innerHeight();
						}
					});
					
					$("td, th", tr).each(function(){
						padTop = parseInt($(this).css("paddingTop") || "");
						padBottom = parseInt($(this).css("paddingBottom") || "");
						padLeft = parseInt($(this).css("paddingLeft") || "");
						padRight = parseInt($(this).css("paddingRight") || "");
						
						h = maxH;
						h -= padTop;
						h -= padBottom;
						
						w = $(this).innerWidth();
						w -= padLeft;
						w -= padRight;
						
						
						$(this).wrapInner($("<span />").css({
							display:"inline-block",
							height:h+"px",
							width:w+"px",
							cssFloat:"left",
							marginRight:"1px",
							verticalAlign:"middle",
							paddingTop: padTop,
							paddingBottom: padBottom,
							paddingLeft: padLeft - 1,
							paddingRight: padRight - 1
						}));
						
						$(this).css({
							paddingTop:0,
							paddingBottom:0,
							paddingLeft:0,
							paddingRight:0
						});
					});
				});
			}
		});
	};
	
	function init_tabs() {
		$(".tabs").each(function(){
			var elem = this;
			var nav = $(".tabs_nav>ul>li");
			
			$(".tabs_nav>ul>li a", elem).each(function(){
				var href = this.hash.replace('#', '');
				var li = $(this).parents("li");
				if (href != '' && $("#" + href).length > 0) {
					$(this).data("_tab_content", $("#" + href));
					
					$(this).bind("click.tabs", function(){
						tabOnClick.apply(elem, [li, this, $("#" + href)]);
						this.blur();
						return false;
					});
				}
				
				if (!$("span.blc", li).length) {
					$(li).append(
						$("<span />").addClass("blc")
					);
				}
				
				if (!$("span.brc", li).length) {
					$(li).append(
						$("<span />").addClass("brc")
					);
				}
			});
			
			var tabOnClick = function(li, a, content) {
				if (!$(li).is(".selected")) {
					//hide cur tab
					$(nav).filter(".selected").removeClass("selected").find("a").data("_tab_content").hide();
					$(li).addClass("selected").find("a").data("_tab_content").show();
				}
			};
			
			if ($(nav).filter(".selected").length < 1) {
				$(nav).filter(":first").addClass("selected");
			}
			
			$(nav).not(".selected").find("a").each(function(){
				$(this).data("_tab_content").hide();
			});
		});
	};
	
	function init_borders() {
		if (!$(".pageWrapper").length) return;
		
		var w = 0;
		$(".pageWrapper").children().each(function(){
			if (w < $(this).outerWidth(true)) {
				w = $(this).outerWidth(true);
			}
		});
		w += 30;
		
		var ww = $(".pageWrapper").data("_w") ? $(".pageWrapper").data("_w") : 0;
		
		var h = $(".pageWrapper").innerHeight();
		var hh = $(".pageWrapper").data("_h") ? $(".pageWrapper").data("_h") : 0;
		
		var t = $(".pageWrapper").data("_to") ? $(".pageWrapper").data("_to") : 0;
		if (t) window.clearTimeout(t);
		$(".pageWrapper").data("_to", 0)
		
		if (hh == h && ww == w) {
			$(".pageWrapper").data("_to", window.setTimeout(function(){
				init_borders();
			}, 400));
			return;
		}
		
		$(".pageWrapper").data("_h", h);
		$(".pageWrapper").data("_w", w);
		
		$(".pageWrapper").css({
			width:w+"px",
			position:"relative",
			marginLeft:"auto",
			marginRight:"auto",
			marginTop:0,
			marginBottom:0
		});
		
		if (!$(".pageBorder-b").length) {
			$("<div />").addClass("pageBorder-b").append($("<img />").attr({
				src: "images/page-bdr-b.png",
				alt: "",
				width:w,
				height:"15",
				marginLeft:"auto",
				marginRight:"auto",
				marginTop:0,
				marginBottom:0
			})).css({
				height:"15px",
				width:w+"px",
				marginLeft:"auto",
				marginRight:"auto",
				marginTop:0,
				marginBottom:0
			}).insertAfter( $(".pageWrapper") );
		}
		
		if ($.browser.msie && $.browser.version < 7) {
			if ($(".pageBorder-l").length) $(".pageBorder-l").remove();
			if ($(".pageBorder-r").length) $(".pageBorder-r").remove();
		}
		
		if (!$(".pageBorder-l").length) {
			$("<div />").addClass("pageBorder-l").append($("<img />").attr({
				src: "images/page-bdr-l.png",
				alt: "",
				width: "15"
			})).appendTo( $(".pageWrapper") );
		}
		if (!$(".pageBorder-lb").length) {
			$("<div />").addClass("pageBorder-lb").append($("<img />").attr({
				src: "images/page-bdr-lb.png",
				alt: "",
				width: "15",
				height: "40"
			})).appendTo( $(".pageWrapper") );
		}
		
		if (!$(".pageBorder-r").length) {
			$("<div />").addClass("pageBorder-r").append($("<img />").attr({
				src: "images/page-bdr-r.png",
				alt: "",
				width: "15"
			})).appendTo( $(".pageWrapper") );
		}
		if (!$(".pageBorder-rb").length) {
			$("<div />").addClass("pageBorder-rb").append($("<img />").attr({
				src: "images/page-bdr-rb.png",
				alt: "",
				width: "15",
				height:"40"
			})).appendTo( $(".pageWrapper") );
		}
		
		$(".pageBorder-l").css({
			position:"absolute",
			left:0,
			top:0,
			width:"15px",
			height:(h-40)+"px"
		});
		$(".pageBorder-l img").attr({
			height:(h-40)
		}).css({
			height:(h-40)+"px",
			width:"15px"
		});
		$(".pageBorder-lb").css({
			position:"absolute",
			left:0,
			top:(h-40)+"px",
			width:"15px",
			height:"40px"
		});
		
		$(".pageBorder-r").css({
			position:"absolute",
			right:0,
			top:0,
			width:"15px",
			height:(h-40)+"px"
		});
		$(".pageBorder-r img").attr({
			height:(h-40)
		}).css({
			height:(h-40)+"px"
		});
		$(".pageBorder-rb").css({
			position:"absolute",
			right:0,
			top:(h-40)+"px",
			width:"15px",
			height:"40px"
		});
		
		if ($.browser.msie && $.browser.version < 7) {
			$(".pageBorder-l img, .pageBorder-r img, .pageBorder-b img, .pageBorder-lb img, .pageBorder-rb img").pngfix({sizingMethod:"scale"});
			
		}
		
		$(".pageWrapper").data("_to", window.setTimeout(function(){
			init_borders()
		}, 400));
	};
	
	$().ready(function(){
		
		//init_borders();
		
		if ($.browser.msie && $.browser.version < 7) {
			$("img[src$=png], .showcase_slider-handle").pngfix({sizingMethod:"scale"});
		}
		
		init_headernav();
		init_sidemenu();
		init_showcase();
		init_cart_dropdown();
		
		init_pricelist(6);
		
		init_grids_listing();
		
		init_carousel();
		
		init_tabs();
		
		init_report_table();
		
		/* fix rounded box in IE 6 */
		if ($.browser.msie && $.browser.version < 7) {
			$(".rbox-cbl, .rbox-cbr").each(function(){
				$(this).clone().appendTo( $(this).parent() );
				$(this).remove();
			});
		}
		
		init_borders();
	});
})(jQuery);
