var _title = "iida calling ver.4.0 | iida";

var column = -1;
var margin = 50;

var isOpen = false;

function icallInit(){
	swfobject.switchOffAutoHideShow();
	
	
	var date = new Date();
	var temp = date.getTime();
	
	var flashvars = {};
	flashvars["temp"] = temp;
	flashvars["browser"] = getSystem();
	
	if(location.hash.length > 1){
		var hash = location.hash.substr(1);
		var trackNum = hash;
		if(trackNum.length > 0){
			flashvars["trackNum"] = trackNum;
			
			var a = trackNum.split("t");
			if (Number(a[0]) <= 382803)
			{
				window.location = "http://iida.jp/calling3/#" + trackNum;
			}
		}
	}
	
	var params = {
		wmode : "transparent",
		allowScriptaccess : "sameDomain"
	};
	
	swfobject.embedSWF("assets/DummyForm.swf", "dummy", "100%", "100%", "10.0.22", "", flashvars, params, null);
	
	swfobject.embedSWF("InputForm.swf", "externalFormContainer", "100%", "100%", "10.0.22", "", flashvars, null);
	
	
	swfobject.addLoadEvent(function(){
		
	});
	
	swfobject.addDomLoadEvent(function(){
		swfobject.embedSWF("NewSongsMain.swf", "newSongs", "355px", "100%", "10.0.22", "", flashvars, params);
		swfobject.embedSWF("OnTwitterMain.swf", "onTwitter", "355px", "100%", "10.0.22", "", flashvars, params);
		swfobject.embedSWF("BannerAreaMain.swf", "bannerArea", "188px", "100%", "10.0.22", "", flashvars, params);
									   
		swfobject.embedSWF("SearchMain.swf", "search", "896px", "100%", "10.0.22", "", flashvars, params);
		
		swfobject.embedSWF("Help.swf", "help", "896px", "100%", "10.0.22", "", null, params);
		/*swfobject.embedSWF("Award.swf", "award", "896px", "100%", "10.0.22", "", flashvars, params);*/
		
		
		resizeHandler();
		
		if(swfobject.hasFlashPlayerVersion("10.0.22")){
			$("#footer").css("display", "block");
			$("#footerNoFlash").css("display", "none");
			
			showHome();
			titleCheck();
		}else{
			$(document).ready(function(){
			 	setNoFlash();
			});
		}
	});
	
	
	if($.browser.msie){
		setInterval(titleCheck, 50);
	}
}

/*====================================================
 browserCheck
=====================================================*/


function browserCheck(){
	this.userAgent = navigator.userAgent;
	this.Win = (this.userAgent.indexOf('Win',0) != -1);
	this.Mac = (this.userAgent.indexOf('Mac',0) != -1);
	
	if($.browser.msie){
		this.IE = true;
	}else if($.browser.mozilla){
		this.FF = true;
	}else if($.browser.safari){
		this.SF = true;
	}else{
		this.OTH = true;
	}
}

function getSystem(){
	var bc = new browserCheck();
	
	var OS;
	var browser;
	
	if(bc.Mac == true){
		OS = "MAC";
	}else if(bc.Win == true){
		OS = "WIN";
	}
	
	if(bc.IE == true){
		browser = "IE";
	}else if(bc.FF == true){
		browser = "FF";
	}else if(bc.SF == true){
		browser = "SF"
	}else{
		browser = "OTH";
	}
	
	return OS + browser;
}


/*====================================================
 titleCheck
=====================================================*/

function titleCheck(){
	if(document.title != _title){
		document.title = _title;
	}
}


/*====================================================
 setNoFlash
=====================================================*/

function setNoFlash(){
	$("html").css("height", "100%");
	$("body").css("height", "100%");
	
	$("#footer").css("display", "none");
	
	$("#noflash").css("visibility", "visible");
}

/*====================================================
 setHeaderHight
=====================================================*/

function setHeaderHight(hight){
	var h = hight + "px";
	$("#header").css("height", h);
	
	if(h == "510px"){
		if($("#dummy").css("marginTop") == "1px"){
			$("#dummy").css("marginTop", "0px");
		}
	}
	
	if($.browser.msie && $.browser.version < 7){
		$("#externalFormContainer").css("height", h);
	}else if($.browser.safari && navigator.userAgent.indexOf('Mac OS X 10_6', 0) != -1){
		$('html,body').animate({ scrollTop: 1 }, 0, "normal", slideBack);
			
		
	}
}

function slideBack(){
	$('html,body').animate({ scrollTop: 0 }, 0);
}

/*====================================================
 initMaxHomeHeight
=====================================================*/

var maxHomeHeight = 0;
var newSongsHeight   = 0;
var onTwitterHeight  = 0;
var bannerAreaHeight = 0;
var searchHeight     = 0;

function initMaxHomeHeight(){
	
	var h;
	
	if (newSongsHeight < onTwitterHeight){
		if (bannerAreaHeight < onTwitterHeight)		h = onTwitterHeight;
		else										h = bannerAreaHeight;
	} else {
		if (bannerAreaHeight < newSongsHeight)		h = newSongsHeight;
		else										h = bannerAreaHeight;
	}
	
	maxHomeHeight = h;
	
	if($("#columnContainer").css("height") == "0px") return;
	
	if (newSongsHeight != 0 && onTwitterHeight != 0 && bannerAreaHeight != 0)
		$("#columnContainer").css("height", maxHomeHeight);
}

function setNewSongsHeight(height){
	
	newSongsHeight = height;
	initMaxHomeHeight();
	
	if($.browser.msie && $.browser.version < 7){
		$("#newSongs").css("height", newSongsHeight);
	}
}
function setOnTwitterHeight(height){
	
	onTwitterHeight = height;
	initMaxHomeHeight();
	
	if($.browser.msie && $.browser.version < 7){
		$("#onTwitter").css("height", onTwitterHeight);
	}
}
function setBannerAreaHeight(height){
	
	bannerAreaHeight = height;
	initMaxHomeHeight();
	
	if($.browser.msie && $.browser.version < 7){
		$("#bannerArea").css("height", bannerAreaHeight);
	}
}
function setSearchHeight(height){
	
	searchHeight = height;
	
	if($("#searchContainer").css("height") == "0px") return;
	$("#searchContainer").css("height", searchHeight);
	
	if($.browser.msie && $.browser.version < 7){
		var arry = $("#searchContainer").children();
		var len = arry.length;
		for(var i = 0; i < len; i++){
			var child = arry[i];
			child.style.height = height + "px";
		}
	}
}

/*====================================================
 Resize
=====================================================*/

if(window.addEventListener)		window.addEventListener('resize', resizeHandler, false);
else if(window.attachEvent)	window.attachEvent('onresize', resizeHandler);
else							window.onresize = resizeHandler;

function resizeHandler(){
	
	var clWidth;
	
	if(document.documentElement.clientWidth != undefined)	clWidth = document.documentElement.clientWidth;
	else if(document.body.clientWidth != undefined)		clWidth = document.body.clientWidth;
	else													clWidth = window.innerWidth;
	
	if ( 896 < clWidth )
	{
		$("#header").css("width", "100%");
		/*$("#header").css("margin", "0");*/
	}
	else
	{
		$("#header").css("width", "896px");
		/*$("#header").css("margin", "0 auto");*/
	}
}

/*====================================================
 footerRelocation
=====================================================*/

function footerRelocation(){
	
	var clHeight;
	
	if(document.documentElement.clientHeight != undefined)	clHeight = document.documentElement.clientHeight;
	else if(document.body.clientHeight != undefined)		clHeight = document.body.clientHeight;
	else													clHeight = window.innerHeight;
	
	var headerHeight  = parseInt( $("#header").css("height") );
	var contentHeight = parseInt( getActiveStyle( document.getElementById("content"), "height" ) );
	var footerHeight  = 66; /*parseInt( getActiveStyle( document.getElementById("footer"), "height" ) );*/
	if ( isNaN( contentHeight ) ) contentHeight = getActiveContentHeight();
	
	var diff = clHeight - (contentHeight + headerHeight + footerHeight);
	
	if ( 0 < diff )
	{
		$("#content").css("height", (diff+contentHeight) + "px");
	}
	
	
	if($.browser.msie && $.browser.version < 7){
		var arry = $("#searchContainer").children();
		var len = arry.length;
		for(var i = 0; i < len; i++){
			var child = arry[i];
			child.style.height = height + "px";
		}
	}
}
function getActiveStyle ( element, property, pseudo ) {
	
	if( element.currentStyle ) { /* IE or Opera */
        return element.currentStyle[ property ];
    }
    else if( getComputedStyle ) { /* Mozilla or Opera */
        return getComputedStyle( element, pseudo ).getPropertyValue( property );
    }
    return "0px";
}
function getActiveContentHeight ( ) {
	
	var container;
	
	if ( $("#columnContainer").css("height") != "0px" )		container = $("#columnContainer");
	else if ( $("#searchContainer").css("height") != "0px" )	container = $("#searchContainer");
	else if ( $("#awardContainer").css("height") != "0px" )	container = $("#awardContainer");
	else if ( $("#helpContainer").css("height") != "0px" )		container = $("#helpContainer");
	var contentHeight = parseInt( container.css("height") );
	
	return contentHeight;
}
function resetFotterLocation(){
	
	$("#content").css("height", "");
}

/*====================================================
 Scroll
=====================================================*/

function scroll(){
	$('html,body').animate({ scrollTop: 0 }, 500);	
}


/*====================================================
 Switch Column
=====================================================*/

function switchColumn(displayNum){
	
	switch(displayNum){
		case 0:
			resetFotterLocation();
			showHome();
			footerRelocation();
			break;
		case 1:
			showSearch();
			break;
		case 2:
			resetFotterLocation();
			showHelp();
			break;
		case 4:
			resetFotterLocation();
			showAward();
			break;
	}	
}

function showHome(){
	
	var c = $("#columnContainer");
	var ch = c.css("height");
	var h = ch.substr(0, ch.length - 2);
	
	if(h  > 500){
		var hash = window.location.hash;
		var url = window.location.href;
		
		if(hash.length > 0){
			var len = hash.length;
			url = url.substr(0, url.length - len);
		}
		
		window.location = url;
		
	}else{
		
		if ( 0 < maxHomeHeight )	showContainer($("#columnContainer"), maxHomeHeight);
		else						showContainer($("#columnContainer"));
		
		hideContainer($("#searchContainer"));
		hideContainer($("#helpContainer"));
		hideContainer($("#awardContainer"));
	}
}



function showSearch(){
	showContainer($("#searchContainer"), 986);
	
	hideContainer($("#columnContainer"));
	hideContainer($("#helpContainer"));	
	hideContainer($("#awardContainer"));
}


function showHelp(){
	showContainer($("#helpContainer"), 1018);
	
	hideContainer($("#columnContainer"));	
	hideContainer($("#searchContainer"));
	hideContainer($("#awardContainer"));
}

function showAward(){
	showContainer($("#awardContainer"), 504);
	
	hideContainer($("#columnContainer"));
	hideContainer($("#searchContainer"));
	hideContainer($("#helpContainer"));
}

function hideContainer(container){
	if(container.css("height") == "0px") return;
	
	container.css("height", "0px");
	
	if($.browser.msie && $.browser.version < 7){
		var arry = container.children();
		var len = arry.length;
		for(var i = 0; i < len; i++){
			var child = arry[i];
			if(child.style.height){
				child.style.height = "0px";
			}else{
				child.style.height = "100%";
			}
		}
	}
}

function showContainer(container, height){
	if(container.css("height") != "0px") return;
	
	if(height == undefined) height = 1736 + margin;
	container.css("height", height + "px");
	
	if($.browser.msie && $.browser.version < 7){
		var arry = container.children();
		var len = arry.length;
		for(var i = 0; i < len; i++){
			var child = arry[i];
			child.style.height = height + "px";
		}
	}
}

/*====================================================
 SetUrl
=====================================================*/

function setUrl(trackNum){

	var exNum = window.location.hash;
	
	if(exNum != trackNum){
		window.location.hash = trackNum;
	}
}


/*====================================================
 SHowWindow
=====================================================*/

function showWindow(url, flashvars, fixHeight){
	var form = $("#externalFormContainer");
	var dummy = $("#dummy");
	var v = form.css("visibility");
		
	if(v == "hidden"){
		form.css("height", "100%");
		form.css("visibility", "visible");
		
		dummy.css("visibility", "hidden");
		dummy.css("height", "1px");
		dummy.css("marginTop", "0px");
	}else{
		dummy.css("visibility", "visible");
		dummy.css("height", "100%");
		dummy.css("marginTop", "-1px");
		
		form.css("height", "1px");
		form.css("visibility", "hidden");
	}
	
	
	if(flashvars){
		flashvars += "&browser=" + getSystem();
	}
	
	floatingWindow ( url, flashvars, fixHeight);
}


/*====================================================
 windowDelayOpen windowOpen
=====================================================*/

var intervalId;

function windowDelayOpen ( url ) {
	
	intervalId = setInterval("windowOpen('"+ url +"')", 100);
}
function windowOpen ( url ) {
	
	alert("windowOpen");
	
	clearInterval(intervalId);
	window.open( url );
}


/*====================================================
 AS Methods
=====================================================*/

function stopOthers(id){
	if(id!=0)	document.getElementById("externalFormContainer").stopSound();
	if(id!=1)	document.getElementById("newSongs").stopSound();
	if(id!=2)	document.getElementById("onTwitter").stopSound();
	if(id!=3)	document.getElementById("search").stopSound();
}
function fadeOutOthers(id){
	if(id!=0)	document.getElementById("externalFormContainer").fadeOut();
	if(id!=1)	document.getElementById("newSongs").fadeOut();
	if(id!=2)	document.getElementById("onTwitter").fadeOut();
	if(id!=3)	document.getElementById("search").fadeOut();
}
function showDetail(trackNum){
	setUrl(trackNum);
	document.getElementById("externalFormContainer").showTrackDetail(trackNum);
}
function searchStart(query){
	document.getElementById("search").searchStart(query);
}

function showDummyDetails(trackNum, iconNum, errorCode, awardNum){
	document.getElementById("dummy").showDummyDetails(trackNum, iconNum, errorCode, awardNum);
}


/*====================================================
 Tracking
=====================================================*/

var _req;
var _url;

function track(url, logUrl){
	_url = url;
	
	_req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('MSXML2.XMLHTTP');
	_req.onreadystatechange = trackHandler;
	_req.open('POST', logUrl);
	_req.send(null);
}

function trackHandler(){
	if(_req && _req.readyState == 4){
		window.location.href = _url;
	}
}