var sizeSET = { "large" : "400",
				"medium": "300",
				"small" : "200",
				"thumb" : "100"
				};

//==============================================================================================================================
function loadImageBox( PARENTCOLUMNSET ){
    $('div.insImg').each(
	    function(){
	    	var id = $(this).attr('id') ;
	    	var caption = $(this).attr('caption');
	    	var source = $(this).attr('src');
	    	var size = $(this).attr('size');
	    	var position = $(this).attr('position');
	    	var mode = $(this).attr('mode');
	    	var imginfo = setImageSize( source, size ); 
	    	$(this).addClass(position);
	    	if(mode == 'thumb'){
	    		var thumbinfo = setImageSize( source, "thumb");
    			var html  = '<div class="thumbBox '+position+'" >';
    				html += 	'<a href="#" onclick="loadImageSubwin(\''+source+'\', \''+size+'\', \''+caption+'\' )" > ';
	    			html += 		'<img src="'+ImageURL+'/'+source+'" alt="'+caption+'" width="'+thumbinfo.width+'" height="'+thumbinfo.height+'" />';
	    			html += 	'</a>';
	    			html += 	'<span style="display:block" >';
	    			html += 		'<a href="#" onclick="loadImageSubwin(\''+source+'\', \''+size+'\', \''+caption+'\' )" > ';
	    			html += 			caption;
	    			html += 		'</a>';
	    			html += 	'</span>';
	    			html += '</div>';
	    			
	    		$(this).empty();
   		    	$(this).append(html);
    			$('#'+id+' .thumbBox').width(thumbinfo.width);
    			if( position != 'center' ){
    				$(this).width(thumbinfo.width + 10);
    			}

	    	}
	    	else if(mode =='full'){
		    	$(this).addClass(size);

		    	var html  = '<div id="'+id+'_imgpanel" class="'+position+'">';
		    		html += 	'<img src="'+ImageURL+'/'+source+'" alt="'+caption+'" width="'+imginfo.width+'" height="'+imginfo.height+'" />';
    				html += 	'<span style="display:block" >'+caption+'</span>';
    				html += '</div>';
    			
    			$(this).empty();
    			$(this).append(html);
    			$('#'+id+'_imgpanel').width(imginfo.width);

    			if(PARENTCOLUMNSET != undefined ){
//    	alert( $('#'+PARENTCOLUMNSET.parent).height() );
    			}
    		
    			if( position != 'center' ){
    				$(this).width(imginfo.width + 10);
    			}
    			else if(position == 'right'){
    				$(this).css('float', 'right');
    			}
  			
	    	
	    	}
	    }
	);

}

//========================= initialize Page ===============================================================
function setImageSize( FILE , SIZE){
	var imageSize = callPhotoSize(FILE);
	var width = parseInt(imageSize.width);
	var height = parseInt(imageSize.height);
	var standardSize = parseInt( sizeSET[SIZE] );
	
	if(width > height){
		var WIDTH = standardSize;
		var HEIGHT = standardSize * height / width; 
	}
	else{
		var HEIGHT = standardSize;
		var WIDTH = standardSize * width / height; 
	}
	var result= { "width" : WIDTH, "height" : HEIGHT };
	return result;
	
}

//========================= initialize Page ===============================================================
function  callPhotoSize(FILE ){
	var URL = CMSROOTURL+"/server/deliverPhotoSize.php";
	
	var PARAM = {"file": FILE };
	
	var response = $.ajax({
		url: URL,
		data: PARAM,
		type: 'POST',
		contentType: "application/x-www-form-urlencoded",
		cache: false,
		async:false,
		error: function(r, t, e){
			alert("イメージサイズ取得エラー" );
	    	$("#confirm").attr('disabled', false);
	    	
	    	result =  false;

		}

    });
	
	var result = response.responseText;
	var data = eval( '('+decodeURIComponent(result)+')' );
	
	return data;
	

}

//========================= initialize Page ===============================================================
function  loadImageSubwin( FILE, SIZE, CAPTION ){
	var imginfo = setImageSize( FILE, SIZE ); 
	var html  = '<html>';
		html += '<head>';
		html += '<script type="text/javascript" src="'+JSROOTURL+'/jquery-1.3.2.min.js" ></script>';
		html += '<script type="text/javascript" src="'+JSROOTURL+'/jquery-ui-1.7.2.custom.min.js" ></script>';
		html += '</head>';
		html += '<body>';
		html += '<div class="imageSubwin">';
		html += '<img src="'+ImageURL+'/'+FILE+'" width="'+imginfo.width+'" height="'+imginfo.height+'" alt="'+CAPTION+'" />';
		html += '<p>'+CAPTION+'</p>';
		html += '<button id="CloseButton">閉じる</button>';
		html += '</div>';
		html += '<script type="text/javascript">';
		html += '<!--';
		html += '$("#CloseButton").click(';
		html += 	'function(){';
		html += 		'alert("HI");';
		html += 		'window.close();';
		html += 	'}';
		html += ');';
		html += '//-->';
		html += '</script>';
		html += '</body>';
		html += '</html>';
	
	var winWidth = parseInt(imginfo.width)+50;
	var winHeight = parseInt(imginfo.height)+50;
	imagesubWin = window.open( '', 'imagesubWin', 'width='+ winWidth +', height='+ winHeight
									+', status=0, directories=0, resizable=1,scrollbars=1,menubar=0, toolbar=0,titlebar=0' );
	imagesubWin.document.write(html);
	

}

