function goTo(Url){ document.location.href=Url; }
function obj(Id) { if(Id!='' && document.getElementById(Id)) return document.getElementById(Id); else return false; }
function objStyle(Id) { if(Id!='' && document.getElementById(Id)) return document.getElementById(Id).style; else return false; }
function objHide(Id) { if(Id!='' && document.getElementById(Id)) document.getElementById(Id).style.display='none'; }
function objShow(Id) { if(Id!='' && document.getElementById(Id)) document.getElementById(Id).style.display='block'; }
function isObj(Id) { if(Id!='' && document.getElementById(Id)) return true; else return false; }
function bgChange(Id, ImgSrc) { objStyle(Id).backgroundImage = "url("+ImgSrc+")"; }

var ObjPopupId = '';
function objPopup(Id){
  if( Id!=ObjPopupId ) objHide(ObjPopupId);
  if( objStyle(Id).display=='block' ) objStyle(Id).display='none';
  else  objStyle(Id).display='block';
  ObjPopupId = Id;
}

function AjaxRequest(Id, URL, Vars, Method){
  if( $(Id) ){
    if( !Method ) Method = 'get';
    if( !Vars ) Vars = '';
    new Request.HTML({'url':URL, 'method':Method,
      'onSuccess': function(html,responseElements,responseHTML){$(Id).set('html',responseHTML);},
      'onFailure': function(){ $(Id).set('text', URL); }
    }).send(Vars);
  }
}

var _IE = ( navigator.userAgent.toLowerCase().indexOf('msie')==-1 ) ? false : true;

/***** NICE SELECT *****************

CSS

#select_left{ margin:5px 0px 0px 26px; float:left; }
#select_left .nice_select{ position:relative; width:105px; height:15px; background-color:#efefeb; }
#select_left .nice_select span{ display:block; padding:1px 0px 0px 4px; width:87px; float:left; cursor:pointer; }
#select_left .nice_select_btn{ background:url(/images/arrow_select.gif) no-repeat 1px 0px; width:13px; height:13px; cursor:pointer; float:left; }
#select_left .nice_select ul{ position:absolute; top:21px; left:0px; width:105px; background-color:#efefeb; }
#select_left .nice_select ul li{ padding:1px 0px 1px 4px; cursor:pointer; }

*/

function nice_select_change(This, Id, Value){
  This.getParent().setStyle('display', 'none');
  This.getParent().getParent().getElement('span').innerHTML = This.innerHTML;
  $(Id).value = Value;
}

function nice_select(Id, Default){
  var Select = $(Id).getElement('select');
  var Selected = false;
  var Name = Select.name;
  var InputId = 'input_'+Id;
  var UlId = 'ul_'+Id;
  var HTML = '<span class="position:absolute; display:block; z-index:10;"><input type="hidden" name="'+Name+'" value="" id="'+InputId+'" />'+
    '<div class="nice_select"><span>'+Default+'</span><div class="nice_select_btn" onclick="javasctipt:objPopup(\''+UlId+'\');"></div>'+
    '<ul id="'+UlId+'" class="block" style="display:none;">';
  var Values = new Array();
  $(Id).getElements('option').each( function(el, i){
    if( !Selected && el.selected ) Selected = i;
    Values[i] = el.value;
    HTML += '<li>'+el.innerHTML+'</li>';
  });
  HTML += '</ul></div></span>';

  $(Id).innerHTML = HTML;
  $(Id).getElements('li').each( function(el, i){
    el.addEvent('click', function(){ nice_select_change(el, InputId, Values[i]); });
    if( Selected==i ) el.fireEvent('click');
  });
}

/***********************/

function change_connection(el){
  var BasketImg = '';
  if( el.checked ){
    var_prefix = 'add_';
    BasketImg = 'images/basket.jpg';
  }
  else{
    var_prefix = 'del_';
    BasketImg = 'blank.gif';
  }
  URL = $(el).getParent().getElement('.change_connection_path').value;
  AjaxRequest('top_basket', URL+'?mode=ajax&basket_refresh=1&'+var_prefix+el.getProperty('name')+'='+el.value+'&return=ajax');
  AjaxRequest('product_price', URL+'?mode=ajax&p_price=1');
  $(el).getParent().getParent().getElement('img.icon_basket').src = BasketImg;
}

window.addEvent('domready', function(){


  var ContLeft = $('container').getPosition().x;
  var ContWidth = $('container').getSize().x;
  var BodyWidth = $('body').getSize().x;
  var BodyHeight = $('body').getSize().y;
  // set margins background
  $('marg_left').setStyles({ 'left':-ContLeft, 'width':ContLeft });
  $('marg_right').setStyles({ 'left':ContWidth, 'width':BodyWidth-ContLeft-ContWidth });

  // set footer height
  var FooterHeight = BodyHeight-$('footer').getPosition().y;
  if( FooterHeight-$('footer').getSize().y>0 )
    $('footer').style.height = FooterHeight+'px';


/************************ PRODUCT TABS ************************************/

  // set width of product tabs
  if( isObj('prod_tabs') ){
    // widths - 20px - padding-right, 10px - firs element
    var Count = $$('#prod_tabs li span').length+1;
    var Width =  Math.floor( ($('prod_tabs').getSize().x-(5*Count))/Count);
    $$('#prod_tabs li span').setStyle('width', Width);
  }
  // set actions
  var ProdTab = new Array();
  var ProdBody = new Array();
  var ProdActive = 0;
  if( isObj('prod_tabs') && isObj('prod_body') ){
    $$('#prod_body li').each( function(el,i){ ProdBody[i] = el; });
    $$('#prod_tabs li span').each( function(el,i){ ProdTab[i] = el; });
    var ProdTabLast = ProdTab.length-1;
    ProdTab.each( function(el,i){
      el.addEvent('click', function(){
        ProdBody[ProdActive].addClass('hide');
        ProdBody[i].removeClass('hide');
        // deselect tab
        if( ProdActive>0 ) ProdTab[ProdActive-1].setProperty('class','');
        else if( ProdActive==0 ) $$('#prod_tabs').getElement('li').setProperty('class','first');
        ProdTab[ProdActive].getParent().setProperty('class','');
        if( ProdActive!=ProdTabLast ) ProdTab[ProdActive].setProperty('class','');
        else ProdTab[ProdActive].setProperty('class','last');
        // select tab
        if( i>0 ) ProdTab[i-1].setProperty('class','active-right');
        else if( i==0 ) $$('#prod_tabs').getElement('li').setProperty('class','first_active');
        ProdTab[i].getParent().setProperty('class','active');
        if( i!=ProdTabLast ) ProdTab[i].setProperty('class','active-left');
        else ProdTab[i].setProperty('class','last_active');

        ProdActive = i;
      });
    });
    ProdTab[0].fireEvent('click');
  }
/******* SEE ALSO TABS ******************************/
  var SA_Active = 0;
  var SA_Tab = new Array();
  var SA_Body = new Array();
  if( isObj('prod_seealso') ){
    $$('#prod_seealso .btn_num').each( function(el,i){ SA_Tab[i] = el; });
    $$('#prod_seealso .tabs li').each( function(el,i){ SA_Body[i] = el; });
    $$('#prod_seealso .btn_num').each( function(el,i){
      el.addEvent('click', function(){
        // deselect tab
        SA_Tab[SA_Active].removeClass('btn_num_active');
        SA_Body[SA_Active].setProperty('class', 'hide');
        // select tab
        if( !SA_Tab[i].hasClass('btn_num_active') ) SA_Body[i].addClass('btn_num_active');
        SA_Tab[i].addClass('btn_num_active');
        SA_Body[i].setProperty('class', '');

        SA_Active = i;
      });
    });
    SA_Tab[0].fireEvent('click');
  }
/****** CHANGE SUBPRODUCTS ********************/
  if( $('product_big') ){
    $$('input.change_subproduct').each( function(el){
      el.addEvent('click', function(){
        document.location.href = el.value;
      });
    });
  }
  //if( $('product_big') ){
  //  var Lines = new Array();
  //  var ActiveLine = 0;
  //  $$('#subproduct .line').each( function(el,i){ Lines[i] = el; });
  //  Lines.each( function(line,i){
  //    line.getElements('.change_subproduct').each( function(link){
  //      var URL = '';
  //      var TagName = link.tagName.toLowerCase();
  //      if( TagName=='input' )
  //        URL = link.value;
  //      else if( TagName=='a' ){
  //        URL = link.href;
  //        link.href = 'javascript:void(0);';
  //      }
  //      link.addEvent('click', function(){
  //        Lines[ActiveLine].getElement('input.change_subproduct').checked = 0;
  //        Lines[i].getElement('input.change_subproduct').checked = 1;
  //        ActiveLine = i;
  //        AjaxRequest('product_top', URL+'?mode=ajax&p_top=1');
  //        AjaxRequest('tab_connection', URL+'?mode=ajax&p_con=1');
  //        AjaxRequest('tab_delivery', URL+'?mode=ajax&p_del=1');
  //      });
  //    });
  //  });
  //}



/********************************************/

  if( isObj('login_module_form') && isObj('top_login') ){
    $('top_login_link').addEvent('click', function(){
      new Fx.Morph('top_login', {duration:400, transition:Fx.Transitions.Sine.easeOut, onComplete: function(){ this.setStyle('display','none'); } }
                   ).start({ 'opacity':[1,0] });
      $('login_module_form').setStyles({ 'display':'block', 'opacity':0 });
      new Fx.Morph('login_module_form', {duration:400, transition:Fx.Transitions.Sine.easeIn } ).start({ 'opacity':[0,1] });
    });
  }

  /**************************************/

  if( _IE ){
    $$('.form_submit').each( function(el){
      el.addEvent('keydown', function(e){
        if( e.key=='enter' ) el.getParent('form').submit();
      });
    });
  }
  $$('.parent_submit').each( function(el){
    el.addEvent('click', function(){
      $(el).getParent('form').submit();
    });
  });

  /**************************************/

  nice_select('select_left', 'wybierz');

});
