/*
  Általános animációs osztály thx:http://www.dustindiaz.com/javascript-animate/
  működését lsd. slideUp() fv-ben
  * @constructor Animate
  * @param {HTMLElement} el the element we want to animate
  * @param {String} prop the CSS property we will be animating
  * @param {Object} opts a configuration object
  * object properties include
  * from {Int}
  * to {Int}
  * time {Int} time in milliseconds
  * callback {Function}
  */
function Animate(el, prop, opts) {
  this.el = el;
  this.prop = prop;
  this.from = opts.from;
  this.to = opts.to;
  this.time = opts.time;
  this.callback = opts.callback;
  this.animDiff = this.to - this.from;
}

/**
  * @private
  * @param {String} val the CSS value we will set on the property
  */
Animate.prototype._setStyle = function(val) {
  switch (this.prop) {
    case 'opacity':
      this.el.style[this.prop] = val;
      this.el.style.filter = 'alpha(opacity=' + val * 100 + ')';
      break;

    default:
      this.el.style[this.prop] = val + 'px';
      break;
  };
};

/**
  * @private
  * this is the tweening function
  */
Animate.prototype._animate = function() {
  var that = this;
  this.now = new Date();
  this.diff = this.now - this.startTime;

  if (this.diff > this.time) {
    this._setStyle(this.to);

    if (this.callback) {
      this.callback.call(this);
    }
    clearInterval(this.timer);
    return;
  }

  this.percentage = (Math.floor((this.diff / this.time) * 100) / 100);
  this.val = (this.animDiff * this.percentage) + this.from;
  this._setStyle(this.val);
};

/**
  * @public
  * begins the animation
  */
Animate.prototype.start = function() {
  var that = this;
  this.startTime = new Date();

  this.timer = setInterval(function() {
    that._animate.call(that);
  }, 4);
};

/**
  Animáltan megnöveli a kapcsolat oldalon található képet
*/
function growMap(){
            mapImg = document.getElementById('dtmMap');
            if(mapImg!==null && mapImg!==undefined){
                        if(mapImg.className!=='shadow'){
                                    mapImg.style.position = 'absolute';
                                    mapImg.style.top = '34px';                                    
                                    mapImg.style.right = '19px';                                    
                                    new Animate(mapImg,'width',{
                                                from : 196,
                                                to : 311,
                                                time : 250,
                                                callback : function(){
                                                            return true;
                                                            }
                                                }).start();
                                    
                                    new Animate(mapImg,'height',{
                                                from : 173,
                                                to : 285,
                                                time : 250,
                                                callback : function(){
                                                            mapImg.className = 'shadow';                                               
                                                            return true;
                                                            }
                                                }).start();
                        }
                        else{
                                    new Animate(mapImg,'width',{
                                                from : 311,
                                                to : 196,
                                                time : 250,
                                                callback : function(){
                                                            return true;
                                                            }
                                                }).start();
                                    
                                    new Animate(mapImg,'height',{
                                                from : 285,
                                                to : 173,
                                                time : 250,
                                                callback : function(){
                                                            mapImg.className = '';                                               
                                                            return true;
                                                            }
                                                }).start();
                                    mapImg.style.position = '';
                                    mapImg.style.top = '';                                    
                                    mapImg.style.right = ''; 
                        }
            }
            return false;
}
/**
    Ajánlatkérés elküldése
*/
function sendInquery()
{
    var objForm = document.getElementById('inqueryForm');
    saveForm(objForm.action, 'inqueryForm', 'btnSend', 'loaderAnim');
    return false;
}
/**
	Elmenti a form adatait Ajax POST-tal
	@argURL				string	POST cím
	@argFormContainer		string	A konténer neve, amiben a form elemei laknak
	@argButtonName		string	Megnyomott gomb neve. Nem kell, hogy létezzen, a lényeg, hogy ez bekerül a POST adatok közé is '1' értékkekl
	@argLoaderAnim		string	A várakozó animáció neve, amit be kell kapcsolni a POST alatt
*/
function saveForm(argURL,argFormContainer,argButtonName,argLoaderAnim)
{

	var arrInputs = document.getElementById(argFormContainer).getElementsByTagName('input');
	var arrSelects = document.getElementById(argFormContainer).getElementsByTagName('select');
	var arrTextAreas = document.getElementById(argFormContainer).getElementsByTagName('textarea');
	var loaderAnim = document.getElementById(argLoaderAnim);
	var button = document.getElementById(argButtonName);
	var returnResult = false;
	
	if (loaderAnim!=null) loaderAnim.style.display = 'block';
	if (button!=null) button.style.display = 'none';

	var parameters = argButtonName+'=1';
	for (i=0;i<arrInputs.length;i++){
		if (arrInputs[i].id != argButtonName ){
			type = arrInputs[i].type.toLowerCase();
			if ((type == 'checkbox') && (arrInputs[i].checked==true)) parameters+= '&'+arrInputs[i].id+'='+escape(arrInputs[i].value);
			else if ((type == 'checkbox') && (arrInputs[i].checked==false)) parameters+= '&'+arrInputs[i].id+'='; 	
			else if (type == 'text') parameters+= '&'+arrInputs[i].id+'='+escape(arrInputs[i].value);
			else if (type == 'textarea') parameters+= '&'+arrInputs[i].id+'='+escape(arrInputs[i].value);
			else if (type == 'password') parameters+= '&'+arrInputs[i].id+'='+escape(arrInputs[i].value);
			else if (type == 'hidden') parameters+= '&'+arrInputs[i].id+'='+escape(arrInputs[i].value);
		}
	}
	for (i=0;i<arrSelects.length;i++){
		if (arrSelects[i].selectedIndex!=undefined)
			parameters+= '&'+arrSelects[i].id+'='+escape(arrSelects[i].options[arrSelects[i].selectedIndex].value);
		arrSelects[i].disabled = true;
	}
        
	for (i=0;i<arrTextAreas.length;i++){
		parameters+= '&'+arrTextAreas[i].id+'='+escape(arrTextAreas[i].value);
		arrTextAreas[i].disabled = true;
	}

    if (parameters!='') parameters+='&';
    else parameters+='?';
    parameters+='ajaxRequest=1';
    client = new HttpClient();
    client.requestType = 'POST';
    client.callback = function(){};
    result = client.makeRequest(argURL, parameters, null);

	if (result){
		arrResult = result.split("\n");
		if (arrResult[0] == 'error') alert (arrResult[1]);
		else if (arrResult[0] == 'succeedWithMessage'){
            returnResult = arrResult[1];
		}
		else if (arrResult[0] == 'succeedWithId'){
			returnResult = arrResult[1];
		}
		else if (arrResult[0] == 'succeedWithURL'){
			window.location.href = arrResult[1];
            returnResult = true;
		}
		else if (arrResult[0] == 'succeed') returnResult = true;
		else alert(result);
	}

	for (i=0;i<arrInputs.length;i++) arrInputs[i].disabled = false;
	for (i=0;i<arrSelects.length;i++) arrSelects[i].disabled = false;
	for (i=0;i<arrTextAreas.length;i++) arrTextAreas[i].disabled = false;
	if (loaderAnim!=null) loaderAnim.style.display = 'none';
	if (button!=null) button.style.display = 'block';

	return returnResult;
}
/**
 *AJAX kérésekhez
 */
function HttpClient(){}
HttpClient.prototype = {
 requestType: 'GET',
 isAsync: false,
 xmlhttp: false,
 callback: false,
 /**
   Az ajax hivas elkuldesekor vegrehajtando tevekenyseg
   @divid          string         annak a layer-nek az azonositoja ahova a vegeredmeny kerul
 */
 onSend: function(divid){
   if( divid ){
     document.getElementById(divid).innerHTML = "<br /><span style='margin:0px 0px 0px 10px;background:#ffffff;'>Adatfeldolgozás...</span>";
   }
 },
 /**
   Az ajax hivas betoltodesekor vegrehajtando tevekenyseg
   @divid          string         annak a layer-nek az azonositoja ahova a vegeredmeny kerul
 */
 onLoad: function(divid){
   if( divid ){
     document.getElementById(divid).innerHTML = "<br /><span style='margin:0px 0px 0px 10px;background:#ffffff;'>Kész</span>";
   }
 },
 /**
   Hiba eseten vegrehajtando tevekenyseg
   @error          string         annak a layer-nek az azonositoja ahova a vegeredmeny kerul
 */
 onError: function(error){ alert(error);
 },
 /**
   Az ajax hivas inicializalasa
 */
 init: function(){
  try{
   this.xmlhttp=new XMLHttpRequest();
  }
  catch(e){
   var xmlhttp_ids = new Array('MSXML2.XMLHTTP.5.0',
             'MSXML2.XMLHTTP.4.0',
             'MSXML2.XMLHTTP.3.0',
             'MSXML2.XMLHTTP',
             'Microsoft.XMLHTTP');
   var success = false;
   for(var i = 0;i < xmlhttp_ids.length && !success; i++){
    try{
     this.xmlhttp = new ActiveXObject(xmlhttp_ids[i]);
     success = true;
    }
    catch (e){}
   }
   if(!success){
    this.onError('XMLHttpRequest is failed.');
   }
  }
 },
 /**
   Az ajax hivas kezdemenyezese
   @url            string         a fogado script url-je, ahol a feldolgozas tortenik
   @payload        string         ha a request tipusa post, akkor ez tartalmazza a post parametereket
   @divid          string         annak a layer-nek az azonositoja ahova a vegeredmeny kerul
   @return         string         ha nem aszinkron volt a hivas, akkor az eredmeny itt tarolodik
 */
 makeRequest: function(url,payload,divid){
  
  if(!this.xmlhttp){
   this.init();
  }
  this.xmlhttp.open(this.requestType,url,this.isAsync);
  if(this.requestType == 'POST'){
   this.xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=ISO-8859-2');
  }
  this.xmlhttp.onreadystatechange = function(){
   self._readyStateChangeCallback(divid);
  }
  var self=this;
  this.xmlhttp.send(payload);
  if( !this.isAsync ){
   return this.xmlhttp.responseText;
  }
 },
 /**
   Az ajax hivas visszateresenek lekezelese
   @divid          string         annak a layer-nek az azonositoja ahova a vegeredmeny kerul
 */
 _readyStateChangeCallback: function(divid){
  switch(this.xmlhttp.readyState){
   case 0:
   case 1:
   case 2:
    this.onSend(divid);
   break;
   case 4:
    this.onLoad(divid);
    if( this.xmlhttp.status == 200 ){
     this.callback(this.xmlhttp.responseText);
    }
    else {
     this.onError('HTTP error during the request: ['+this.xmlhttp.status+'] '+this.xmlhttp.statusText);
    }
   break;
  }
  return false;
 }
}

/**
 *  Elindítja a főoldal jobb hasábjában lévő banner rotálást
 *  @argBoxName      string      Box neve
 *  @argPrevIndex    int         Előző banner index
 *  @argNextIndex    int         Következő banner index
 *  @argAnimStep     int        Animáció lépés állapota 1-5
 */
function rotateBanners(argBoxName, argPrevIndex, argNextIndex, argAnimStep)
{
    var nextObj = document.getElementById(argBoxName+'_'+argNextIndex);
    var prevObj = document.getElementById(argBoxName+'_'+argPrevIndex);
    if (prevObj==null) return false;
    if (nextObj==null){
        prevObj.style.display = 'block';
        return false;
    }

    if (argAnimStep==0){
        prevObj.style.filter  = "alpha(opacity=100)";
        prevObj.style.opacity = 1;
        prevObj.style.zIndex = 5;
        prevObj.style.display = 'block';
    }

    nextObj.style.zIndex = 10;
    nextObj.style.filter  = "alpha(opacity=" + (100*(0.2*argAnimStep)) + ")";
    nextObj.style.opacity = (0.2*argAnimStep);
    nextObj.style.display = 'block';

    if (argAnimStep<5) setTimeout(function(){rotateBanners(argBoxName, argPrevIndex, argNextIndex, argAnimStep+1);}, 40);
    else{
        prevObj.style.display = 'none';
        if (document.getElementById(argBoxName+'_'+(parseInt(argNextIndex)+1)) == null) nextIndex = 1;
        else nextIndex = parseInt(argNextIndex)+1;
        setTimeout("rotateBanners('"+argBoxName+"', '"+argNextIndex+"', '"+nextIndex+"', 0)", 5000);
    }
}


//create onDomReady Event
window.onDomReady = DomReady;

//Setup the event
function DomReady(fn)
{
	//W3C
	if(document.addEventListener)
	{
		document.addEventListener("DOMContentLoaded", fn, false);
	}
	//IE
	else
	{
		document.onreadystatechange = function(){readyState(fn)}
	}
}

//IE execute function
function readyState(fn)
{
	//dom is ready for interaction
	if(document.readyState == "complete")
	{
		fn();
	}
}

window.onDomReady(onReady);

function onReady(){
            document.getElementById('rsmdtm').className+='js';
            
            //bannerek a főoldalon;
            rotateBanners('box1',1,2,0);
}


/** Kinyitja az árfolyamokat
 * @argLink		Object	Link, amikre kattintottunk
 */
function openCurrencies(argLink)
{
    var element = $('#currencies');
    if(element!==null){
		if (element.css('display')!== 'block'){
			element.slideDown(250);
			argLink.parentNode.removeChild(argLink);
		}
	}
}


