/**
*
*/

//Global variable
if(document.URL.match('dev.')){
    var HM_URL = "http://dev.www.hotmovies.jp/";
    var MY_URL = "https://dev.my.hotmovies.jp/";
}else if(document.URL.match('staging.')){
    var HM_URL = "http://staging.www.hotmovies.jp/";
    var MY_URL = "https://staging.my.hotmovies.jp/";
}else{
    var HM_URL = "http://www.hotmovies.jp/";
    var MY_URL = "https://my.hotmovies.jp/";
}

function seltab(bpref, hpref, id_max, selected) {
  if (! document.getElementById) return;
  for (i = 0; i <= id_max; i++) {
    if (! document.getElementById(bpref + i)) continue;
    if (i == selected) {
      document.getElementById(bpref + i).style.visibility = "visible";
      document.getElementById(bpref + i).style.position = "";
      document.getElementById(hpref + i).className = "MenuOn orange";
    } else {
      document.getElementById(bpref + i).style.visibility = "hidden";
      document.getElementById(bpref + i).style.position = "absolute";
      document.getElementById(hpref + i).className = "MenuOff alink";
    }
  }
}


/**
* Function for switch scene view on video detail page
* @param string viewType (list or detail)
* @see: views/scripts/video/common/detail/sceneDetail.{lang}.php
*		views/scripts/video/common/detail/sceneList.{lang}.php
*/
function switchSceneView(viewType)
{
	var content="";
	var hcontent="";

	if(viewType=="detail"){
		var detail = document.getElementById("sdetail").innerHTML;
		content = "<div id='sdetail'>" + detail + "</div>";
		var list = document.getElementById("slist").innerHTML;
		hcontent = "<div id='slist' style='display:none;'>"+list +"</div>";
		var scene = document.getElementById("scene");
    	scene.innerHTML = content+hcontent;
	}else{
		var list = document.getElementById("slist").innerHTML;
        content = "<div id='slist'>"+list+"</div>";
        var detail = document.getElementById("sdetail").innerHTML;
        hcontent = "<div id='sdetail' style='display:none;'>" + detail + "</div>";
		var scene = document.getElementById("scene");
		scene.innerHTML = content+hcontent;
	}
}

/**
* function for url (ie. http://www.hotmovies.jp/ja/Scene/Search/Id/30/performer)
*/
function switchSceneBoxView(viewType)
{
	if(viewType=='detail'){
	document.getElementById('sboxdetail').className='block';
	document.getElementById('sboxlist').className='hide';
	} else {
	document.getElementById('sboxdetail').className='hide';
	document.getElementById('sboxlist').className='block';
	}
}

/**
* Function for sort functionality on list page 
* (by release_date, A-Z, or populality)	
*/
function sortList(sortBy)
{
	var videoIdList = document.getElementById("videoIdList").innerHTML;
	var noPerPage =  document.getElementById("noPerPage").innerHTML;
	var controller = document.getElementById("controller").innerHTML;
	var searchName = "";
    if(controller=='Search'){
        searchName = document.getElementById("searchName").innerHTML;
    }
	var categoryId = "";
    if(controller=='Category'){
        categoryId = document.getElementById("categoryId").innerHTML;
    }
    search("1", noPerPage, sortBy, videoIdList, controller, true, "",
        searchName, categoryId);
}

/**
* Function for sort functionality on list page
* (by release_date, A-Z, or populality)
*/
function sortSceneList(sortBy)
{
    var sceneIdList = document.getElementById("sceneIdList").innerHTML;
    var noPerPage =  document.getElementById("noPerPage").innerHTML;
    var controller = document.getElementById("controller").innerHTML;
	var searchName = "";
    if(controller=='Search'){
        searchName = document.getElementById("searchName").innerHTML;
    }
	var categoryId = "";
    if(controller=='Category'){
        categoryId = document.getElementById("categoryId").innerHTML;
    }
    search("1", noPerPage, sortBy, sceneIdList, controller, true, "",
        searchName, categoryId);
}

/**
* function for page link on list page
*
*/
function switchPage(pageNo)
{
	var videoIdList = document.getElementById("videoIdList").innerHTML;
    var noPerPage =  document.getElementById("noPerPage").innerHTML;
    var controller = document.getElementById("controller").innerHTML;
	var sortBy = document.getElementById("sortBy").innerHTML;
	var searchName = "";
    if(controller=='Search'){
        searchName = document.getElementById("searchName").innerHTML;
    }
	var categoryId = "";
    if(controller=='Category'){
        categoryId = document.getElementById("categoryId").innerHTML;
    }
    search(pageNo, noPerPage, sortBy, videoIdList, controller, true, "",
        searchName, categoryId);
}

/**
* function for scene search result list
*/
function switchScenePage(pageNo)
{
    var sceneIdList = document.getElementById("sceneIdList").innerHTML;
    var noPerPage =  document.getElementById("noPerPage").innerHTML;
    var controller = document.getElementById("controller").innerHTML;
    var sortBy = document.getElementById("sortBy").innerHTML;
	var searchName = "";
    if(controller=='Search'){
        searchName = document.getElementById("searchName").innerHTML;
    }
	var categoryId = "";
    if(controller=='Category'){
        categoryId = document.getElementById("categoryId").innerHTML;
    }
    search(pageNo, noPerPage, sortBy, sceneIdList, controller, true, "",
        searchName, categoryId);

}

function search(pageNo, noPerPage, sortBy, videoIdList, controller, sortOnly,
	searchField, searchName, categoryId)
{
	var lg;
    if(document.URL.match('/ja/')){
        lg = 'ja';
    }else{
        lg = 'en';
    }
	var form = document.createElement("form");
    form.setAttribute("method", "post");
	form.setAttribute("action", "/"+lg+"/"+controller+"/Search/");

	var hiddenField;
    hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "pageNo");
    hiddenField.setAttribute("value", pageNo);
    form.appendChild(hiddenField);

	hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "noPerPage");
    hiddenField.setAttribute("value", noPerPage);
    form.appendChild(hiddenField);

	hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "sortBy");
    hiddenField.setAttribute("value", sortBy);
    form.appendChild(hiddenField);

	//if request controller were Scene, parameter is sceneIdList
	if(controller=="Scene"){
        hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "sceneIdList");
        hiddenField.setAttribute("value", videoIdList);
        form.appendChild(hiddenField);
	}else{
		hiddenField = document.createElement("input");
    	hiddenField.setAttribute("type", "hidden");
    	hiddenField.setAttribute("name", "videoIdList");
    	hiddenField.setAttribute("value", videoIdList);
		form.appendChild(hiddenField);
	}

	hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "searchField");
    hiddenField.setAttribute("value", searchField);
    form.appendChild(hiddenField);

	if(controller=="Search"){
        hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "searchName");
        hiddenField.setAttribute("value", searchName);
        form.appendChild(hiddenField);
	}
	
	if(controller=='Category'){
        hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "categoryId");
        hiddenField.setAttribute("value", categoryId);
        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
}

function play(language, videoId, videoSpeed, startTime)
{

    var url=null;
    var paramStr = "/Id/"+videoId+"/"+videoSpeed+"/"+startTime;
    var url = HM_URL+language+"/Player"+paramStr;
    window.open(url, 'hmplayer', 'toolbar=0, location=0, directories=0, status=0, menuebar=0, '+
            'scrollbars=1, resizable=1, width=425, height=500');

}

function samplePlay(language, videoId, type, videoSpeed)
{
    var url=null;
    var paramStr = "/Id/"+videoId+"/"+videoSpeed;

    if(type=='S'){
        var url = HM_URL+language+"/Sample/play"+paramStr;
    }else if(type=='D'){
        var url = HM_URL+language+"/Sample/download"+paramStr;
    }
    window.open(url, 'hmplayer', 'toolbar=0, location=0, directories=0, status=0, menuebar=0, '+
            'scrollbars=1, resizable=1, width=425, height=500');
}

function trim(argValue){

    var str = String(argValue).replace(/^[ 　]*/gim, "");
    return String(str).replace(/[ 　]*$/gim, "");
}

function changeCountry(){
	ChooseValue = document.getElementById("country").value;
    if (ChooseValue == "124" || ChooseValue == "840"){
		document.getElementById("province_select").className="hide";
		document.getElementById("province_select").disabled=true;
		document.getElementById("state_select").className="inline_only";
		document.getElementById("state_select").disabled=false;
		document.getElementById("japan_select").className="hide";
		document.getElementById("japan_select").disabled=true;
    } else
    if (ChooseValue == "392"){
		document.getElementById("province_select").className="hide";
		document.getElementById("province_select").disabled=true;
		document.getElementById("state_select").className="hide";
		document.getElementById("state_select").disabled=true;
		document.getElementById("japan_select").className="inline_only";
		document.getElementById("japan_select").disabled=false;
    } else {
		document.getElementById("province_select").className="inline_only";
		document.getElementById("province_select").disabled=false;
		document.getElementById("state_select").className="hide";
		document.getElementById("state_select").disabled=true;
		document.getElementById("japan_select").className="hide";
		document.getElementById("japan_select").disabled=true;
    }
}

function purchase(language, videoId, planId, planType, type)
{
    url = MY_URL+"/"+language+"/Purchase/";
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", url);

    var hiddenField;
    hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "videoId");
    hiddenField.setAttribute("value", videoId);
    form.appendChild(hiddenField);

    hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "planId");
    hiddenField.setAttribute("value", planId);
    form.appendChild(hiddenField);

    hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "planType");
    hiddenField.setAttribute("value", planType);
    form.appendChild(hiddenField);

    hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "type");
    hiddenField.setAttribute("value", type);
    form.appendChild(hiddenField);

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();

}
