
var sPlayerPath = "typo3conf/ext/showroom/tmpl/swf/jwplayer.swf";
var sAudioPlayerConfig = 'typo3conf/ext/showroom/tmpl/xml/audio_conf.xml';
var sVideoPlayerConfig = 'typo3conf/ext/showroom/tmpl/xml/video_conf.xml';

var sImageDir = 'typo3conf/ext/showroom/tmpl/images/';
var sAjaxBusyIconPath = sImageDir + "gray_busy.gif";
var sAjaxBusyIcon  = "<div><img src='" + sAjaxBusyIconPath + "'></div>";


function general_load_event() {
  document.fire("page:fully_loaded");
  document.fire("page:detailpage_file_download");
}

var requestsCounter = [];

/**
 * send ajax query and update some div block
 *
 * @param str url
 * @param str pars
 * @param str placeHolder
 */
function vUpdateContent(url,pars, placeHolder, sAjaxBusyIcon, oFunction) {
  var browser = navigator.appName;
  if (requestsCounter[$(placeHolder).id] == undefined) {
    requestsCounter[$(placeHolder).id]=0;
  }
  pars += '&_r_c=' + ++requestsCounter[$(placeHolder).id];
  //alert(pars);
  if (browser == "Microsoft Internet Explorer" ) {
  	var oAjax = new Ajax.Request(url, {
        method: 'get',
        parameters: pars,
        onCreate: function() {
          var oDiv = $(placeHolder);
          oDiv.update(sAjaxBusyIcon);
        },
        onLoaded: function() {
          var oDiv = $(placeHolder);
          oDiv.update(sAjaxBusyIcon);
        },
        onComplete: function(response) {
          vContentUpdater(placeHolder, response);
          if ( typeof oFunction == 'function') {
            oFunction.call();
          }
        }
      });
  } else {
  	var oAjax = new Ajax.Request(url, {
        method: 'get',
        parameters: pars,
        onLoading: function() {
          var oDiv = $(placeHolder);
          oDiv.update(sAjaxBusyIcon);
        },
        onComplete: function(response) {
          vContentUpdater(placeHolder, response);
          if ( typeof oFunction == 'function') {
            oFunction.call();
          }
        }
      });
  }
}


function vContentUpdater(placeHolder, oAjaxResponce) {
  oJson = oAjaxResponce.responseText.evalJSON(true);
  if (undefined != oJson._r_c) {
    if (oJson._r_c < requestsCounter[$(placeHolder).id]) {
      return;
    }
  }
  $(placeHolder).update(oJson.content.replace(/&quot;/gi, '"').unescapeHTML());
}


var sUpdateOwnFilesListBlock = function (oEL, sSiteName, sPageParams) {
 if (oEL) {
   sFilesListBlickId = "showroom_own_files_list_block";
   if ($(oEL).checked) {
    sNewParam = '&checked=' + 1;
   } else {
     sNewParam = '&checked=' + 0;
   }
   sUrlParams =  sPageParams + sNewParam;
   if (sSiteName) {
     vUpdateContent(sSiteName, sUrlParams, sFilesListBlickId, sAjaxBusyIcon); 
   }	
 }
}


/**
 * php compatible encode function
 *
 * @param str s
 * @return str
 */
function urlencode(sStr) {
  sStr = encodeURIComponent(sStr);
  return sStr.replace(/~/g,'%7E').replace(/%20/g,'+');
}

/**
 * php compatible decode function
 *
 * @param str s
 * @return str
 */
function urldecode(sStr) {
  sNewStr = sStr.replace(/%7E/g,'~').replace(/\+/g,'%20');
  sNewStr = decodeURIComponent(sNewStr);
  return sNewStr;
}

var bCheckRequiredFields = function (oForm, sErrorMessage) {
  var bChecked = true;
  var sPrefix = oForm.name + '_';
  var sRequiredFieldsId = sPrefix + 'required';  
  oFieldRequired = $(sRequiredFieldsId);
  if (null == oFieldRequired) {
    return true;
  }
  var aRequired = [];
  oFieldRequired.value.scan(/\S+/, function(match){ aRequired.push(match[0])});
  aRequired.each (function(sField) {
    sFieldId = sPrefix + sField;
    if (('' == $F(sFieldId)) || (0 == $F(sFieldId))) {
      sErrorMessage += "\n - " + sFieldId;
      bChecked = false;
    }
  });
  if (!bChecked) {
    alert(sErrorMessage);
  }
  return bChecked;
}

var vUpdateContainer = function(sAddress, sUrlParams, oElement, sContainerId, oFunction){
  //alert(sAddress + '?' + sUrlParams + '&' + oElement.name + '=' + oElement.value + ' ' + sContainerId);
  var sElementParam = oElement.name + '=' + oElement.value;
  sUrlParams += '&' + sElementParam;
  vUpdateContent(sAddress, sUrlParams, $(sContainerId), sAjaxBusyIcon, oFunction); //url,pars, placeHolder, sAjaxBusyIcon
}

var vShowFieldHelp = function(oHelpElement) {
  alert($(oHelpElement).innerHTML);
}

var oLocalLang = {
  
  sLoc:location.search,
  L:0,
  oDefaultLang: {},
  oCurrentLang: {},
  init: function() {
    var oUrlParams = this.sLoc.toQueryParams();
    if (0 < oUrlParams.L) {
      this.L = oUrlParams.L;
    }
    
    if (undefined == window.oL10n) {
      return;
    }
    var oLangDefault = window.oL10n[0];
    var oLangCurrent = window.oL10n[this.L];
    this.oDefaultLang = (undefined == oLangDefault) ? {} : oLangDefault;
    this.oCurrentLang =  (undefined == oLangCurrent) ? this.oDefaultLang : oLangCurrent;
  },
  sGetLang: function(sKey, sDefault) {
    if (undefined == sKey) {
      return "[please specify localisation parameters]";
    }
    sOutput = this.oCurrentLang[sKey];
    if (undefined == sOutput) {
      sOutput = this.oDefaultLang[sKey];
    }
    if (undefined == sOutput) {
      sOutput = sDefault;
    }
    if (undefined == sOutput) {
      sOutput = sKey;
    }
    return sOutput;
  }
}

oLocalLang.init();

function noop() {
  return;
}

function onCountryChange(target, sShortZipCountries, iShortZipLength, iDefaultZipLength){
	var iZipID = 'tx_showroom_pi1_showroom_edit_form_zip';
	var iPostboxZipID = 'tx_showroom_pi1_showroom_edit_form_postbox_zip';
	
	var aShortZipCountries = sShortZipCountries.split(",");
	var iCountShortZipCountries = aShortZipCountries.length;
	
	bChangeLength = false;
	for(i=0; i<iCountShortZipCountries; i++){
		if(parseInt(target.value) == parseInt(aShortZipCountries[i])) bChangeLength = true;
	}
		
	if(bChangeLength) iSize = parseInt(iShortZipLength);
	else iSize = parseInt(iDefaultZipLength);
	
	document.getElementById(iZipID).maxLength = iSize;
	document.getElementById(iZipID).size = iSize;
	document.getElementById(iZipID).value = document.getElementById(iZipID).value.substr(0,iSize);
	
	document.getElementById(iPostboxZipID).maxLength = iSize;
	document.getElementById(iPostboxZipID).size = iSize;	
	document.getElementById(iPostboxZipID).value = document.getElementById(iZipID).value.substr(0,iSize);
}

var vSetValue = function (sField, sValue, sForm){
	$(sField).value = sValue;
	if(sForm){
		$(sForm).submit();
	}
}

vEditChangeCountry = function (target, aZipCountries, iDefaultZipLength) {
  var sZipID = 'tx_showroom_pi1_showroom_edit_form_zip';
  var sPostboxZipID = 'tx_showroom_pi1_showroom_edit_form_postbox_zip';
  iZipLength = aZipCountries[target.value];
  if (undefined == iZipLength) {
    iZipLength = iDefaultZipLength;
  }
  vCutField(sZipID, iZipLength);
  vCutField(sPostboxZipID, iZipLength);
}

vCutField = function (oField, iSize) {
  $(oField).maxLength = iSize;
  $(oField).size = iSize;
  $(oField).value = $(oField).value.substr(0, iSize);
}
