var now_ajax_load_page =document.URL;
var now_ajax_load_page_start ='no';
var my_site_place;
function ajax_link(){
	if ( now_ajax_load_page_start =='no' ){
		$.historyInit(pageload);
		now_ajax_load_page_start ='ok'
	}
	$(".ajax").each(function(i){
		if( $(this).attr("mdajax") !=='ok'){
			$(this).attr("mdajax","ok");
			$(this).click(function (){
				$.historyLoad(this.href);
				return false;
			})
		}
	});
}
function pageload(url){
	if( url == '' ){
		url = document.URL
	} 
	if ( now_ajax_load_page !==url  ){
		page_load(url)
		now_ajax_load_page = url;
	}
}	
	
	
function page_load(url){
	if ((i = url.indexOf("?")) > 0) {
		ajax_url  = url+"&class=ajax";	
	}else{
		ajax_url  = url+"?class=ajax";
	}
	do_ajax( "GET" , ajax_url , "" , "md_main" , "body" , "ok" , "normal", "fade" )
}
function do_ajax( ajax_method , ajax_url , ajax_data , id_for_show , id_for_loading , is_loading_need , loading_type, onsuccess ){
	$.ajax({ 
	  type: ajax_method, 
	  url: ajax_url, 
	  data: ajax_data ,
	  beforeSend : function( msg ){
		if ( is_loading_need =='ok' ){
			do_load( id_for_loading  , 'on' ,loading_type )
		}
	  },
	  success :  function( msg ){ 
		if ( onsuccess =='write' ){
			$('#'+id_for_show).show();
			$('#'+id_for_show).html(msg);
		}else if ( onsuccess =='fade' ){
			$('#'+id_for_show).hide();
			$('#'+id_for_show).html(msg);
			$('#'+id_for_show).slideDown(3000);
			//$("#md_main").height(480);
			$("#md_main").jScrollPane({showArrows:true, scrollbarWidth: 15, arrowSize: 16});
			ajax_link()
		}
		if ( is_loading_need =='ok' ){
			do_load( id_for_loading  , 'off' ,loading_type )
		}
	  } 
	});

}
var first_loading = new Array;
function do_load( id , mode , type){
	if ( mode =='on' ){
		pageSize = new Array;
		if (id =='body' ){
			pageSize['w'] = $(window).width();
			pageSize['h'] = $(document).height();
			pageSize['t'] = 0;
			pageSize['l'] = 0;
			m_app= 'body';
		}else{
			m_app= '#'+id;
			me = $(m_app).position('top')
			pageSize['w'] = $(m_app).width();
			pageSize['h'] = $(m_app).height();
			pageSize['t'] = me.top;
			pageSize['l'] = me.left;
		}
		if ( ! first_loading[id]  ) {
			$(m_app).append('<div id="lightbox-overlay_'+id+'" class="overal"></div>');
			overlay = $('#lightbox-overlay_'+id)
			overlay.click( function() { $('#lightbox-overlay_'+id).animate({ height: 'hide', opacity: 'hide' }, "slow") }  )
			first_loading[id] ='ok';
		}else{
			overlay = $('#lightbox-overlay_'+id)
		}
		overlay.css({
			position	: 'absolute',
			display		: 'none',
			top			: pageSize.t,
			left		: pageSize.l,
			zIndex		: 100,
			opacity		: 0,
			height : pageSize.h + 'px' ,
			width :pageSize.w+ 'px'
			})
			.fadeTo(1 , 0.6)
			.animate({ height: 'show'}, "slow");
		}else{
			$('#lightbox-overlay_'+id).animate({ height: 'hide', opacity: 'hide' }, "slow")
		}
}
var js_load = new Array;
function include_once_js(url){
	if( ! js_load[url] ){
		//$.getScript(url)
		head = document.getElementsByTagName("head")[0];
		script = document.createElement("script");
		script.type = "text/javascript";
		script.src = url;
		head.appendChild( script );
		js_load[url]='ok';
	}
}
function get_value_for_ajax(id){
	if($("#"+id).attr('type') == 'checkbox'){
        return $("input[@id='"+id+"']").is(":checked")
	}else if ( $("#"+id).attr('class') =='inputprice' ){
		return removecomma(id);
	}else{
	
        return encodeURIComponent($("#"+id).val());
	}
}
function change_list_class(){
	$(".list").each(function(i){ 
	  this.className= "list_" + (i%2) ; 
	});
}
function my_go(url){
setTimeout('document.location.href="'+decodeURIComponent(url)+'"',100 )
}
function select_user_value( myid , myvalue){
	$("#"+myid+" option").each(function(i){
			if ( this.value == myvalue ){
				this.selected = true
			}
		}
	);
}
function hide_id_link(id){
	$("#"+id).find("a").each(function(i){ 
		this.style.display ='none';
	});
}
function moneycommasep(ctrl){
  var separator = ",";
  var int = ctrl.value.replace ( new RegExp ( separator, "g" ), "" );
  var regexp = new RegExp ( "\\B(\\d{3})(" + separator + "|$)" );
  do
  {
    int = int.replace ( regexp, separator + "$1" );
  }
  while ( int.search ( regexp ) >= 0 )
  ctrl.value = int;
}

function removecomma(ctrl){
  var separator = ",";
  removed = document.getElementById(ctrl).value.replace ( new RegExp ( separator, "g" ), "" );
  return removed;
}
$(document).ready(function() {
	if ( my_site_place !=='sys' ){
		ajax_link()
		$("#md_main").jScrollPane({showArrows:true, scrollbarWidth: 15, arrowSize: 16});
	}
});

