var blank = {
  config:
  {
    wrapperId: '#wrapper'
  },
  init: function()
  {
    if(!$(this.config.wrapperId).length) return false;
  }
}


// pridat stranku do zaloziek
function AddFavorite(linkObj,addUrl3,addTitle3)
{
     if (document.all && !window.opera){
         window.external.AddFavorite(addUrl3,addTitle3);
         return false;
     }
     else if (window.opera && window.print)     {
         linkObj.title = addTitle3;
         return true;
     }
     else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function')) {
         window.sidebar.addPanel(addTitle3,addUrl3,'');
         return false;
     }
     window.alert('Po potvrzení stiskněte CTRL-D,\nstránka bude přidána k Vašim oblíbeným odkazům.');
     return false;
}

//rozbalovani produktu pri prejeti mysi
var productsSliding = {
  config:
  {
    rowWrapper: '#boxTips ul li',
    imageWrapper: 'p.tipImg',
    animationDuration: 200
  },
  //break
  //break
  init: function()
  {
    if(!$(this.config.rowWrapper).length) return false;
    $(this.config.rowWrapper+':gt(0)').css('padding-top','0px').children(this.config.imageWrapper).hide();
    //nuluj produkty krome prvniho
    this.config.rowPaddingTop = $(this.config.rowWrapper+':eq(0)').addClass('opened').css('padding-top');
    //uloz prave otevreny produkt
    productsSliding.opened = $(this.config.rowWrapper+':eq(0)');
    //nastav prepinac
    productsSliding.processing=false;
    //nastavit napevno vysku obalu
    $(this.config.rowWrapper).parent().height( $(this.config.rowWrapper).parent().height() );
    //nstavit udalost
    $(this.config.rowWrapper).hover(productsSliding.show, function(){});
    $(this.config.imageWrapper+' a').hover(function(){ $(this).parent().parent().prev('.tipName').children().addClass('hover') }, function(){ $(this).parent().parent().prev('.tipName').children().removeClass('hover'); });
  },
  show: function()
  {
    if(productsSliding.processing==false && !$(this).hasClass('opened'))
    {
      productsSliding.processing = true;
      productsSliding.hideAll();
      $(this).addClass('opened');
      $(this).animate({ paddingTop: productsSliding.config.rowPaddingTop },productsSliding.config.animationDuration).queue(function(){
        $(this).children(productsSliding.config.imageWrapper).fadeIn(productsSliding.config.animationDuration);
        productsSliding.opened = $(this);
        productsSliding.processing=false;
        $(this).dequeue();
      });
    }
  },
  hideAll: function()
  {
    $(productsSliding.opened).removeClass('opened').animate({ paddingTop: 0 },productsSliding.config.animationDuration).children(productsSliding.config.imageWrapper).hide().queue(function(){
      $(this).dequeue();
    });
  }
}

var menu = {
  config:
  {
    menuList: '#boxMenuCatalog > ul > li',
    noSubmenuClass: 'noSubmenu'
  },
  init: function()
  {
    if(!$(this.config.menuList).length) return false;
    
    //onclick
    $(this.config.menuList+' > a').click(menu.toggle)
      //polozka nema submenu - priradit class
      .filter(function(){ return !$(this).next('ul').length }).parent().addClass(this.config.noSubmenuClass);  
    
    //neaktivni a zavrene polozky skryt
    $(this.config.menuList+' > a:not(.opened):not(.act)').addClass('closed').next('ul').hide();
    
    //otevrit submenu, pokud obsahuje aktivni polozku
    $(this.config.menuList+' > ul > li a.act').parent().parent().show().prev('a').addClass('opened').removeClass('closed');
      
  },
  toggle: function()
  {
    $(this).toggleClass('opened').toggleClass('closed').next('ul').toggle();
    return false;
  }
}

function showLogin()
{
  $('div#loginInfo').hide();
  $('form#sideLogin').show();
  return false;
}

var miniPhotogallery = {
  config:
  {
    detailImage: '#prdImage a img', 
    imagesList: '#prdIList a',
    imagesListInner: '.sliderInner',
    prevButton: '#prdcNext',
    nextButton: '#prdcPrev',
    denyButtonClass: 
    {
      add: 'deny'
    },
    imageWidth: 76, //72+4
    imagesInRow: 2,
    sliderControls: 
    {
      insertCode: '<p id="prdControls"><a href="#" id="prdcPrev">&laquo;<span></span></a><a href="#" id="prdcNext">&raquo;<span></span></a></p>',
      insertAfterElement: '#prdIList'
    }
  },
  init: function()
  {
    if(!$(this.config.detailImage).length) return false;
    //vlozit ovladaci prvky
    $(this.config.sliderControls.insertAfterElement).after(this.config.sliderControls.insertCode);
    //zkontrolovat pocet obrazku
    this.imagesCount = $(this.config.imagesListInner+' img').length;
    //priradit udalosti
    $(this.config.prevButton).click(miniPhotogallery.prev);
    $(this.config.nextButton).click(miniPhotogallery.next);
    //zjistit maxima a minima - margin
    this.mLeftMax = 0;
    this.mLeftMin = - this.imagesCount * this.config.imageWidth + this.config.imagesInRow * this.config.imageWidth;
    //kontrola poctu
    $(this.config.nextButton).addClass('deny');
    //$(this.config.imagesListInner+' a').unbind('click').click(miniPhotogallery.showDetail);
    if( $(this.config.imagesListInner+' img').length <= this.config.imagesInRow )
    {
      $(this.config.prevButton).addClass('deny');
    }  
  },
  prev: function()
  {
    miniPhotogallery.mLeft = parseInt($(miniPhotogallery.config.imagesListInner).css('margin-left'))-miniPhotogallery.config.imageWidth;
    if(miniPhotogallery.mLeftMin <= miniPhotogallery.mLeft)
    {
     $(miniPhotogallery.config.imagesListInner).animate({ marginLeft: '-='+miniPhotogallery.config.imageWidth+'px' });
     $(miniPhotogallery.config.nextButton).removeClass('deny');
    }
    if(miniPhotogallery.mLeftMin >= miniPhotogallery.mLeft)
    {
     $(miniPhotogallery.config.prevButton).addClass('deny');
    }
    return false;
  },
  next: function()
  {
    miniPhotogallery.mLeft = parseInt($(miniPhotogallery.config.imagesListInner).css('margin-left'))+miniPhotogallery.config.imageWidth;
    if(miniPhotogallery.mLeftMax >= miniPhotogallery.mLeft)
    {
     $(miniPhotogallery.config.imagesListInner).animate({ marginLeft: '+='+miniPhotogallery.config.imageWidth+'px' });
     $(miniPhotogallery.config.prevButton).removeClass('deny');
    }
    if(miniPhotogallery.mLeftMax <= miniPhotogallery.mLeft)
    {
     $(miniPhotogallery.config.nextButton).addClass('deny')
    }
    return false;
  },
  showDetail: function()
  {
    var url = $(this).attr('href');
    $(miniPhotogallery.config.detailImage).attr('src',url).parent('a').attr('href',url); 
    return false;
  }
}

//zobrazit / skryt box ovladany relativnimi odkazy
var tabs = {
  config:
  {
    tabs: '#prdTabs li',
    detail: '.tabDetail'
  },
  init: function()
  {
    if(!$(this.config.tabs).length) return false;
    //akce zalozek
    $(this.config.tabs).children('a').click(tabs.open);
    //schovat nadpisy
    $(this.config.detail).children('h4').hide();
    //prvni zvyraznit
    $(this.config.tabs+':first').addClass('act');
    //nastavit citac
    var gt = 0;
    //pokud je nutnost nektery detail zobrazit
    if($(this.config.detail+'.forceVisibility').length)
    {
      gt = -1;
      //zrus zvyrazneni
      $(tabs.config.tabs).removeClass('act');
      //zvyrazni otevrenou
      $(this.config.tabs).find('a[href=#'+$(this.config.detail+'.forceVisibility').attr('id')+']').parent().addClass('act');
    }  
    //zavri vse krome prvni nebo otevrene 
    $(this.config.detail+':gt('+gt+'):not(.forceVisibility)').hide();
    
    //form diskuze
    //zobraz formular
    $('#addComment').click(tabs.addComment);
    //skryj formular, pokud neni nutno nechat otevreny
    $('#addnew:not(.forceVisibility)').hide(); //hidden kvuli ie6
    //pokud je nutno nechat otevreny
    if($('#addnew.forceVisibility').length)
    {
     $('#commentAdd').hide();
    }
  },
  open: function()
  {
    $(tabs.config.detail).hide();
    $( $(this).attr('href') ).show()
    $(tabs.config.tabs).removeClass('act');
    $(this).parent().addClass('act');
    return false;
  },
  addComment: function()
  {
    $(this).hide();
    $('#addnew').show(200); //time nutny - jinak CSS bug IE
    return false;
  }
}

var counterPieces = {
  config:
  {
    elCounter: 'input.counterPieces',
    counterCtrlPlus:  '<a href="#" class="counter counterPlus" onclick="return counterPieces.change(1,this)"><span>+</span></a>',
    counterCtrlMinus: '<a href="#" class="counter counterMinus" onclick="return counterPieces.change(-1,this)"><span>-</span></a>'
  },
  init: function()
  {
    if(!$(this.config.elCounter).length) return false;
    $(this.config.elCounter).after(this.config.counterCtrlPlus+this.config.counterCtrlMinus);
  },
  change: function(step, el)
  {
    //first control
    inputEl = $(el).prev(this.config.elCounter);
    //second control
    if(!inputEl.length)
      inputEl = $(el).prev().prev(this.config.elCounter);
    
    if( parseInt(inputEl.attr('value')) + step >=0 )
     inputEl.attr('value', parseInt(inputEl.attr('value')) + step );
    
    return false;
  }
}

//auto rollovers
var imageHover = {
  elementsFilter: '*[src*=but_][src*=_normal]',
  replaceSrcNormal: '_normal.',
  replaceSrcHover:  '_hover.',
  init: function()
  {
    if(!$(this.elementsFilter).length) return false;
    $(this.elementsFilter).each(function(key){
      imageHover.images[key] = new Image();
      imageHover.images[key].src = $(this).attr('src').replace(imageHover.replaceSrcNormal,imageHover.replaceSrcHover);
      if(imageHover.isLoaded(imageHover.images[key]))
      {
        $(this).hover(
          function(){ $(this).attr('src',$(this).attr('src').replace(imageHover.replaceSrcNormal,imageHover.replaceSrcHover)); },
          function(){ $(this).attr('src',$(this).attr('src').replace(imageHover.replaceSrcHover,imageHover.replaceSrcNormal)); }
        );
      }
    });
  },
  isLoaded: function(el)
  {
     if (!el.complete) {
        return false;
     }
     if (typeof el.naturalWidth != "undefined" && el.naturalWidth == 0) {
        return false;
     }
     return true;
  },
  images: new Array()
}

var ajaxBuy = {
  config:
  {
    buyLink: '.prBuy a',
    message: 'Produkt byl vložen do košíku.'
  },
  init: function()
  {
    if(!$(this.config.buyLink).length) return false;
    
    $(this.config.buyLink).click(ajaxBuy.buy);
    
  },
  ret: true,
  buy: function()
  {
    var url = $(this).attr('href');	
    	
		$.ajax({
	     type: "GET",   
	     url: url,
	     success: function(data)
	     {
					alert(ajaxBuy.config.message);
					ajaxBuy.refreshCart();
		   },
			 error: function()
			 {
          window.location(url);   
  		 }
    }); 
    
    return false;

  },
  refreshCart: function()
  {
    var url = '/?page=ajax-kosik';
		$.ajax({
	     type: "GET",   
	     url: url,
	     success: function(data)
	     {
					$('#box_cart').replaceWith(data);
		   }		 
			 
    });
  }
}

//--------------------------------------------------------------------------

$(document).ready(function() {     

  productsSliding.init();
  //menu.init();
  $('a[rel*=lightbox]').lightBox({ 
    overlayBgColor:'#D3E0B7', 
    containerResizeSpeed:200,    
    imageLoading:'/images/lightbox-ico-loading.gif',
    imageBtnPrev:'/images/lightbox-btn-prev.png',
    imageBtnNext:'/images/lightbox-btn-next.png',
    imageBtnClose:'/images/lightbox-btn-close.png',
    imageBlank:'/images/lightbox-blank.gif'
  });
  miniPhotogallery.init();
  tabs.init();
  counterPieces.init();
  imageHover.init();
  //ajaxBuy.init();
  
  if(typeof(console)!='undefined')
  {
    if( ! window.location.href.match(/^http[s]?:\/\/www.golf-prodejna.cz/) )
    {
      console.log( 'GOOGLE' );
    }
  }
  
})

// translate -----------------------------------------------------------

function dominnerText(node, text) { while (node.hasChildNodes()) { node.removeChild(node.firstChild); } node.appendChild(document.createTextNode(text)); }
function OpenNewWindow(soubor, jmeno, dx, dy, vlastnosti)
{
window.open(soubor, jmeno, "left=" + ((screen.width - dx) / 2) + ", top=" + ((screen.height - dy) / 2 - 16) + ", width=" + dx + ", height=" + dy +", " + vlastnosti);
// vlastnosti: channelmode=no, status=no, toolbar=no, menubar=no, location=no, scrollbars=no, resizable=yes);
}
if (top.location != self.location) top.location = self.location;

function doTranslate(lang_pair)
{
  if (lang_pair.value)
  {
    lang_pair = lang_pair.value;
  }
  if (location.hostname == 'www.golf-prodejna.cz' && lang_pair == 'cs|cs')
  {
    return;
  }
  else if (location.hostname != 'www.golf-prodejna.cz' && lang_pair == 'cs|cs')
  {
    location.href = unescape(gfg('u'));
  }
  else if (location.hostname == 'www.golf-prodejna.cz' && lang_pair != 'cs|cs')
  {
    location.href = 'http://translate.google.com/translate?client=tmpg&hl=en&langpair='+lang_pair+'&u='+escape(location.href);
  }
  else
  {
    location.href = 'http://translate.google.com/translate?client=tmpg&hl=en&langpair='+lang_pair+'&u='+unescape(gfg('u'));
  }
}

function gfg(name)
{
  name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
  var regexS="[\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(location.href);
  if (results == null)
  {
    return '';
  }
  return results[1];
}