$(document).ready(function () {

// fix navtab spacing
var the_number_of_tabs = $(".navbar tr:first td").length;

// var navtab_body_width = $(".navtab_body").outerWidth();

var navtab_body_width = 485;

$(".navbar tr:first td a").css({
	width: "96px"
});

// add "navtab last" class if missing

if($(".navtab_last").length == 0 && the_number_of_tabs > 1) {
	$(".navtab_middle:last").attr("class","navtab_last");
} else if(the_number_of_tabs == 1) {
	$(".navtab_first").attr("class","navtab_last");
}


// var single_tab_width = $(".navbar tr:first td").eq(0).outerWidth(true);
var single_tab_width = 96;
var all_tab_width = single_tab_width * the_number_of_tabs;

// set the length of the last tab
// purpose is to remove the space between tabs
var new_navtab_last_width = navtab_body_width - all_tab_width + "px";

// console.log("new_navtab_last_width: " + new_navtab_last_width + ", navtab_body_width: " + navtab_body_width + ", all_tab_width: " + all_tab_width );

$(".navtab_last a").css("margin-right",new_navtab_last_width);

    $.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 = [];

            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" && prop_arr[i] != "") {
                                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];
                            } // 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
            }
            
            // determine if JQZoom should be used
            // Only use JQZoom if the large image is at least 1000px wide            
            // ajax to viking_extra_image_size.php page. argument includes image url
            // php page "viking_extra_image_size.php" has curl request to grab image size

			
			if(jqzoom_images_large.length > 0){
				vb_large_image_url = the_path + jqzoom_images_large[0];
			}
			
            $.post( "viking_extra_image_size.php", 
            		{
            			image_to_check: vb_large_image_url
            		},
            		function(data){
            	  		use_jq_zoom = data;
					 if(use_jq_zoom != "yes"){
						$(".thumb_list_message").hide();
					}
						
            if(vb_large_image_url == '') {
            	// add "missing" image
            	$('#large_image_holder').html("<img src='images_templ/noImg_available_big.jpg' alt='' border=''>");
            } else {
            // render images
            // change href attribute of static, big image
           var first_zoom_image = the_path + jqzoom_images_large[0];
          
          if(use_jq_zoom == "yes"){
        	  $("#img_0").attr("href",first_zoom_image);
        	  $("#img_0").attr("title",vb_product_name);
          } //use_jq_zoom
          
            // 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);
            };
			$('.thumb_list').append('<br clear="all">');
            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];
                        if(use_jq_zoom == "yes"){
                        		var zoom_image = "<a class='jqzoom' id='" + curr_a_id + "' href='" + the_path + jqzoom_images_large[curr_index] + "' title='" + vb_product_name + "'><img src='" + the_path + jqzoom_images[curr_index] + "'></a>";
                        } else {
                    		    var zoom_image = "<a class='jqzoom' id='" + curr_a_id + "' title='" + vb_product_name + "'><img src='" + the_path + jqzoom_images[curr_index] + "'></a>";
                        } // if_use_jq_zoom
                        
                        $('#large_image_holder').html(zoom_image);
                        if(use_jq_zoom == "yes"){ 
                        var options = {
                            zoomWidth: 426,
                            zoomHeight: 426,
                            xOffset: 7,
                            yOffset: 0,
                            position: "right",
                            title: false
                        };
                        $('.jqzoom').jqzoom(options);
                        }// if use_jq_zoom

                        return (false);
                    } //else
                }); // hover
                $(this).click(function () {
                    return false;
                }); // click
            }); // each
            // invoke the jqzoom plugin - disabled
            if(use_jq_zoom == "yes"){
			var options = {
                zoomWidth: 426,
                zoomHeight: 426,
                xOffset: 7,
                yOffset: 0,
                position: "right",
                title: false
            };
            $('.jqzoom').jqzoom(options);
            } // if use_jq_zoom
            // 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]);
                }
            }
			
			// preload first large extra image
			
			$.preloadImages(the_path + jqzoom_images_large[0]);
			
			$("#thumb_list_wrapper").one("mouseover",function(){
			
				// preload rest of large extra images
			
				for (i = 1; i < jqzoom_images.length; i++) {
					$.preloadImages(the_path + jqzoom_images_large[i]);
				};
				
				// preload small extra images
			
				for (i = 0; i < jqzoom_images.length; i++) {
					$.preloadImages(the_path + jqzoom_images[i]);
				};
				
			});
			
			
// add the scroll arrows

 $(".thumb_list").arrow_scroll_custom( {  
        number_visible_items: 7,
        scroll_items_width_manual: 58,
        is_product_template: false
  });
  
// simulate mouseover the first thumb image

$('.thumb_list a').eq(0).mouseover();
			
            } // if
          }); // ajax post 
        } // success
    }); // ajax
 

}); // ready
