﻿var FixWidthOfImages = function(maxHeight) {
    //var currentTitle = getTitle();
    $(".udt_images").each(function() {
        //*
		var udtImgs=this;
		var t = new Image();
		t.id = '_' + udtImgs.id;
		
		$(udtImgs).load(function () {
            $(this).css('display','none'); // since .hide() failed in safari
            $(this).fadeIn();

            //add alt message
            /*
            if($(this).attr("title") == ""){
                $(this).attr("title", currentTitle);
            }
            */
            
//			console.log(this.id+' loaded, wd='+this.width);
        }).error(function () {

        }).attr('src', udtImgs.src);

        $(t).load(function() {
            toheight = maxHeight;
            towidth = 102;
            $(this).css('display','none'); // since .hide() failed in safari
            
            var t1 = this;

            var aWidth = Math.round(t1.width / towidth);
            var aHeight = Math.round(t1.height / toheight);
            
            if (aWidth < aHeight)
            {
                toheight = maxHeight;
                towidth = Math.round(t1.width * maxHeight / t1.height);
            }
            else
            {
                towidth = 102;
                toheight = Math.round(t1.height * 102 / t1.width);
            }      
            
			$(udtImgs).css("cursor", "pointer");
			$(udtImgs).css("width", towidth);
			$(udtImgs).css("height", toheight);
			$(udtImgs).click(function() {
			    showLightBox(this.id, t1.height + 50, t1.width + 40, udtImgs.src, udtImgs.title, $(udtImgs).attr("mid"));
			});

        }).error(function () {

        }).attr('src', udtImgs.src);
		
    });
}

var showLightBox = function(aId, height, width, src, sTitle, mid) {
    height = height > 300 ? height : 300;
    width = width > 450 ? width : 450;

    var _height = height - 50;
    var _width = width - 40;
    var html = '';

    var _sId = aId.split("_")[1];

    html += '<div style="overflow:hidden;text-align:center;" id="dialog_' + aId + '" height="' + height + '" width="' + width + '">';
    //<img id="dialogImage" src="' + src + '" border="0" title="' + sTitle + '" />';
    
    html += '</div>';
    html += '<iframe id="iframe_' + aId + '" src="' + targetUrl + '?sId=' + _sId + '&moduleid=' + mid + '" height="200" width="200" style="display:none;"></iframe>';



    $(html).appendTo($("body"));

    $('#dialog_' + aId).dialog({
    //$(html).dialog({
        title: sTitle,
        'height': height,
        'width': width,
        modal: true,
        posotion: "center",
        resizable: false,
        close: function(event, ui) {
            $('#dialog_' + aId).remove();
            $('#iframe_' + aId).remove();
        }
    });    
    
    $('#dialog_' + aId).dialog('option', 'height', height+50);
    $('#dialog_' + aId).html('<img id="dialogImage" src="' + src + '" border="0" title="' + sTitle + '" />')
    
    $('#dialogImage').load(function () {
            $(this).css('display','none'); // since .hide() failed in safari
            $(this).fadeIn();
            $('#dialog_' + aId).css('height',this.height);
    }).error(function () {

    }).attr('src', src);
}

function getTitle (sdf){
	/*
    var str = document.location.href;
    var strArray = str.split('/');

    var oTitle = strArray[strArray.length - 2];
    oTitle = "8-inch-fishing-lures";
    var s = oTitle.replace("-"," ");
    */
    //var s = document.getElementsByTagName("title")[0].innerHTML;
    //return s;
    //return sdf;
}

