//
(function($){
	var bgimg = new Image(), bgloaded = false, domready = false, bg = {}, api;
	var setBgImage = function(){
		$('#wrapper').before('<img id="bgimage" src="'+bgimg.src+'" />');
		bg.image = $('#bgimage');
		resizeBgImage();
		$(window).resize(resizeBgImage);
	};
	var resizeBgImage = function(){
		var screenSize = { width: $('html').width(), height: $(window).height()	};
		bg.image.width(screenSize.width).height(screenSize.height);
/*
		if (bg.width/bg.height < screenSize.width/screenSize.height) {
			bg.image.removeAttr('height').attr('width', screenSize.width);
		}
		else {
			bg.image.removeAttr('width').attr('height', screenSize.height);
		}
*/
	};
	var menuEffect = function(){
		var active;
		if ($('#tab-navi .current').length) {
			active = $('#tab-navi .current').attr('id').substr(4);
			$('#tab-navi').removeClass('active-1 active-2 active-3 active-4').addClass('active-'+active);
			$('#tab-navi li a').hover(function(){
				var h = $(this).parent().attr('id').substr(4);
				$('#tab-navi').removeClass('active-1 active-2 active-3 active-4').addClass('active-'+h);
			}, function(){
				$('#tab-navi').removeClass('active-1 active-2 active-3 active-4').addClass('active-'+active);
			})
		}
		else {
			$('#tab-navi li a').hover(function(){
				var h = $(this).parent().attr('id').substr(4);
				$('#tab-navi').removeClass('active-1 active-2 active-3 active-4').addClass('active-'+h);
			}, function(){
				$('#tab-navi').removeClass('active-1 active-2 active-3 active-4');
			})
		}
	};
	var setForm = function(){
		$('#document-modal #login-panel form').ajaxForm({
			dataType:'json',
			success:function(json){
				if (json.result) {
					location.href = '/partner';
				}
				else {
					$('#document-modal').html(json.html);
					setForm();
				}
			}
		});
	};
	var loginform = function(){
		var modal = '<div id="document-modal"></div>';
		$.getJSON('/partner/logincheck', function(json){
			if (json.result) {
				location.href = '/partner';
			}
			else {
				$('body').append(modal);
				$('#document-modal').dialog({
					title:'ログイン',
					width:300,
					height:220,
					open: function(){
						$('#document-modal').html(json.html);
						setForm();
					},
					modal:true,
					close:function(){
			            $('#document-modal').dialog('destory').empty().remove();
					},
					buttons: {
						'キャンセル':function(){
							$('#document-modal').dialog('destory').empty().remove();
						}
					}
				});
			}
		});
		return false;
	};
	$(bgimg).load(function(){
		bgloaded = true;
		bg.width = bgimg.width;
		bg.height = bgimg.height;
		if (domready) {
			setBgImage();
		}
	});
	bgimg.src = '/css/new/images/bgimage.jpg';
	$(function(){
		domready = true;
		$('#opening-replay-link').click(function(){
			$.cookie('ach', null);
			location.href = '/';
			return false;
		});
		$('#header').insertAfter('#container');
		menuEffect();
		if (bgloaded) {
			setBgImage();
		}
		$('#partner-link').click(loginform);
	});
})(jQuery);//

