/* sIFR inicializace */
var ffont = { src: 'http://dev.cybergroup.cz/u_andersena/images/sifr.swf',ratios: [7,1.32,11,1.31,13,1.24,14,1.25,19,1.23,27,1.2,34,1.19,42,1.18,47,1.17,48,1.18,69,1.17,74,1.16,75,1.17,1.16] };
/**/
//sIFR.delayCSS  = true;
sIFR.activate(ffont);
/**/


var formValues = new Array();

$(function() {
	$('.lightbox a, a.lightbox').lightBox({fixedNavigation:false, txtImage:'Obrázek', txtOf:'z' });
});


var declining = {
  states: 
  {
    "karton": [
      'karton',
      'kartony',
      'kartonů'
    ],
    "lahev": [
      'láhev',
      'láhve',
      'lahví'
    ]
  },
  
  init: function()
  {
	
    if( !$('#obchod,#vinoPopis').length ) return false;
	
	var mElem = $('#obchod input[name=mnozstvi],#vinoPopis input#mnozstvi');
	
	mElem.focus(function(){ declining.startCapture( $(this).attr('id') ); });
	mElem.blur(function(){ declining.stopCapture( $(this).attr('id') ); });
	
  },
  
  startCapture: function(timerId)
  {
  	declining.stack.timer = window.setTimeout('declining.decline(\''+timerId+'\'); declining.startCapture(\''+timerId+'\')', 100);
  },
  
  stopCapture: function(timerId)
  {
  	window.clearTimeout(declining.stack.timer);	
	declining.decline(timerId);
  },
  
  decline: function(elId)
  {
    var countVal = parseInt( $('#'+elId).attr('value') );
	if(!(countVal>=0)) countVal = 0;	
	$('#'+elId).attr('value', countVal);
    var label = $('label[for='+elId+']');

	// store current text
    declining.stack.labelVal = label.text();
    
    // find state in states array
	$.each(declining.states, function(key) {
		// is current state in array
	 	if( $.inArray(declining.stack.labelVal, $(this)) >= 0 )
		{
			// save array key for later use
			declining.stack.state = key;
		}
	});    
	// get matching state array
    var cState = declining.states[declining.stack.state];
    
	// change state
    if(countVal==1)
    {
       var nState = cState[0];
    }
    else if(countVal>=2 && countVal<=4)
    {
       var nState = cState[1];    
    }
    else// if(countVal>=5 || countVal==0)
    {
       
	   var nState = cState[2];    
    }
    label.text(nState);    
    
  },
  
  stack: { timer: {} } 
}

var ajaxRating = {
  config:
  {
    ratingLink: '.ratingBox a',
	ratingPage: 'include/ajax/rating.php'
  },
  init: function()
  {
    if(!$(this.config.ratingLink).length) return false;
    
    $(this.config.ratingLink).click(function(){
		ajaxRating.rate( $(this) );
		return false;
	});
    
  },
  ret: true,
  
  rate: function(el)
  {
    var url = el.attr('href').split('?');
	ajaxRating.url = url[1];
	url = el.attr('href').split('/');
	ajaxRating.url = url[0]+'//'+url[2]+'/' + ajaxRating.config.ratingPage + '?' +ajaxRating.url;
	
	ajaxRating.el = el;
	
    $.ajax({
	     type: "GET",   
	     url: ajaxRating.url,
	     data: '',//ajaxRating.sendData,
	     success: function(data)
	     {
		 	ajaxRating.el.parents('.ratingBox').parent().css('vertical-align', 'middle'/*IE8!*/).find('.forBlind').remove();
			ajaxRating.el.parents('.ratingBox').replaceWith(data);
	   	 },
		 error: function(rq,textStatus)
		 {
           window.location.href(url+sendData);   
  		 }
    }); 
    
    return false;

  }

}

var regForm = {
	
	init: function()
	{
		if( !$('#registrationForm').length ) return false;
		
		regForm.cctrlReg = $('#registrationForm input[name=registration-eshop]');
		regForm.ctrlReg = $('#registrationForm #registration-eshop-yes');
		regForm.blockReg = $('#registrationForm #registration-eshop-yes-true');
		
		regForm.cctrlAddr = $('#registrationForm input[name=registration-equal]');	
		regForm.ctrlAddr = $('#registrationForm #registration-equal-yes');		
		regForm.blockAddr = $('#registrationForm #registration-equal-yes-true');
		

		regForm.cctrlReg.click(regForm.toggleReg);
		regForm.cctrlAddr.click(regForm.toggleAddr);
		
		regForm.toggleReg();
		regForm.toggleAddr();
		
	},
	
	toggleReg: function()
	{
		if(regForm.ctrlReg.attr('checked'))
		{
			regForm.blockReg.show();
		}
		else
		{
			regForm.blockReg.hide();
		}
	},
	
	toggleAddr: function()
	{
		if(!regForm.ctrlAddr.attr('checked'))
		{
			regForm.blockAddr.show();
		}
		else
		{
			regForm.blockAddr.hide();
		}
	}
	
}

var loginPopup = {
	
	init: function()
	{
		if( !$('#loginPopup').length ) return false;
		
		$('#buttonLogin').click(loginPopup.toggle);
		
	},
	
	toggle: function()
	{
		$('#loginPopup').toggleClass('hidden');
		
		  
	        var colors = new Array();
	        colors[1] = '#539cce';
	        colors[2] = '#539cce';
        
	        var fonts = new Array();
	        fonts[3] = '13px';
			
			sIFR.replace(ffont,
			{
			    selector: 'legend.sifr',
			    css:
			      [
			      '.sIFR-root { font-size: '+fonts[3]+'; font-weight: normal; color: '+colors[1]+'; }',
			      'a { color: '+colors[1]+'; text-decoration: none; }',
			      'a:link { color: '+colors[1]+'; }',
			      'a:visited { color: '+colors[1]+'; }',
			      'a:hover { color: '+colors[2]+'; }',
			      'a:focus { color: '+colors[2]+'; }',
			      'a:active { color: '+colors[2]+'; }'
			      ],
			    wmode: 'transparent',
			    fitExactly: true
			});
		
		return false;
	}
	
}

/*******************************************
 *
 *  on DOM ready
 *
 ********************************************/
$(document).ready(function()
{

	declining.init();
	ajaxRating.init();
	regForm.init();
	loginPopup.init();

  jQuery.easing.def = 'easeOutCubic';

  // fix eolas
  var objects = document.getElementsByTagName("object");
  for (var i=0; i<objects.length; i++)
  {
    objects[i].outerHTML = objects[i].outerHTML;
  }

  var colors = new Array();
  colors[1] = '#539cce';
  colors[2] = '#539cce';
	
	colors[3] = '#ffffff';	
	colors[4] = '#ffffff';
	
	colors[5] = '#ffffff';	
	colors[6] = '#8fc6ed';
	colors[7] = '#c2da98';	

  var fonts = new Array();
  fonts[1] = '20px';
	fonts[2] = '17px';
	fonts[3] = '13px';

  /* sIFR replace */
	
  sIFR.replace(ffont,
  {
    selector: 'h2.sifr',
    css:
      [
      '.sIFR-root { font-size: '+fonts[1]+'; font-weight: normal; color: '+colors[1]+'; letter-spacing: -0.5;}',
      'a { color: '+colors[1]+'; text-decoration: none; }',
      'a:link { color: '+colors[1]+'; }',
      'a:visited { color: '+colors[1]+'; }',
      'a:hover { color: '+colors[2]+'; }',
      'a:focus { color: '+colors[2]+'; }',
      'a:active { color: '+colors[2]+'; }'
      ],
    wmode: 'transparent',
    fitExactly: true
  }); 
	
	sIFR.replace(ffont,
  {
    selector: 'h3.sifr',
    css:
      [
      '.sIFR-root { font-size: '+fonts[2]+'; font-weight: normal; color: '+colors[1]+'; letter-spacing: -0.5;}',
      'a { color: '+colors[1]+'; text-decoration: none; }',
      'a:link { color: '+colors[1]+'; }',
      'a:visited { color: '+colors[1]+'; }',
      'a:hover { color: '+colors[2]+'; }',
      'a:focus { color: '+colors[2]+'; }',
      'a:active { color: '+colors[2]+'; }'
      ],
    wmode: 'transparent',
    fitExactly: true
  });
	
	sIFR.replace(ffont,
  {
    selector: 'h4.sifr',
    css:
      [
      '.sIFR-root { font-size: '+fonts[3]+'; font-weight: normal; color: '+colors[1]+'; }',
      'a { color: '+colors[1]+'; text-decoration: none; }',
      'a:link { color: '+colors[1]+'; }',
      'a:visited { color: '+colors[1]+'; }',
      'a:hover { color: '+colors[2]+'; }',
      'a:focus { color: '+colors[2]+'; }',
      'a:active { color: '+colors[2]+'; }'
      ],
    wmode: 'transparent',
    fitExactly: true
  });
	
$(function() {
	$('.lightbox a, a.lightbox, a[rel*=lightbox]').lightBox({
    overlayBgColor:'#000',
    overlayOpacity:0.8,
    fixedNavigation:false,
    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/transparent.gif',
    containerBorderSize:10,
    containerResizeSpeed:400,
    txtImage:'Obrázek: ',
    txtOf:'z',
    keyToClose:'c',
    keyToPrev:'p',
    keyToNext:'n',
    imageArray:[],
    activeImage:0
  });
});





  /* slide */
  $('#slideArea').after('<span id="slideRight" class="slider"><a href="#"><!-- --></a></span><span id="slideLeft" class="slider"><a href="#"><!-- --></a></span>');

  var slideMarginLeft    = 0;
  var maxSlideMarginLeft = 0;
  var itemWidth          = parseInt($('#slideArea .item').css('margin-left')) + parseInt($('#slideArea .item').css('margin-right')) +
                           parseInt($('#slideArea .item').css('border-left-width')) + parseInt($('#slideArea .item').css('border-right-width')) +
                           parseInt($('#slideArea .item').css('padding-left')) + parseInt($('#slideArea .item').css('padding-right')) +
                           $('#slideArea .item').width();
  var countItem          = 0;

// TODO: počítat z šířky slideArea a itemWidth počet zobrazených položek

  $('#slideRight a').click(
    function()
    {
      if ( slideMarginLeft > maxSlideMarginLeft )
      {
        slideMarginLeft -= itemWidth;
        $('.items').animate({marginLeft: slideMarginLeft }, slowEffectTime);
//         $('#slideRight').removeClass('hidden');
      }
      if ( slideMarginLeft <= maxSlideMarginLeft )
      {
        $('#slideRight a').fadeOut('normal');
      }
      if ( slideMarginLeft < 0 )
      {
        $('#slideLeft a').fadeIn('normal');
      }
      return false;
    }
  );

  $('#slideLeft a').click(
    function()
    {
      if ( slideMarginLeft < 0 )
      {
        slideMarginLeft += itemWidth;
        $('.items').animate({marginLeft: slideMarginLeft }, slowEffectTime);
      }
      if ( slideMarginLeft >= 0 )
      {
        $('#slideLeft a').fadeOut('normal');
      }
      if ( slideMarginLeft > maxSlideMarginLeft )
      {
        $('#slideRight a').fadeIn('normal');
      }
      return false;
    }
  );

  countItem = $('.item').length;

  maxSlideMarginLeft = ( -itemWidth * ( countItem - 3 ) );

  if ( slideMarginLeft >= 0 )
  {
    $('#slideLeft a').hide();
  }

  if ( maxSlideMarginLeft >= 0 || countItem == 0 )
  {
    $('#slideRight a').hide();
  }

  /* change events */
  /*$('select').change(
    function()
    {
      $(this).parents('form').submit();
    }
  );*/

  /* focus events */
  $('#search-string').focus(
    function()
    {
      var itemID = $(this).attr('id');

      if ( formValues[itemID] == null || $(this).val() == formValues[itemID] )
      {
        formValues[itemID] = $(this).val();
        $(this).val('');
      }
    }
  );

  /* onblur events */
  $('#search-string').blur(
    function()
    {
      if( $(this).val() == '' )
      {
        $(this).val(formValues[$(this).attr('id')]);
      }
    }
  );
  
  

});

function openCloseNode(el)
{
	el.parentNode.className = el.parentNode.className == 'open' ? 'close' : 'open';
}

function change(el, increment)
{
	var elItemCount = document.getElementById('itemCount-'+el );
	var itemCount;
	if ( elItemCount.value < 99 && increment > 0 || elItemCount.value > 1 && increment < 0 )
	{
		// item count
		itemCount					= parseInt(elItemCount.value) + parseInt(increment);
		elItemCount.value	= itemCount;
	}
}

