// Options

var lightbox_options = {
	'callbackOnClose' : function() { update_minicart(); clear_hash(); },
	'frameWidth' : 800,
	'frameHeight' : 500,
	'padding' : 0,
	'overlayOpacity' : 0.8,
	'overlayColor' : '#888'
};

function update_minicart() {
  var link = jQuery(this);
  var cart = jQuery("#minicart");

  if (cart[0] === undefined) {
    return;
  }

  hide_minicart();
  jQuery.post(
  	link.attr("href"), 
  	{update_minicart: 1},
  	function(data) {
      cart.html(data);
      update_fancybox();
      show_minicart();
    }
  );
  
  return false;
};

function hide_minicart() {
	jQuery("#loading").show();
	jQuery("#minicartdata").hide();
	return false;
}

function show_minicart() {
	jQuery("#loading").hide();
	jQuery("#minicartdata").show();
	return false;
}

function xls_logout() {
	hide_minicart();
	jQuery.get(xlsws_root + 'index.php', { xlspg: 'logout' }, update_minicart );
}

function popup($url, $name) {
	window.open($url, $name, 'width=800,height=500,toolbar=yes,status=no,scrollbars=yes' )
}

function update_fancybox() {
	jQuery("a.fancybox").fancybox(lightbox_options);
	
	jQuery("a.fancybox, a.fancybox > img").click(function() {

		var hash = this.id.substring(0, this.id.length - 2);
		window.location.hash = hash;
	})
	return false;	
}

function clear_hash() {
	if (window.location.hash != '')
		window.location.hash = '_';
}

function autorun_fancybox() {

	var hash = window.location.hash;

	if (hash != '#_' && hash != '') {
		jQuery(hash + '-l').fancybox(lightbox_options).trigger('click');
	} else {
		jQuery("a.xls_autorun").fancybox(lightbox_options).trigger('click');
	}
	
	return false;
}

jQuery(function(){

	// Paginate things
	jQuery(".paginate .product_list_item").quickpaginate({ perpage: 12, pager: jQuery(".results_counter") });

	// Fancybox
	update_fancybox();
	autorun_fancybox();
	update_minicart();
});

