/*jQuery.fn.log = function (msg) {
	if ($.browser.mozilla) {
		console.log("%s: %o", msg, this);
	}
	return this;
};*/

Shadowbox.init({
	skipSetup: true, 
	clearCache: function() {
		each(S.cache, function(obj) {
			if( obj.el) {
				S.lib.removeEvent(obj.el, 'click', handleClick);
				try {
					delete obj.el.shadowboxCacheKey;
				} catch(e) {
					if (obj.el.removeAttribute) {
						obj.el.removeAttribute('shadowboxCacheKey');
					}
				}
			}
		});
		S.cache = [];
	}
});

function key(number) {
	return Math.floor(Math.random() * number);
}

function fetchImages(selector, table, link_name) {
	$.ajax({
		type	: "GET",
		url		: "/images.php",
		data	: "table=" + table + "&link_name=" + link_name + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
			
			Shadowbox.setup(".popup", {
				gallery:	link_name
			});
			
		}
	});
}

function fetchInfo(link_name) {
	$.ajax({
		type	: "GET",
		url		: "/info.php",
		data	: "link_name=" + link_name + "&key=" + key(10000),
		success	: function(html){
			$("#listing_info").empty();
			$("#listing_info").append(html);
		}
	});
}

function fetchGallery(width) {
	$.ajax({
		type	: "GET",
		url		: "/gallery.php",
		data	: "width=" + width + "&key=" + key(10000),
		success	: function(html){
			
			$("#gallery").empty();
			$("#gallery").append(html);
			
			Shadowbox.clearCache();
			
			$("#albums").ImageOverlay({
				overlay_text_color: '#fff' 
			});
		}
	});
}

function fetchAlbum(gallery_id, width) {
	$.ajax({
		type	: "GET",
		url		: "/gallery.php",
		data	: "width=" + width + "&gallery_id=" + gallery_id + "&key=" + key(10000),
		success	: function(html){
			
			$("#gallery").empty();
			$("#gallery").append(html);
			
			$("#images").ImageOverlay({
				overlay_text_color: '#fff' 
			});
			
			Shadowbox.setup("#gallery li a", {
				gallery:	"Gallery"
			});
		}
	});
}

function fetchMenu(position, selector) {
	$.ajax({
		type	: "GET",
		url		: "/menu.php",
		data	: "position=" + position + "&key=" + key(10000),
		success	: function(html){
			
			$(selector).empty();
			$(selector).append(html);
			
			if (position == 'top') {
				$("ul.sf-menu").supersubs({ 
					minWidth:    12, 
					maxWidth:    27, 
					extraWidth:  1    
				}).superfish({
					dropShadows : false
				});
			}
			
			if (position == 'left') {
				$("ul.sf-menu").superfish({
					dropShadows : false
				});
			}
			
		}
	});
}

function fetchAdverts(page, position, selector) {
	$.ajax({
		type	: "GET",
		url		: "/adverts.php",
		data	: "page=" + page + "&position=" + position + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function fetchLogin(selector) {
	$.ajax({
		type	: "GET",
		url		: "/login.php",
		data	: "key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function fetchNews(limit) {
	var selector = '#news';
	$.ajax({
		type	: "GET",
		url		: "/news.php",
		data	: "limit=" + limit + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
			
			/*Shadowbox.setup(".listing", {
				gallery:	"listing"
			});*/
			
		}
	});
}

function fetchSlideshow(name, width, height, selector) {
	
	$.ajax({
		type	: "GET",
		url		: "/slideshow.php",
		data	: "name=" + name + "&width=" + width + "&height=" + height + "&key=" + key(10000),
		success	: function(html){
			
			$(selector).empty();
			$(selector).append(html);
			
			$(selector).cycle({
				fx:     'fade', 
				timeout: 8000, 
				//next:   '#slideshow', 
				pause:   1,
				random:  1
				//before:  onBefore, 
				//after:   onAfter 
			});
		}
	});
}

function fetchBasket() {
	var selector = '#basket';
	$(selector).show();
	$.ajax({
		type	: "GET",
		url		: "/basket.php",
		data	: "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function emptyBasket() {
	var selector = '#basket';
	$.ajax({
		type	: "GET",
		url		: "/basket.php",
		data	: "empty=1&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function fetchSpecials() {
	var selector = '#specials';
	$(selector).show();
	$.ajax({
		type	: "GET",
		url		: "/specials.php",
		data	: "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

// TWINKLE
function randomNumber(numLow, numHigh) {
	var adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
	var numRand = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
	return numRand;
}

function makeSlide(selector, effect, seconds) {
	$(selector).cycle({
		fx:      effect, 
		timeout: seconds, 
		pause:   0,
		random:  1
	});
}

function twinkle(numLow, numHigh, effect) {
	
	makeSlide('#slideshow1_1', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow2_1', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow3_1', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow4_1', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow5_1', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow6_1', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow7_1', effect, randomNumber(numLow, numHigh));
	
	makeSlide('#slideshow1_2', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow2_2', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow3_2', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow4_2', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow5_2', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow6_2', effect, randomNumber(numLow, numHigh));
	makeSlide('#slideshow7_2', effect, randomNumber(numLow, numHigh));
	
}