/**
 * iPhone / Andorid などいわゆるスマートフォン向けに最適化するスクリプト
 *
 * jQuery に依存
 * 元々の横幅が画面幅より大きい場合は100%にする
 * 元々の横幅が画面幅より小さい場合は元々の横幅にする
 */

$(document).ready(function(){init(adaptWidth);});
window.onresize = function(){adaptWidth();}

// ↓jQuery.data(); に変更できない？
// 要素の元サイズを格納
function init(func){
	$("body *").each(function() {
        $(this).attr("data-orginal-width",$(this).width());
        $(this).attr("data-orginal-height",$(this).height());
	});
	func();
}

// リサイズ処理
function adaptWidth(){
    var window_width = $(window).width();
    $("body *:not(img)").each(function() {
        var orginal_width = $(this).attr("data-orginal-width");
        if(window_width < orginal_width && window_width < 640){
	        $(this).css({maxWidth:"100%"});
	        $(this).css({width:"auto"});
	        $(this).css({minWidth:"0"});
        }else{
	        $(this).css({maxWidth:""});
	        $(this).css({width:""});
	        $(this).css({minWidth:""});
        }
	});
    $("img").each(function() {
        var orginal_width = $(this).attr("data-orginal-width");
        var orginal_height = $(this).attr("data-orginal-height");
        if(window_width < orginal_width && window_width < 640){
	        $(this).css({maxWidth:"100%"});
	        $(this).css({width:"auto"});
	        $(this).css({minWidth:"0"});
	        var new_height = orginal_height * $(this).width() / orginal_width;
	        $(this).css({maxHeight: new_height});
	        $(this).css({height: new_height});
	        $(this).css({minHeight: new_height});
        }else{
	        $(this).css({maxWidth:""});
	        $(this).css({width:""});
	        $(this).css({minWidth:""});
	        $(this).css({maxHeight:""});
	        $(this).css({height:""});
	        $(this).css({minHeight:""});
        }
	});
}

