// $.extend($.blockUI.defaults.overlayCSS, { backgroundColor: '#fff' });

// global application scope, which holds the app methods and variables:
var app = {
	notice: function(msg, type) {
		type = type || '';
		if ($("#notice").length > 0) {
			app.t = clearTimeout(app.t);
			$("#notice").hide().removeClass().addClass(type).html(msg).slideDown(400);
		} else {
			$(".pagetitle").after('<div id="notice" class="' + type + '">' + msg + '</div>').show();
		}
		app.t = setTimeout(function() { $("#notice").animate({ opacity: "hide" }, 400) }, 3000);
	},
	updateCart: function(data) { // data = json object, with data.cartstatus = string
		if (data.cartstatus) {
			$("#cartstatus").html(data.cartstatus);
		}
	},
	closeDialog: function() {
		if ($.browser.msie) $("#dialog .shadow").css("backgroundImage", "none");
		$.unblockUI();
	}
}

//
$(function() {
	/*
	$("a.icon-var").click(function(e){
	$.getJSON(e.target.href, function(data){
	app.notice(data.notice);
	});
	return false;
	});
	*/
	$("a.icon-var").click(function(e) {
		$.getJSON(e.target.href, function(data) {
			app.notice(data.notice);
		});
		return false;
	});

	$(document).bind('keydown', function(e) {
		var key = e.which || e.keyCode || -1;
		if (key == 27) {
			app.closeDialog();
			$(document).unbind('keydown');
		}
	});
	//testing out a generic error display...
	$("body").ajaxError(function(request, settings) {
		closeDialog();
		app.notice('something went wrong... see the firefox console for more info</p>', 'error')
		console.debug(request, settings)
	});

	/* video popup */
	$(".video .videolink").click(function(e) {
		$vidsrc = $(this).attr("href");
		if ($vidsrc.toUpperCase().match("YOUTUBE") == null) {
			//$(".popup-box.video-box #video-player").attr("href", $vidsrc)
			$(".popup-box.video-box").modal({
				overlayClose: true,
				overlayCss: { backgroundColor: "#000" },
				closeClass: "popup-close",
				onClose: function(dialog) {
					$(".popup-box.video-box .popup-box-content").html("");
					$.modal.close();
					//flowplayer("video-player").stop().unload();
					//return false;
				},
				onShow: function(dialog) {
				$(".popup-box.video-box .popup-box-content").html("<object id='flowplayer' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='427' height='348'><param name='wmode' value='transparent' /><param name='movie' value='/js/flowplayer-3.2.2.swf' /><param name='flashvars' value='config={\"clip\":\"" + $vidsrc + "\"}' /><embed type='application/x-shockwave-flash' wmode='transparent' width='427' height='348' flashvars='config={\"clip\":\"" + $vidsrc + "\"}' src='/js/flowplayer-3.2.2.swf' /></object>");
				}
			});
		} else {
			$(".popup-box.video-box #video-player").attr("href", $vidsrc)
			$(".popup-box.video-box").modal({
				overlayClose: true,
				overlayCss: { backgroundColor: "#000" },
				closeClass: "popup-close",
				onClose: function(dialog) {
					$.modal.close();
					//return false;
				},
				onShow: function(dialog) {
					var flashvars = {};
					var attributes = { id: "video-player" };
					var params = { allowScriptAccess: "always" };
					params.wmode = "transparent";
					swfobject.embedSWF($vidsrc, "video-player", "10", "10", "8", "expressInstall.swf", flashvars, params, attributes);
				}
			});
		}
		return false;
	});

	/* photo gallery */
	$('.photo .photolink').click(function() { $("#gallery-small a:first").click(); });

	var $galleryContent = "<div id='img-placeholder'></div><ul id='gallery' class='jcarousel-skin-glarge'>" + $('#gallery-small').html() + "</ul>";
	$(".popup-box.gallery-box .popup-box-content").append($galleryContent);


	if (typeof $.fn.jcarousel == "function") {	
		$('#gallery-small').jcarousel({ scroll: 3 })
	}
	$("#gallery-small a").click(function(e) {
		$(".popup-box.gallery-box").modal({
			overlayClose: true,
			overlayCss: { backgroundColor: "#000" },
			closeClass: "popup-close",
			onClose: function(dialog) {
				$(".popup-box.gallery-box .popup-box-content").html("");
				$.modal.close();
				return false;
			}
		});

		$('#gallery li').each(function(idx) {
			$(this).data('index', (++idx));
		});

		$('#gallery').jcarousel({
			scroll: 6,
			initCallback: initCallbackFunction
		})
		$("#gallery").find("li[jcarouselindex=" + $(this).parent().attr("jcarouselindex") + "]").addClass("active");
		function initCallbackFunction(carousel) {
			$('#img-placeholder').bind('image-loaded', function() {
				var idx = $('#gallery li.active').data('index') - 2;

				carousel.scroll(idx);
				return false;
			});
		};

		// load and fade-in thumbnails
		$('#gallery li img').css('opacity', 0).each(function() {
			if (this.complete || this.readyState == 'complete') { $(this).animate({ 'opacity': 1 }, 300) }
			else { $(this).load(function() { $(this).animate({ 'opacity': 1 }, 300) }); }
		});

		$('#gallery').galleria({
			// #img is the empty div which holds full size images
			insert: '#img-placeholder',

			// enable history plugin
			history: false,

			// function fired when the image is displayed
			onImage: function(image, caption, thumb) {

				// fade in the image 
				image.hide().fadeIn(500);
				// animate active thumbnail's opacity to 1, other list elements to 0.6
				thumb.parent().parent().find("li").fadeTo(200, 0.6).end().find("li.active").fadeTo(200, 1);

				$('#img-placeholder').trigger('image-loaded');
			},

			// function similar to onImage, but fired when thumbnail is displayed
			onThumb: function(thumb) {
				var $li = thumb.parent(),
					opacity = $li.is('.active') ? 1 : 0.6;

				// hover effects for list elements
				$li.hover(
					function() { $li.fadeTo(200, 1); },
					function() { $li.not('.active').fadeTo(200, opacity); }
				)
			}
		});
		e.preventDefault();
	});

});

function closeDialog() {
	app.closeDialog();
}

function onAnimate(show) {
	//$(this).fadeIn('slow').show();
	if (show) {
		$(this)
			.css('visibility', 'hidden').show()
				.css('width', $(this).innerWidth())
			.hide().css('visibility', 'visible')
		.fadeIn('fast');
	} else {
		$(this).fadeOut('fast');
	}
}

var MENU_COUNTER = 1;
function loadMenu() {
	if (this.id == 'dynamicMenu') {
		$('> ul > li', this).remove();

		var ul = $('<ul></ul>');
		var t = MENU_COUNTER + 10;
		for (; MENU_COUNTER < t; MENU_COUNTER++) {
			$('> ul', this).append('<li>Item ' + MENU_COUNTER + '</li>');
		}
	}
}
/* 
function unloadMenu() {
if (MENU_COUNTER >= 30) {
MENU_COUNTER = 1;
}
}
*/
// We're passed a UL
function onHideCheckMenu() {
	return !$(this).parent().is('.LOCKED');
}

// We're passed a LI
function onClickMenu() {
	$(this).toggleClass('LOCKED');
	return true;
}


function finishAjax(response) {
	$('#loadingMessage').html(unescape(response));
	setTimeout("displayResponse();", 1500);
}

//finishAjax
function displayResponse() {
	$('#sendToFriendForm').slideUp();
	$('#responseMessage').fadeOut();
	$('#submitItems').show();
}

(function($) {
	$.fn.tipAFriend = function(s) {

		s = jQuery.extend({
			postURL: 'tell_friend_ajax.php',
			send2friend: 'Send to Friend',
			fname: 'Friends Name:',
			fmail: 'Friends Email:',
			yname: 'Your Name:',
			ymail: 'Friends Email:',
			msg: 'Message:',
			articlename: 'error',
			articlelink: 'error',
			sendBtn: 'Send Email',
			cancelBtn: 'Cancel',
			sendingMsg: 'Sending Message...'
		}, s);

		s.postURL = s.postURL;
		s.send2friend = s.send2friend;
		s.fname = s.fname;
		s.fmail = s.fmail;
		s.yname = s.yname;
		s.ymail = s.ymail;
		s.msg = s.msg;
		s.articlename = s.articlename;
		s.articlelink = s.articlelink;
		s.sendBtn = s.sendBtn;
		s.cancelBtn = s.cancelBtn;
		s.sendingMsg = s.sendingMsg;

		show();

		function show() {
			if ($('#sendToFriendForm').length == 0) {
				renderMarkup();
			}

			$('#loadingMessage').hide();
			$('#responseMessage').hide();
			$('#sendToFriendForm').slideDown(500);

			$('#sendToFriendForm form').bind('submit', function() {
				var params = 'fname=' + $("#sendToFriendPost #friends_name").val() + '&fmail=' + $("#sendToFriendPost #friends_email").val() + '&yname=' + $("#sendToFriendPost #your_name").val() + '&ymail=' + $("#sendToFriendPost #your_email").val() + '&msg=' + $("#sendToFriendPost #message").val() + '&articlename=' + $("#sendToFriendPost #articlename").val() + '&articlelink=' + $("#sendToFriendPost #article_link").val();
				$('#loadingMessage').fadeIn('slow');
				$('#submitItems').hide();
				$.ajax({
					type: "POST",
					url: s.postURL,
					data: params,
					//data: ,
					success: function(response) {
						setTimeout("finishAjax('" + escape(response) + "')", 450);
					}
				});
				return false;
			});

			$('#cancelSend, #closeSend, #closeSuccess, #tryAgainLink').click(function() {
				$('#sendToFriendForm').slideUp();
				$('#responseMessage').fadeOut();
				return false;
			});
		}


		function bringBack() {
			$('#formContainer').slideDown();
		}

		function renderMarkup() {
			$("body").append('<div id="sendToFriendForm"><a id="closeSend" href="#"></a><div id="formContainer"><form name="sendToFriend" id="sendToFriendPost" method="post"><h3>' + s.send2friend + '</h3><span id="sendToFriendLeft"><p><label>' + s.fname + '</label><input type="text" name="friends_name" id="friends_name" /></p><p><label>' + s.fmail + '</label><input type="text" name="friends_email" id="friends_email" /></p><p><label>' + s.yname + '</label><input type="text" name="your_name" id="your_name" /></p><p><label>' + s.ymail + '</label><input type="text" name="your_email" id="your_email" /></p></span><span id="sendToFriendRight"><label>' + s.msg + '</label><textarea name="message" id="message" rows="6"></textarea><span id="submitItems"><input type="submit" name="submitSendFriend" id="submitSendFriend" value="' + s.sendBtn + '" /> | <a id="cancelSend" href="#">' + s.cancelBtn + '</a></span><span id="loadingMessage">' + s.sendingMsg + '</span></span><div style="clear: both;"></div><input type="hidden" id="article_link" value="' + s.articlelink + '" /><input type="hidden" id="articlename" value="' + s.articlename + '" /></form></div><div id="responseMessage"><span id="addResult"></span></div></div>');
		}


	}
})(jQuery);
