String.prototype.trim = function() {
  a = this.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
};


//CONFIG
var MAINPROMO_DELAY = 7;

//GLOBALS
var ActPromo = 1;
var PromoTMO;

function RightPromoOver(img,w) {
  if(w) $(img).style.left = (-1* $(img).width / 2)+'px';
  else $(img).style.left = '0px';
  
}

function HeadSearchClick(inp,w) {
  if(w && inp.value == 'Keresés') inp.value = '';
  if(!w && inp.value == '') inp.value = 'Keresés';
}

function ChMainPromo(newpromo) {
  if(ActPromo == newpromo) return false;
  PromoCount = $('mainpromo').getElementsByClassName('promo').length;
  if(newpromo == 'next') {
    clearTimeout(PromoTMO);
    newpromo = ActPromo + 1;
    Delayer = 2;
  }
  else if(newpromo == 'prev') {
    clearTimeout(PromoTMO);
    newpromo = ActPromo - 1;
    Delayer = 2;
  }
  else {

    Delayer = 1;
  }

  if(newpromo <= 0) newpromo = PromoCount;
  if(newpromo > PromoCount) newpromo = 1;
 
  var olddiv = $('mainpromo').getElementsByClassName('promo')[ActPromo-1];
  var newdiv = $('mainpromo').getElementsByClassName('promo')[newpromo-1];

  $(olddiv).appear({ duration: 0.5, from: 1.0, to: 0.0 });
  $(newdiv).appear({ duration: 0.5, from: 0.0, to: 1.0 });

  ActPromo = newpromo;

  $('mainpromo').getElementsByClassName('count')[0].innerHTML = ActPromo+' / '+PromoCount;
  PromoTMO = setTimeout(function() {ChMainPromo(ActPromo+1);},Delayer*MAINPROMO_DELAY*1000);
}


function IndexInit() {
  PromoTMO = setTimeout(function() {ChMainPromo(ActPromo+1);},MAINPROMO_DELAY*1000);
}

function FindPos(obj) {
	var curleft = curtop = 0;
  if (obj.offsetParent) {
  do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
      } while (obj = obj.offsetParent);
  }
	return [curleft,curtop];
}


var ActiveDD = null;
var ShowDropdownTimer = new Array();
var HideDropdownTimer = new Array();

function ShowDropDown(id,sender) {
  if(HideDropdownTimer[id]) {
    clearTimeout(HideDropdownTimer[id]);
  }

  ShowDropdownTimer[id] = setTimeout(function () { DoShowDorpDown(id, sender); }, 50);
}

function HideDropDown(id,sender) {
  if(ShowDropdownTimer[id]) {
    clearTimeout(ShowDropdownTimer[id]);
  }

  HideDropdownTimer[id] = setTimeout(function () { DoHideDropDown(id, sender); }, 500);
}

function DoShowDorpDown(id,sender) {
  if(ActiveDD && ActiveDD != id) {
    DoHideDropDown(ActiveDD)
  }
  
  if(sender) {
    SenderPos = FindPos($(sender));
    dd = $('submenu'+id);

    dd.style.display = '';
    dd.style.top = '80px';
    dd.style.left = SenderPos[0]+5+'px';
  }

  ActiveDD = id;
}

function DoHideDropDown(id,sender) {
  $('submenu'+id).style.display = 'none';
}

function numericTest(e){
    var key = e.keyCode? e.keyCode : e.charCode
    if((key > 47 && key < 58) || key == 8 || key == 37 || key == 39 || key == 13 || key == 46) {
        return true;
    }
    else return false;
}

function ShippingClick(chk) {
  if(chk.checked) {
    $('shippingFrm').hide();
  }
  else {
    $('shippingFrm').show();
  }
}


Seeker = {
  MouseFixPoint: null,
  Width: 308,
  ButtonWidth: 33,
  Min: 10,
  Max: null,
  OrigPosition: null,
  Position: null,
  Level: 1,
  InDrag: false,

  StartDrag: function(Event) {
    MouseFixPoint = Seeker.GetMousePosition(Event).x;
    OrigPosition = parseInt($('SeekerBtn').style.left);
    Seeker.Max = Seeker.Width-Seeker.ButtonWidth-12;
    Seeker.InDrag = true;

    document.onmouseup = function (Event)
    { 
      document.onmouseup = null;
      document.onmousemove = null;

      N = (Seeker.Max - Seeker.Min) / 4;

      if(Position <= N) {
        Position = Seeker.Min;
        NLevel = 1;
      }
      else if(Position >= N * 3) {
        Position =  Seeker.Max;
        NLevel = 3;
      }
      else {
        Position = parseInt(Seeker.Width / 2 - Seeker.ButtonWidth /2);
        NLevel = 2;
      }
      //$('SeekerBtn').style.left = Position + 'px';
      new Effect.Move($('SeekerBtn'), { x: Position,
                                      y: 3, mode: 'absolute',
                                      duration: 0.4,
                                      afterFinish: function() {
                                        $('confleft').focus();
                                        if(NLevel != Seeker.Level) {
                                          Seeker.Level = NLevel;
                                          ConfReq(null);
                                        }
                                      }
                                    });

    };

    document.onmousemove = function (Event) { Seeker.MouseMove(Event); };
  },

  MouseMove: function (Event) {
    if (!Event) var Event = window.event;
    Seeker.DisableEvent(Event);

    // set moving handle position
    Position = OrigPosition + Seeker.GetMousePosition(Event).x - MouseFixPoint;
    if (Position < Seeker.Min) {
      Position = Seeker.Min;
    } else if (Position > Seeker.Max) {
      Position = Seeker.Max;
    }

    
    $('SeekerBtn').style.left = Position + 'px';
  },

  DisableEvent: function (Event)
  {
    if (!Event) var Event = window.event;

    Event.cancelBubble = true;
    if (Event.stopPropagation) Event.stopPropagation();

    return false;
  },

  GetMousePosition: function(Event) {
    var PosX = 0;
    var PosY = 0;
    if (Event.pageX || Event.pageY) {
      PosX = Event.pageX;
      PosY = Event.pageY;
    }
    else if (Event.clientX || Event.clientY)  {
      PosX = Event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      PosY = Event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
    return {x: PosX, y: PosY};
  },

  Reset: function() {
    Position = Seeker.Min;
    Seeker.Level = 1;
    $('SeekerBtn').style.left = Position + 'px';
  },

  Click: function(Eve) {
    if(Seeker.InDrag) {
      Seeker.InDrag = false;
      return false;
    }
    Seeker.Max = Seeker.Width-Seeker.ButtonWidth-12;
    Position = Seeker.GetMousePosition(Eve).x - Event.element(Eve).positionedOffset()[0];
    N = (Seeker.Width) / 4;

    if(Position <= N) {
      Position = Seeker.Min;
      NLevel = 1;
    }
    else if(Position >= N * 3) {
      Position =  Seeker.Max;
      NLevel = 3;
    }
    else {
      Position = parseInt(Seeker.Width / 2 - Seeker.ButtonWidth /2);
      NLevel = 2;
    }
    if(NLevel == Seeker.Level) return false;
    Seeker.Level = NLevel;

    //$('SeekerBtn').style.left = Position + 'px';
    new Effect.Move($('SeekerBtn'), { x: Position,
                                      y: 3, mode: 'absolute',
                                      duration: 0.4,
                                      afterFinish: function() {
                                        $('confleft').focus();
                                        ConfReq(null);
                                      }
                                    });
    //$('SeekerBtn').Move({x: Position, y:0});
    
  }
}

var SelectedApp = new Array();
var ProductDatas;
var ConfGroup = 'SERVER';
var ConfLeft = false;
var ConfLeftTO;
var ActiveConf;

function ConfReq(App,Level) {
  if(App) {
    P = SelectedApp.indexOf(App);
    if(P == -1) {
      SelectedApp[SelectedApp.length] = App;
      $('confapp'+App).className = "selected";
    }
    else {
      SelectedApp = SelectedApp.without(App);
      $('confapp'+App).className = "";
    }
  }
  
  $('conflist').innerHTML = '<img src="/image/ajaxloader.gif" alt="Betöltés" style="margin: 10px;" />';
  //$('conflist').style.verticalAlign = 'middle';
  $('conflist').style.background = '#BAC3C9';

  setCookie('configApp',SelectedApp.toString(),365);
  setCookie('configLevel',Seeker.Level,365);
  setCookie('configGroup',ConfGroup,365);


  new Ajax.Request('/?mod=ajaxjson&action=CtrlAjaxConfig&app='+SelectedApp.toString()+'&level='+Seeker.Level, {
     onSuccess: function(transport) {
       datas = transport.responseText.evalJSON();
       $('conflist').innerHTML = datas.Body;
       $('conflist').style.background = '#FFFFFF';
       $('conflist').style.verticalAlign = 'top';
       ProductDatas = datas.Products;
     }
  }
  );
}



function ConfOver(CfgId) {
  if(!ActiveConf) {
    ConfProps(CfgId);
  }
  else {
    ConfLeftTO = setTimeout(function() {ConfProps(CfgId);},1100);
  }
}

function ConfProps(CfgId) {
  clearTimeout(ConfLeftTO);


  if($('listcfg'+ActiveConf)) {
    $('listcfg'+ActiveConf).className = '';
  }
  $('listcfg'+CfgId).className = 'selected';
  ActiveConf = CfgId;

  eval('Product = ProductDatas.p'+CfgId+";");
  HTML = '<div class="cont"><img src="/prodpic/tn/'+Product.list_pic+'" alt="'+Product.name+'" />';
  HTML += '<h3>'+Product.name+'</h3>';
  for(i = 0;i < Product.properties.length;i++) {
    if(Product.properties[i] && i < 14) {
      HTML += '<b><u>'+Product.properties[i].trim()+'</u>:</b> '+Product.property_values[i]+'<br />';
    }
  }

  HTML += '<div style="text-align: right"><a href="/kiegeszitok/'+Product.config_id+'.html" style="color: #E87711; font-size: 11px;">Tovább »</a></div>';

  if(Product.sale_text) {
    HTML += '<div style="font-size: 12px; font-weight: bold; text-align: center;"><br />'+Product.sale_text+'<br /></div>';
  }

  if(Product.cfg_sale_price) {
    HTML += '<br /><span class="price">HUF '+addCommas(Product.cfg_sale_price)+'<br /><small>HUF '+addCommas(Product.cfg_price)+'<br /></small></span>';
  }
  else {
    HTML += '<br /><span class="price">HUF '+addCommas(Product.cfg_price)+'</span>';
  }

/*
 * {if $Page->Product.cfg_sale_price}
              HUF {$Page->Product.cfg_sale_price|price_format}<br />
              <small>HUF {$Page->Product.cfg_price|price_format}</small>
              {else}
              HUF {$Page->Product.cfg_price|price_format}
              {/if}
 */

  HTML += '</div>';
  $('confleft').innerHTML = HTML;
}

function SetConfGroup(Group,lnk) {
  if(Group == ConfGroup) return false;

  Links = $('cfghright').childElements();
  for(i = 0; i < Links.length; i++) {
    Links[i].className = '';
  }

  $(lnk).className = 'active';
  if(Group == 'SERVER') {
    $('confgserver').src = "/image/conf_head_arr_left-2.jpg";
    $('confgstorage').src = "/image/conf_head_arr_right.jpg";
  }
  else {
    $('confgserver').src = "/image/conf_head_arr_left.jpg";
    $('confgstorage').src = "/image/conf_head_arr_right-2.jpg";
  }
  
  

  ConfGroup = Group;
  clearTimeout(ConfLeftTO);
  new Ajax.Updater('applist','/?mod=ajax&module=Index&action=CtrlAjaxConfGroup&group='+Group);
  Seeker.Reset();
  SelectedApp.clear();
  ConfReq();
}

function ConfOut() {
  clearTimeout(ConfLeftTO);
}

ContLeftOver = function() {
  if(ConfLeft !== false && ConfLeftTO) {
    clearTimeout(ConfLeftTO);
    $('confleft').innerHTML = ConfLeft;
    ConfLeft = false;
  }
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	return x1 + x2;
}

function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function PlayVideo(Video,VidId,AutoStart) {
  var s1 = new SWFObject('/js/player.swf','ply','620','454','9','#ffffff');
  s1.addParam('allowfullscreen','true');
  s1.addParam('wmode','opaque');
  flashvars = 'file=/videos/'+Video;//+'&image=/videos/'+VidId+'.jpg';
  if(AutoStart) {
    flashvars += '&autostart=true';
  }
  s1.addParam('flashvars',flashvars);
  s1.write('video');
}
