$(document).ready(function(){

// for  persistent cart display at top of template
$("#scart_order_qty_str_holder").html(scart_order_qty_str);

// for newsletter subscribe form

$("#subscribe_input_field").click(function(){
    $this = $(this);
    if($this.attr("value") == "Enter Your Email"){
        $this.attr("value","");
    } // if
}); // click

$("#subscribe_input_field").blur(function(){
    $this = $(this);
    if($this.attr("value") == ""){
        $this.attr("value","Enter Your Email");
    } // if
}); // blur

// for options on detailed product template

$(".product_options_title td").attr("align","");
$(".option_name").attr("align","");

// for tell a friend page

if($(".search_header td b").html() == "Suggest our Webpage to a Friend"){
	$("#mt_wrapper").addClass("tell_a_friend_wrapper");
}


////////////////////////////////////

//Mega Menu

////////////////////////////////////
// Settings
////////////////////////////////////

// width of columns inside mega dropdown. Does NOT set the width. This is used to calculate the appropriate width for the dropdown containers.
var mega_dropdown_col_width = 195;
// defines the element, such as table or div that is the outer edge of the template. Replace #wrapper with id or class of element.
var mega_template_container = $("#mt_wrapper");

////////////////////////////////////
////////////////////////////////////
// End Settings
////////////////////////////////////

var mega_template_container_width = $(mega_template_container).width();

// set width of .mega_content dropdowns in horizontal nav
$("#mega_menu .mega_content").each(function(){
	// set width to same width as parent li instead of columnsxcol width
	/*
	var curr_number_of_columns = $(this).find("div").length;
	var curr_mega_content_width = (curr_number_of_columns * mega_dropdown_col_width);
	if(curr_number_of_columns == 1){curr_mega_content_width = curr_mega_content_width - 10;}
	curr_mega_content_width = curr_mega_content_width + "px";
	$(this).css("width",curr_mega_content_width);
	*/
	// set width to same width as parent li
	var the_dropdown_width = $(this).parent("li").width();
	$(this).css("width",the_dropdown_width + "px");
	// set the width of links within the dropdowns to less than the width of the parent li
	var child_links_width = the_dropdown_width - 30;
	
	$(this).find("a").each(function(){
		$(this).css("width", child_links_width + "px");
    });

});

existing_template_position = $(mega_template_container).offset();
existing_template_left_margin = existing_template_position.left;
$(".mega").each(function(){
	// if browser is ie6, add iframe beneath dropdown so that it appears over select boxes
	
/*	if($.browser.msie && $.browser.version=="6.0") {
		var ie6_iframe = "<iframe class='mega_cover_select' />";
		$(this).prepend(ie6_iframe);
		var mega_height_to_match = $(this).height;
		$(".mega_cover_select").css("height",mega_height_to_match);
	}
*/
	var curr_mega_content_width = $(this).find(".mega_content").width();
	// if dropdown is greater than half of screen width, then place in center
	if(curr_mega_content_width >= mega_template_container_width/2 ){
		var desired_left_value = (mega_template_container_width - curr_mega_content_width)/2;
		var existing_position = $(this).offset();
		var existing_left_position = existing_position.left;
		var existing_left_inside_template_position = existing_left_position - existing_template_left_margin;
		var left_position_adjustment = ((existing_left_inside_template_position - desired_left_value) * -1) + "px";
		// dont' center the dropdown if it's too close to the edge
		if($(this).hasClass("mega_right_align") == false && $(this).hasClass("no_auto_center") == false){
			$(this).find(".mega_content").css("left",left_position_adjustment);
		} 
		// desired_left_value > existing_left_inside_template_position
	}
	
// assign right position to dropdown so that it stays on screen
	if($(this).hasClass("mega_right_align")) {
			// get width of mega_content
			var curr_mega_width = $(this).width();
			if(curr_mega_content_width < 498 || existing_left_inside_template_position > $(this).width()){
				var curr_left_value = -1 * (curr_mega_content_width - curr_mega_width + 8);
				// assign CSS left property to mega content
				$(this).find(".mega_content").css("left",curr_left_value);
			} //if
		} // if
	} //function
); // each

//change class on hover to cause dropdown
$(".mega").hover(function() {
    $this = $(this);
    var curr_background_position = $this.find("a:first").css("background-position");
    $this.find("a:first").css("background-position",curr_background_position);
    t = setTimeout(function() {
       $this.addClass("hovering");
    }, 200);
    $this.data('timeout', t);
 
}, function() {
    $this = $(this);
    $this.find("a:first").removeAttr("style");
    clearTimeout($this.data('timeout'));
		$this.removeClass("hovering");
});

////////////////////////////
////////////////////////////
// jqzoom
////////////////////////////
////////////////////////////

// if the current page is a product page
if(typeof current_product_id != "undefined" && current_product_id !== "0"){
	$.ajax({
	    type: "POST",
	    url: SFUI.ThisWebsite.getPublicJsonService("products/getProductExtraImagesInfo") + "&pid=" + current_product_id,
	    dataType: "json",
	    success: function (msg) {
	        // create arrays related to images
	        jqzoom_images = [];
	        jqzoom_images_large = [];
	        jqzoom_images_thumb = [];
	        var max_small_height = 1;
	        for (var key in msg) {
	            var obj = msg[key];
	            for (var prop in obj) {
	                if (prop === "srcUrl") {
	                    the_path = obj[prop];
	                }
	                if (prop === "extraImgsCsv") {
	                    prop_arr = obj[prop].split(",");
	                    for (i = 0; i < prop_arr.length; i++) {
	                        if (prop_arr[i] != "undefined") {
	                            jqzoom_images_large[i] = prop_arr[i];
	                        } // if
	                    } // for
	                } else if (prop === "smallExtraImgsCsv") {
	                    prop_arr = obj[prop].split(",");
	                    for (i = 0; i < prop_arr.length; i++) {
	                        if (prop_arr[i] != "undefined") {
	                            jqzoom_images[i] = prop_arr[i];
	                            curr_small_image = the_path + prop_arr[i];
	                            $.post('image_size_checker.php',
	                            		{
	                    			image_to_check: curr_small_image
	                    		},
	                            		function(data) {
	                            	  var curr_small_image_size = data;
	                            	  if(curr_small_image_size > max_small_height){
	                            		  max_small_height_raw = curr_small_image_size;
	                            		  max_small_height = parseFloat(max_small_height_raw);
	              	                    $("#large_image_holder").css("height",max_small_height + 20 + "px");
	                            	  }
	                            	});
	                        } // if
	                    } // for
	                } else if (prop === "thumbnailExtraImgsCsv") {
	                    prop_arr = obj[prop].split(",");
	                    for (i = 0; i < prop_arr.length; i++) {
	                        if (prop_arr[i] != "undefined") {
	                            jqzoom_images_thumb[i] = prop_arr[i];
	                        } // if
	                    } // for
	                }
	            } // for
	        }
	
	        // render images
	        // render first zoom image
	        var zoom_image = "<a class='jqzoom' id='img_0'" + "href='" + the_path + jqzoom_images_large[0] + "' title='" + aa_product_name + "'><img src='" + the_path + jqzoom_images[0] + "'></a>";
	        $('#large_image_holder').append(zoom_image);
	        
			///////////////////////////////////
			
			// only show thumb images if there is more than one thumb
			if(jqzoom_images.length > 1) {
				// add hover message
				$(".thumb_img_msg").html("Hover Over Image to Enlarge");
				
			
			// render thumb images
	        for (i = 0; i < jqzoom_images.length; i++) {
	            var swatch_image = "<a class='img_" + i + "' href='#'><img src='" + the_path + jqzoom_images_thumb[i] + "'></a> ";
	            $('.thumb_list').append(swatch_image);
	        };
	        var curr_a_id = "img_0";
	        // initial highlighted and rollover effect for thumb images
	        $(".thumb_list a").attr("id", "thumb_not_selected");
	        $(".img_0").attr("id", "thumb_selected");
	        $(".thumb_list a").each(function (i) {
	            // add hover functionality to thumbnail images. Changes large image.
	            $(this).hover(function () {
	                thumb_class = $(this).attr('class');
	                if (curr_a_id == thumb_class) {
	                    return (false);
	                } else {
	                    $(".thumb_list a").attr("id", "thumb_not_selected");
	                    $(this).attr("id", "thumb_selected");
	                    function make_curr_a_id() {
	                        curr_a_id = thumb_class;
	                    }
	                    thumb_classx = document.getElementById(thumb_class);
	                    make_curr_a_id();
	                    var curr_index = thumb_class.split("_");
	                    var curr_index = curr_index[1];
	
	                    var zoom_image = "<a class='jqzoom' id='" + curr_a_id + "' href='" + the_path + jqzoom_images_large[curr_index] + "' title='" + aa_product_name + "'><img src='" + the_path + jqzoom_images[curr_index] + "'></a>";
	                    $('#large_image_holder').html(zoom_image);
	
	                    var options = {
	                        zoomWidth: 481,
	                        zoomHeight: 481,
	                        xOffset: 10,
	                        yOffset: -118,
	                        position: "right"
	                    };
	                    $('.jqzoom').jqzoom(options);
	
	                    return (false);
	                } //else
	            }); // hover
	            $(this).click(function () {
	                return false;
	            }); // click
	        }); // each
	        
	        
			} // if
	        ///////////////////////
	        
	        
	        // invoke the jqzoom plugin
	        var options = {
	            zoomWidth: 481,
	            zoomHeight: 481,
	            xOffset: 10,
	            yOffset: -118,
	            position: "right"
	        };
	        $('.jqzoom').jqzoom(options);
	
	        // preload images
	        // create array of all large images
			
	        jQuery.preloadImages = function () {
	            for (var i = 0; i < arguments.length; i++) {
	                jQuery("<img>").attr("src", arguments[i]);
	            }
	        }
	        for (i = 0; i < jqzoom_images.length; i++) {
	            $.preloadImages(the_path + jqzoom_images_large[i]);
	           
	        };
			for (i = 0; i < jqzoom_images.length; i++) {
	            $.preloadImages(the_path + jqzoom_images[i]);
	        };
			
			
	    } // success
	}); // ajax

} // if the current page is a product page



/////////////////////////////
/////////////////////////////


}); // ready
