/* prototype methods */
String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

/* global variables */
var current_popup_count = 0;

/* font apply */
Cufon.replace('div, span, li, a, h1, h2, h3, h4, h5, h6, p, table', { autoDetect:true, onAfterAllReplace:call_after_cufon_replace });

/* document ready */
$(document).ready(function() {
	
	// menu
	$("#btn_login").click(function(){
		show_popup("login_box");
	});
	
	// popup - login
	$("#login_box .close").click(function(){
		hide_popup("login_box");
	});
	
	// resize
	resize_contents();
	$(window).resize(function() {
	    resize_content();
	});
	
	$(".popup").click(function()
	{
		hide_popup($(this).attr("id"));
	});
});

/* popupbox */
function show_popup(id)
{
	if( $("#" + id).length == 1 )
	{
		if(current_popup_count == 0)
		{
			$(".popup_bg").show();
		}
		current_popup_count++;
		
		if ($.browser.msie  && parseInt($.browser.version) <= 7)
		{
			$("#" + id).show();
		}
		else
		{
			$("#" + id).fadeIn();
		}
	}
}

function hide_popup(id)
{
	if( $("#" + id).length == 1 )
	{
		if(current_popup_count == 0)
		{
			return;
		}
		else if(current_popup_count == 1)
		{
			$(".popup_bg").hide();
		}
		
		current_popup_count--;
		if ($.browser.msie  && parseInt($.browser.version) <= 7)
		{
			$("#" + id).hide();
		}
		else
		{
			$("#" + id).fadeOut();
		}
	}
}



/* common functions */
function resize_contents() {
	var new_height = window.innerHeight - $("#header").height() - $("#menu").height() - $("#footer").height();
	$("#contents").css("min-height", new_height + "px");
}

function call_after_cufon_replace()
{
	resize_content();
	if(typeof after_cufon_replace == 'function')
	{
		after_cufon_replace();
	}
}

function resize_content()
{
	$margin_bottom = 30;
	$ul = $("#contents .content_wrapper .content ul");
	$lis = $ul.find("li");
	$ul_new_height = 0;
	$lis.each(function(i) {
		if($(this).height() > $ul_new_height)
		{
			$ul_new_height = $(this).height();
		}
		if(i == $lis.length - 1)
		{
			$ul.height($ul_new_height + $margin_bottom);
		}
	});
	
}

function requests(func_name, callback, arg1, arg2, arg3, arg4, arg5, arg6)
{
	if(func_name.contains("/"))
	{
		var request_url = Config.web + func_name;
	}
	else
	{
		var request_url = Config.web + "requests/" + func_name;
	}
	if(typeof(arg1) != 'undefined') { request_url += "/" + arg1; }
	if(typeof(arg2) != 'undefined') { request_url += "/" + arg2; }
	if(typeof(arg3) != 'undefined') { request_url += "/" + arg3; }
	if(typeof(arg4) != 'undefined') { request_url += "/" + arg4; }
	if(typeof(arg5) != 'undefined') { request_url += "/" + arg5; }
	if(typeof(arg6) != 'undefined') { request_url += "/" + arg6; }
	$.getJSON(request_url, function(data) {
		callback(data);
	}).error(function(){
		callback(false);
	});
}

