$(document).ready(function(){				
	$("#productDetailsSizeTableBTN").click(function(){	
	
		$("#fixedPageOverlay").css({ display: "block" });
		$("#sizeTable").fadeIn(200);		
	
		var ww = $(window).width();
		var iw = $("#productDetailsSizeTable table").width() + $("#productDetailsSizeTable img").width() + 50;
		
		var wr = $("#wrapper").width();
		var rr = (ww - iw) / 2;
		if (rr < 0) rr = 0;		
		
		$("#sizeTable").css({width: iw + "px", margin: "50px " + rr + "px"});
		$(".fixedPageOverlayClose").css({ width: iw + "px"});
					
	});
	
	$("#sizeTable, #fixedPageOverlay").click(function(){
		$("#sizeTable, #fixedPageOverlay").css({ display: "none" });
	});
});

(function($){
	$.fn.tawFisheye = function(options) {
		
		first_img = $('img:first', this);
		var sfwidth = first_img.width();
		var sfheight = first_img.height();
		
		if(sfwidth == null || sfheight == null) return false;
		
		img = new Image();
		img.src = first_img.attr("src");
		var sowidth = img.width;
		var soheight = img.height;
		
		
		var defaults = {
			duration: "slow",
			startWidth: sfwidth + "px",
			startHeight: sfheight + "px",
			endWidth: sowidth + "px",
			endHeight: soheight + "px"
		};
		
		var options = $.extend(defaults, options);	
		
		curr_top_pos = $(first_img).css("top");
				
		if(curr_top_pos == "auto") curr_top_pos = "0px";
		
		
		var indexOfPxStartHeight = options.startHeight.indexOf("px");
		var indexOfPxEndHeight = options.endHeight.indexOf("px");
		var indexOfPcStartHeight = options.startHeight.indexOf("%");
		var indexOfPcEndHeight = options.startHeight.indexOf("%");
		var indexOfEmStartHeight = options.startHeight.indexOf("em");
		var indexOfEmEndHeight = options.startHeight.indexOf("em");
		
		if(indexOfPxStartHeight != -1) {
			var sh = options.startHeight.substr(0, indexOfPxStartHeight);
			var eh = options.endHeight.substr(0, indexOfPxEndHeight);
		} else if(indexOfPcStartHeight != -1) {
			var sh = options.startHeight.substr(0, indexOfPcStartHeight);
			var eh = options.endHeight.substr(0, indexOfPcEndHeight);
		} else if(indexOfEmStartHeight != -1) {
			var sh = options.startHeight.substr(0, indexOfEmStartHeight);
			var eh = options.endHeight.substr(0, indexOfEmEndHeight);
		} else {
			zoom_top_pos = "0px";
		}
		
		zoom_top_pos = ((sh - eh) / 2) + "px";		
		
		if(navigator.appName == "Opera") {
			var padding_error = $(first_img).css("margin-top");
			padding_error = padding_error.substr(0, padding_error.indexOf("px"));
			curr_top_pos = curr_top_pos.substr(0, curr_top_pos.indexOf("px"));
			curr_top_pos = (curr_top_pos - padding_error) + "px";
		}
		
		
					
		
		return this.each(function() {
								 
			var obj = $(this);
			var $imgs = $('img', obj);
			if($imgs.length === 0){
				return false;
			
			} else {
				var loadedImages = [];
				$imgs.each(function(i, val){
					$(this).bind('load', function(){
					
					}).each(function(){
						
						owidth = options.startWidth;
						oheight = options.startHeight;
						fwidth = options.endWidth
						fheight = options.endHeight;
						
						$(this).mouseover(function(){
							$(this).stop();
							$(this).animate({
								width: fwidth,
								height: fheight,
								top: zoom_top_pos
							}, options.duration, "", function(){
								$(this).css({ "top": zoom_top_pos });
							});
						}).mouseout(function(){
							$(this).stop();
							$(this).animate({
								width: owidth,
								height: oheight,
								top: curr_top_pos
							}, options.duration, "", function(){
								$(this).css({ "top": curr_top_pos });
							});
						});
					});
				});
			}
		
		});
	};
})(jQuery);

(function($){
	$.fn.tawPreloadImages = function(options, func) {
				
		var defaults = {
			image_path: 'ajax-loader.png',
			css_background_class: 'tawImagePreloaderContainer',
			css_background_class_on_error: 'tawImagePreloaderContainerOnError',
			width: "100%",
			height: "100%",
			image_fade_in_time: 200,
			jqSize: false,
			substractCssSize: false,
			substractPadding: false,
			substractBorder: false
		};
		
		
		
		var options = $.extend(defaults, options);		
		
		return this.each(function() {
								  
			var imgs = $('img', this);
			
			if(imgs.length === 0){
				return false;
			} else {
				
     			var loadedImages = [];
				var i = 0;
				
				imgs.each(function(i, val){	
							   
					if(options.jqSize == false) {
						if($(this).width() > 0){
							var rslt_width = $(this).width() + "px";
						} else {
							var rslt_width = options.width;
						}
						if($(this).height() > 0){
							var rslt_height = $(this).height() + "px";	
						} else {
							var rslt_height = options.height;
						}
					} else {
						var rslt_width = options.width;
						var rslt_height = options.height;
					}
					$(this).css({ "visibility": "hidden" });
					$(this).wrap('<div class="' + options.css_background_class + '"></div>');
					
					cssWidth = $(this).parent().css("width");
					cssHeight = $(this).parent().css("height");
					if(cssWidth != "auto" && cssWidth != "0px") rslt_width = cssWidth;
					if(cssHeight != "auto" && cssHeight != "0px") rslt_height = cssHeight;
					
					
					$(this).parent().width(rslt_width).height(rslt_height);
					
					var or_width = $(this).width();
					var or_height = $(this).height();
					
					$(this).bind('load', function () {
												   
						$(this).css({ "visibility": "visible", "display": "none" });
						var randomID = "tawImagePreloaderRandomId" + (Math.floor(Math.random() * 100001));
						$(this).addClass(randomID);
						$(this).parent().replaceWith($(this).parent().html());
						
						if(options.jqSize === true) $("." + randomID).width(options.width).height(options.height);
						
						$("." + randomID).fadeIn(options.image_fade_in_time, function(){
							$(this).removeClass(randomID);
						});
						
						if($.inArray(i, loadedImages) == -1){
							loadedImages.push(i);
							if(loadedImages.length == imgs.length){
								if(func) setTimeout(function(){ func() }, options.image_fade_in_time + 50);								
							}
						}
						
						
					}).error(function() {					
						
						if($(this).width() > 0 && options.jqSize == false){
							if(or_width > 0){
								var rslt_width = or_width + "px";
							} else {
								var rslt_width = $(this).width() + "px";
							}
						} else {
							var rslt_width = options.width;
						}
						if($(this).height() > 0 && options.jqSize == false){
							if(or_width > 0){
								var rslt_height = or_height + "px";
							} else {
								var rslt_height = $(this).height() + "px";
							}
						} else {
							var rslt_height = options.height;
						}
						
						var randomID = "tawImagePreloaderRandomId" + (Math.floor(Math.random() * 100001));
						$(this).parent().replaceWith('<div id="' + randomID + '" class="' + options.css_background_class_on_error + '">' + $(this).attr("alt") + '<div>');
						$("#" + randomID).hide();
											
						cssWidth = $("#" + randomID).css("width");
						cssHeight = $("#" + randomID).css("height");
						if(cssWidth != "auto" && cssWidth != "0px") rslt_width = cssWidth;
						if(cssHeight != "auto" && cssHeight != "0px") rslt_height = cssHeight;
						
						if(options.substractCssSize){
							options.substractPadding = true;
							options.substractBorder = true;
						}						
						if(options.substractPadding === true){
							var paddingTop = parseInt($("#" + randomID).css("paddingTop"));
							var paddingRight = parseInt($("#" + randomID).css("paddingRight"));
							var paddingBottom = parseInt($("#" + randomID).css("paddingBottom"));
							var paddingLeft = parseInt($("#" + randomID).css("paddingLeft"));
							
							rslt_width = (parseInt(rslt_width) - paddingLeft - paddingRight) + "px";
							rslt_height = (parseInt(rslt_height) - paddingTop - paddingBottom) + "px";
						}
						if(options.substractBorder === true){
							var borderTopWidth = parseInt($("#" + randomID).css("borderTopWidth"));
							var borderRightWidth = parseInt($("#" + randomID).css("borderRightWidth"));
							var borderBottomWidth = parseInt($("#" + randomID).css("borderBottomWidth"));
							var borderLeftWidth = parseInt($("#" + randomID).css("borderLeftWidth"));
							
							rslt_width = (parseInt(rslt_width) - borderLeftWidth - borderRightWidth) + "px";
							rslt_height = (parseInt(rslt_height) - borderTopWidth - borderBottomWidth) + "px";
						}
						
						$("#" + randomID).width(rslt_width).height(rslt_height);	
						
						$("#" + randomID).fadeIn(options.image_fade_in_time);
						$("#" + randomID).removeAttr("id");
						
						
						if($.inArray(i, loadedImages) == -1){
							loadedImages.push(i);
							if (navigator.appName == "Netscape") imgslength = imgs.length - 1;
							else imgslength = imgs.length;
							if(loadedImages.length == imgslength){
								if(func) setTimeout(function(){ func(); }, options.image_fade_in_time + 50);
							}
						}
						
					});
						
				}).each(function(){
					  if(this.complete || this.complete === undefined){ this.src = this.src; } //needed for potential cached images
					  
					  i++
					  if(i == imgs.length){
					 	 if(func) setTimeout(function(){ func() }, options.image_fade_in_time + 50);
						 i = 0;
					  }
					  /*
					  if($.inArray(i, loadedImages) == -1){
						  loadedImages.push(i);
						  if(loadedImages.length == imgs.length){
						     if(func) setTimeout(function(){ func() }, options.image_fade_in_time + 50);								
						  }
					  }
						*/
					  $(this).css("visibility", "visible");
					  $(this).parent().removeAttr("id").removeClass();
				});
			}
		});
	};
})(jQuery);

(function($) {
    $.fn.tawImagePreview = function(options) {

        var defaults = {
            targetContainerId: '',
            targetImageContainerId: '',
            targetImageContainerErrorClass: '',
            targetDescriptionId: '',
            max_width: '100px',
            max_height: '100px',
            offsetLeft: '0px',
            offsetRight: '0px',
            offsetTop: '0px',
            offsetBottom: '0px',
            substractCssSize: false,
            substractPadding: false,
            substractBorder: false,
            float: "right",
            delay: 0
        };

        var options = $.extend(defaults, options);

        return this.each(function() {

            var alt = $(this).attr("alt");
            var title = $(this).attr("title");
            var longdesc = $(this).attr("longdesc");


            var resizePreloadingBox = function(magnifier, image) {

                var offset = $(magnifier).offset();
                var offset_left = checkFloatDirection(offset, parseInt(options.max_width));
                var offset_top = offset.top - parseInt(options.max_height);


                $("#" + options.targetImageContainerId).width(options.max_width).height(options.max_height);

                $("#" + options.targetContainerId).css({
                    "position": "absolute",
                    "top": offset_top + "px",
                    "left": offset_left + "px",
                    "width": options.max_width,
                    "height": "auto"
                });

                var tmp_height = $("#" + options.targetContainerId).height();
                offset_top = offset.top - tmp_height;
                $("#" + options.targetContainerId).css({ "top": offset_top + "px" });

                $("#" + options.targetContainerId).show();

            }



            var resizeLoadedBox = function(magnifier, image) {

                $(image).width("auto").height("auto");
                var img_width = $(image).width();
                var img_height = $(image).height();

                if (img_width > parseInt(options.max_width)) {
                    var factorX = parseInt(options.max_width) / img_width;
                    img_width = Math.round(parseInt(options.max_width));
                    img_height = Math.round(img_height * factorX);
                }
                if (img_height > parseInt(options.max_height)) {
                    var factorY = parseInt(options.max_height) / img_height;
                    img_height = Math.round(parseInt(options.max_height));
                    img_width = Math.round(img_width * factorY);
                }

                if (img_width <= 0) img_width = options.max_width;
                if (img_height <= 0) img_height = options.max_height;

                var infobox_width = img_width - parseInt($("#" + options.targetDescriptionId).css("paddingLeft")) - parseInt($("#" + options.targetDescriptionId).css("paddingRight"))

                $(image).width(img_width + "px").height(img_height + "px");
                $("#" + options.targetImageContainerId).width(img_width + "px").height(img_height + "px");
                $("#" + options.targetDescriptionId).width(infobox_width + "px");
                $("#" + options.targetContainerId).width(img_width);

                var offset = $(magnifier).offset();
                var offset_left = checkFloatDirection(offset, img_width);
                var offset_top = offset.top - parseInt(options.max_height);

                $("#" + options.targetContainerId).height("auto").css({ "top": (offset.top - $("#" + options.targetContainerId).height()) + "px" });
                $("#" + options.targetContainerId).show();
            }


            var checkFloatDirection = function(offset, img_width) {
                if (options.float == "right") {
                    var offset_left = offset.left + parseInt(options.offsetRight);

                } else if (options.float == "left") {
                    var offset_left = offset.left - parseInt(options.offsetLeft) - img_width;

                } else if (options.float == "dynamic") {
                    if (offset.left < ($(window).width() / 2)) {
                        var offset_left = offset.left + parseInt(options.offsetRight);
                    } else {
                        var offset_left = offset.left - parseInt(options.offsetLeft) - img_width;
                    }
                }
                return offset_left;
            }


            var stillMouseOver = false;
			
            $(this).hover(
				function() {
					
					stillMouseOver = true;
					
				    $("#" + options.targetContainerId).hide();
				    $(this).attr("title", "");
				    var magnifier = this;
				    var image = $("#" + options.targetImageContainerId + " img");

				    window.tawImagePreviewTimer = setTimeout(function() {

				        resizePreloadingBox(magnifier, image);

				        $("#" + options.targetDescriptionId).html("");
				        $.post(longdesc, function(data) {
							if(stillMouseOver == true){
								$("#" + options.targetDescriptionId).replaceWith('<div id="' + options.targetDescriptionId + '">' + data + "</div>");
								resizeLoadedBox(magnifier, image);
							}
				        });


				        $(image).attr("src", title);
				        $(image).bind('load', function() {
				            $(image).unbind('load');
				            resizeLoadedBox(magnifier, image);
				        }).error(function() {
				            $("#" + options.targetImageContainerId).html('<div class="' + options.targetImageContainerErrorClass + '">' + options.targetImageContainerErrorMessage + '</div>')
				        });
				    }, options.delay);

				},
				function() {
				    clearTimeout(window.tawImagePreviewTimer);
					stillMouseOver = false;
				    $(this).attr("title", title);
				    $("#" + options.targetContainerId).hide();
				    $("#" + options.targetImageContainerId).html('<img src="" alt="" />');
				}
			);
        });
    };
})(jQuery);

