﻿$(function() {
        if ($('#wdgShowMedia_image'))
        {
            $(".wdgShowMedia_thumb").jScale({ls:'100px'});
            $('#wdgShowMedia_image').hide();
            $("#wdgShowMedia_panel").attr('src',$(".wdgShowMedia_image:first").attr("rel"));
            $("#wdgShowMedia_panel").attr('maxi',$(".wdgShowMedia_image:first").attr("maxi"));
            
            $("#wdgShowMedia_panel").click(function () {
                if (typeof(DownloadFile) == 'function')
                {
                    if ($(this).attr('maxi') != '')
                        DownloadFile($(this).attr('maxi'));
                }
            });
            
            $(".wdgShowMedia_thumb:first").removeClass("wdgShowMedia_blurImage");
            $(".wdgShowMedia_thumb:first").addClass("wdgShowMedia_unblurImage");
            
            var img = new Image();
            img.src = $(".wdgShowMedia_image:first").attr("rel");



            if ($.browser.safari)
                var retObj = getHeightWidth($("#wdgShowMedia_panel").width, $("#wdgShowMedia_panel").height, 500, 300);
            else
                var retObj = getHeightWidth(img.width, img.height, 500, 300);




            $("#wdgShowMedia_panel").jScale({w:retObj.width,h:retObj.height});
            $("#wdgShowMedia_panel").css('margin-top', '0px');

            $('#wdgShowMedia_image').show();
            
            $(".wdgShowMedia_image").click(function() {
                    var image = $(this).attr("rel");
                    var maxi = $(this).attr("maxi");
                    $('#wdgShowMedia_image').hide();
                    $(".wdgShowMedia_thumb").addClass("wdgShowMedia_blurImage");
                    $(".wdgShowMedia_thumb").removeClass("wdgShowMedia_unblurImage");
                    $(this).children(".wdgShowMedia_thumb").removeClass("wdgShowMedia_blurImage");
                    $(this).children(".wdgShowMedia_thumb").addClass("wdgShowMedia_unblurImage");
                    
                    $("#wdgShowMedia_panel").attr('src',image);
                    $("#wdgShowMedia_panel").attr('maxi',maxi);
                    
                    var img = new Image();
                    img.src = $(this).attr("rel");



                    if ($.browser.safari)
                        var retObj = getHeightWidth($("#wdgShowMedia_panel").width, $("#wdgShowMedia_panel").height, 500, 300);
                    else
                        var retObj = getHeightWidth(img.width, img.height, 500, 300);
                    
                    
                    $("#wdgShowMedia_panel").jScale({w:retObj.width,h:retObj.height});
                    $("#wdgShowMedia_panel").css('margin-top', '0px');

                    $('#wdgShowMedia_image').show();

                    if (typeof(wdgShowDownloadImageHandler) == 'function')
                    {
                        wdgShowDownloadImageHandler($(this).attr("order"));
                    }
                    return false;
	            });
	            
	        if ($('#wdgShowMedia_carousel'))
	        {
	            if ($('.wdgShowMedia_image').children(".wdgShowMedia_thumb").length > 1)
	            {
	                $('#wdgShowMedia_carousel').show();
	            }
	        }
    	        
	        $('#wdgShowMedia_mycarousel').jcarousel();
	        
	     }
    });
    
    
    function getHeightWidth (imgWidth, imgHeight, maxWidth, maxHeight)
    {
        var ret = [{width:'0px',height:'0px',hasBorderTop:false}];
        
        ret.width = maxWidth + 'px';
        ret.height = maxHeight + 'px';
        if (imgWidth != 0 && imgHeight != 0)
        {
            if ((imgWidth/imgHeight) > 1)
            {
                if ((maxWidth/(imgWidth/imgHeight)) > maxHeight)
                {
                    ret.height = maxHeight + 'px';
                    ret.width = Math.floor((maxHeight/(imgHeight/imgWidth))) + 'px';
                    ret.hasBorderTop = false;
                }
                else
                {
                    ret.width = maxWidth + 'px';
                    ret.height = Math.floor((maxWidth/(imgWidth/imgHeight))) + 'px';
                    ret.hasBorderTop = true;
                }
            }
            else
            {
                if ((maxHeight/(imgHeight/imgWidth)) > maxWidth)
                {
                    ret.width = maxWidth + 'px';
                    ret.height = Math.floor((maxWidth/(imgWidth/imgHeight))) + 'px';
                    ret.hasBorderTop = true;
                }
                else
                {
                    ret.height = maxHeight + 'px';
                    ret.width = Math.floor((maxHeight/(imgHeight/imgWidth))) + 'px';
                    ret.hasBorderTop = false;
                }
            }
        }
        
        return ret;
    
    }
