function shareTwitter(title, link){
  window.open('http://twitter.com/home?status=' + title + ': ' + link, 'share');
}
function shareFacebook(title, link){
  window.open('http://www.facebook.com/sharer.php?u=' + link + '&t=' + title, 'share');
}
function shareDraugiem(title, link){
  window.open('http://www.draugiem.lv/say/ext/add.php?link=' + link + '&title=' + title + '&titlePrefix=egramatnica.lv', 'share');
}
function setGMDCookie(name, value, cokie_expire){
  var expire = new Date();
  expire.setTime( expire.getTime() + parseInt(cokie_expire) );
  var str = "GMD_" + name + "=" + value + "; expires=" + expire.toGMTString() + "; path=/";
  document.cookie = str;
}
function getGMDCookie( name ) {
  var prefix = "GMD_" + name + "=";
  var begin = document.cookie.indexOf( "; " + prefix );
  if ( begin == -1 ) {
    begin = document.cookie.indexOf( prefix );
    if ( begin != 0 ) {
      return false;
    }
  } else {
    begin += 2;
  }
  var end = document.cookie.indexOf( ";", begin );
  if (end == -1){
    end = document.cookie.length;
  }
  var result = unescape(document.cookie.substring(begin + prefix.length, end));
  return result;
}
var fontSize = 11;
var maxFontSize = 18;
var minFontSize = 11;
var resizeFont = function(pixels){
  $('#catalogProduct, .article').css('font-size', pixels + 'px');
  setGMDCookie('font_size', pixels, 30 * 24 * 60 * 60);
}
var decreaseFont = function(){
  fontSize --;
  if (fontSize < minFontSize){
    fontSize = minFontSize;
  }
  resizeFont(fontSize);
}
var increaseFont = function(){
  fontSize ++;
  if (fontSize > maxFontSize){
    fontSize = maxFontSize;
  }
  resizeFont(fontSize);
}

var ebookLink = '';
function warnCart(){
	$('#cart-warning').css('display', 'block');
}
function warnCheckout(){
	$('#cart-warning').css('display', 'block');
}

$(document).ready(function(){
  var storedSize = getGMDCookie('font_size');
  if (storedSize != false){
    fontSize = parseInt(storedSize);
    if (fontSize > maxFontSize){
      fontSize = maxFontSize;
    } else if (fontSize < minFontSize){
      fontSize = minFontSize;
    }
    resizeFont(fontSize);
  }
  
  $('select.per-page').change(function(){
			var loc = String(window.location);
			if (loc.indexOf('?') > 0){
				window.location = loc + '&per-page=' + $(this).val();
			} else {
				window.location = loc + '?per-page=' + $(this).val();
			}
		});
		$('a.lightbox').fancybox();
		$('img.fancy').parent('a').fancybox();
		$('#cart-country').change(function(){
			window.location='?country=' + $(this).val();
		});
		$('#cart-delivery').change(function(){
			window.location='?delivery=' + $(this).val();
		});
		$('#cart-region').change(function(){
			window.location='?region=' + $(this).val();
		});
		$('#cart-zone-select input').click(function(){
			window.location='?zone=' + $(this).val();
		});
		$('.warn-ebook').click(function(){
			 $('.ebook-buy').attr('href', $(this).attr('href'));
			 $('#ebook-warning').css('display', 'block');
			 return false;
		});
		$('.ebook-cancel').click(function(){
      $('#ebook-warning').css('display', 'none');
			return false;
		});
		$('.cart-ok').click(function(){
      $('#cart-warning').css('display', 'none');
			return false;
		});
		
		$('#bill-delivery').change(function(){
			if ($('#bill-delivery').is(':checked')){
				$('#bill-firstname').val($('#firstname').val());
				$('#bill-lastname').val($('#lastname').val());
				//$('#bill-reg-num').val($('#reg-num').val());
				$('#bill-address').val($('#address').val());
				$('#bill-city').val($('#city').val());
				$('#bill-country').val($('#country').val());
				$('#bill-district').val($('#district').val());
				$('#bill-zip').val($('#zip').val());
			}
		});
		$('#chart').tabs({
			selected: 1
		});
});


