var winH, winW;
winH = $(window).height();
winW = $(window).width();

function getCookie(c_name) {
	if (document.cookie.length>0) {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1) {
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function checkCookie(current_page) {
	username=getCookie('alchemy_cache_'+current_page);
	if (username!=null && username!="") {
		//$('#loading').css({'display':'none'});
		//$('#body').css({'display':'block'});
	}
	else {
		setCookie('alchemy_cache_'+current_page,'cached',1);
		//$('#loading').css({'display':'block'});
		//$('#body').css({'display':'none'});
	}
}

function rePosition() {
			//$('#loading').css({'display':'none'});
			$('#body').css({'display':'block'});
			//$('#body').animate({"opacity": "show"}, "fast", "linear");

			var texture_left_bg_OH = $('#texture_left_bg').attr('height');
			var texture_left_bg_OW = $('#texture_left_bg').attr('width');
			var texture_left_bg_NW = texture_left_bg_OW * winH / texture_left_bg_OH;

			var texture_right_bg_OH = $('#texture_right_bg').attr('height');
			var texture_right_bg_OW = $('#texture_right_bg').attr('width');
			var texture_right_bg_NW = texture_right_bg_OW * winH / texture_right_bg_OH;

			var texture_main_bg_OH = $('#texture_main_bg').attr('height');
			var texture_main_bg_OW = $('#texture_main_bg').attr('width');
			var texture_main_bg_NH = texture_main_bg_OH * winW / texture_main_bg_OW;

			$('#texture_left_bg').attr({'height':winH});
			$('#texture_left_bg').attr({'width':texture_left_bg_NW});
			
			$('#texture_right_bg').attr({'height':winH});
			$('#texture_right_bg').attr({'width':texture_right_bg_NW});
			
			$('#texture_main_bg').attr({'width':winW});
			$('#texture_main_bg').attr({'height':texture_main_bg_NH});
			
			$('#content_main').width(Math.round((winW - texture_left_bg_NW - texture_right_bg_NW) * 1.0));
			$('#page_logo').width(winW - texture_left_bg_NW - texture_right_bg_NW);
			
			$('#'+current_page).attr({'class':'page_menu_links_selected'});
			
			$('#body').animate({"opacity": "show"}, "slow", "linear");
}

function stringConvert() {
	$('.string_convert').each(function(){
		var result = '';
		var target = $(this).html();
		var target_array = target.split('');
		
		for (i=0;i<target_array.length;i++) {
			if(target_array[i] == '$'){target_array[i]='<img src="images/number_sign/dollar.png" />';}
			else if(target_array[i] == '0'){target_array[i]='<img src="images/number_sign/0.png" />';}
			else if(target_array[i] == '1'){target_array[i]='<img src="images/number_sign/1.png" />';}
			else if(target_array[i] == '2'){target_array[i]='<img src="images/number_sign/2.png" />';}
			else if(target_array[i] == '3'){target_array[i]='<img src="images/number_sign/3.png" />';}
			else if(target_array[i] == '4'){target_array[i]='<img src="images/number_sign/4.png" />';}
			else if(target_array[i] == '5'){target_array[i]='<img src="images/number_sign/5.png" />';}
			else if(target_array[i] == '6'){target_array[i]='<img src="images/number_sign/6.png" />';}
			else if(target_array[i] == '7'){target_array[i]='<img src="images/number_sign/7.png" />';}
			else if(target_array[i] == '8'){target_array[i]='<img src="images/number_sign/8.png" />';}
			else if(target_array[i] == '9'){target_array[i]='<img src="images/number_sign/9.png" />';}
			else if(target_array[i] == '.'){target_array[i]='<img src="images/number_sign/fullstop.png" />';}
			else {target_array[i]=target_array[i];}
			result += target_array[i];
		}
		$(this).html(result);
		//$.each(target.split(' '), function(key, value) { 
			//alert(key + ': ' + value); 
		//});
		
	});
}

$(document).ready(function() {
	//set bg W and H
    var imgTotal = $('img').size();
	var imgCount = 0;
	
	$('img').one('load',function(){
		imgCount++;
		if (imgCount == imgTotal) {
			rePosition();
			checkCookie(current_page);
			//$('#loading').animate({"opacity": "hide"}, "fast", "linear", function(){rePosition()});
			stringConvert();
			
		}
	})
	.each(function(){
	if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
	$(this).trigger('load');
	});
	
});


/*
$(document).bind({
  ready: function() {
    var imgTotal = $('img').size();
	var imgCount = 0;
	
	$('img').one('load',function(){
		imgCount++;
		if (imgCount == imgTotal) {
			rePosition();
		}
	})
	.each(function(){
	if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
	$(this).trigger('load');
	});
  },
  resize: function() { alert('resize');
    rePosition();
  }
});
*/

