﻿/**
 * Cobalt Elements for jQuery
 * popup - This pops up an iframe remote url.
 *
 * These methods are build on the jQuery and interface foundation to provider
 * client functionality to the Cobalt CMS system.
 *
 * Copyright (c) 2008 Scorpion Design, Inc.
 *
 */
(function($) {

	// Create the cobalt namespace if it has not already been done.
	$.cobalt = $.cobalt || {};
	$.cobalt.popup = {};

	// Initialize these static properties/methods.
	$.extend($.cobalt.popup, {
		title : '',
		contentHtml : '<iframe class="ipopup" src="Shared/blank.html" frameborder="0"></iframe>',
		contentHtml2 : '<iframe class="ipopup" src="Shared/blank.html" frameborder="0" scrolling="no"></iframe>',
		width : 640,
		height : 480,

		list : [],

		// Activate the popup.
		start : function(el,o)
			{
				// Build the popup and add to the properties.
				o = o||{};
				var popup;
				if (!el || !(popup=el.$popup))
				{
					popup = new $.cobalt.dialog();
					$.cobalt.popup.list.push(popup);

					popup.element = el?(el.jquery?el:$(el)):null;
					$.extend(popup,$.cobalt.popup);
					
					// Hide scrolling if so specified.
					if (o.noscrolling)
						popup.contentHtml = popup.contentHtml2;

					// Assign the onclose event.
					if (o.onclose)
					{
						popup.oncloseevent = o.onclose;
						o.onclose = null;
						try { delete o['onclose']; } catch(ex) {;}
					}
					
					// Obtain the basic popup values.
					o.title = o.title||popup.element.attr('_title')||'';
					o.width = o.width||$.toInt(popup.element.attr('_width'))||popup.width;
					o.height = o.height||$.toInt(popup.element.attr('_height'))||popup.height;
					
					$.extend(popup,o);
					if (o.submit)
						popup.onselect = popup.submitForm;

					// If we have an element, record this popup and that it is active.
					if (popup.element && popup.element.length)
						popup.element[0].$popup = popup;
				}

				// Active the popup.
				o.url = o.url||popup.element.filter('a').attr('href')||'';
				popup.open(o);
				popup.active = true;
			},

		// Overload this function to initialize other elements of the dialog box.
		startDialog : function(o)
			{
				this.frame.attr('src',o.url||'Shared/blank.html');
			},

		// Submit the form when clicking on the "save" button.
		submitForm : function()
			{
				// Set a loading screen as appropriate.
				if (this.options.submitLoading)
					this.main.loading();

				// Get the child frame document.
				var doc = this.frame.contents();
				
				try
				{
					// If jquery is defined in that child frame, use it to fire the submit button.
					var wn = doc[0].parentWindow;
					if (wn && wn.$)
					{
						var form = wn.$(doc[0]).find('form');
						if (form.length) form[0].submit();
						return false;
					}
				}
				catch(ex){;}

				// Otherwise, fire the submit button from here.
				var form = doc.find('form');
				if (form.length) form[0].submit();
				return false;
			},

		// Blank out the remote url.
		onclose : function(popup)
			{
				if ($.isFunction(popup.oncloseevent)) popup.oncloseevent(popup);
				popup.frame.attr('src','Shared/blank.html');
				popup.active = false;
			},

		// This static method, called from a child iframe'd popup page, will close the parent popup.
		closeThis : function(document)
			{
				for (var i=0;i<$.cobalt.popup.list.length;i++)
				{
					var popup = $.cobalt.popup.list[i];
					var doc = popup.frame.contents();
					if (doc && doc.length && doc[0]==document)
					{
						popup.close();
						break;
					}
				}
			},

		// Initialize the image browser for the first time.
		initContents : function(o)
			{
				if (!this.main) return;
				o = this.options||{};

				// If we're not going to submit a form, we don't need to see the save/cancel buttons at the bottom.
				if (!o.submit)
				{
					this.savebtn.hide();
					this.cancelbtn.hide();
				}

				// Assign the frame properties.
				this.frame = this.main.find('iframe');
				this.frame.css({width:o.width||this.width,height:o.height||this.height});
			}
	});

	// On click, popup a remote url iframe.
	$.fn.popup = function(o) {

		var fn = function(e) { $.cobalt.popup.start(this,o); return false; };
		return this.click(fn);

	};

})(jQuery);


/*!
	reflection.js for jQuery v1.03
	(c) 2006-2009 Christophe Beyls <http://www.digitalia.be>
	MIT-style license.
*/

(function($) {

$.fn.extend({
	reflect: function(options) {
		options = $.extend({
			height: 1/4,
			opacity: 0.45
		}, options);

		return this.unreflect().each(function() {
			var img = this;
			if (/^img$/i.test(img.tagName)) {
				function doReflect() {
					var imageWidth = img.width, imageHeight = img.height, reflection, reflectionHeight, wrapper, context, gradient;
					var opt = ($(img).attr('_reflect')||'').split(',');
					var h = (opt.length>0 && opt[0] && $.toFloat(opt[0])) || options.height;
					var o = (opt.length>1 && opt[1] && $.toFloat(opt[1])) || options.opacity;
					
					reflectionHeight = Math.floor((h > 1) ? Math.min(imageHeight, h) : imageHeight * h);

					if ($.browser.msie) {
						reflection = $("<img />").attr("src", img.src).css({
							width: imageWidth,
							height: imageHeight,
							marginBottom: reflectionHeight - imageHeight,
							filter: "flipv progid:DXImageTransform.Microsoft.Alpha(opacity=" + (o * 100) + ", style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy=" + (reflectionHeight / imageHeight * 100) + ")"
						})[0];
					} else {
						reflection = $("<canvas />")[0];
						if (!reflection.getContext) return;
						context = reflection.getContext("2d");
						try {
							$(reflection).attr({width: imageWidth, height: reflectionHeight});
							context.save();
							context.translate(0, imageHeight-1);
							context.scale(1, -1);
							context.drawImage(img, 0, 0, imageWidth, imageHeight);
							context.restore();
							context.globalCompositeOperation = "destination-out";

							gradient = context.createLinearGradient(0, 0, 0, reflectionHeight);
							gradient.addColorStop(0, "rgba(255, 255, 255, " + (1 - o) + ")");
							gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");
							context.fillStyle = gradient;
							context.rect(0, 0, imageWidth, reflectionHeight);
							context.fill();
						} catch(e) {
							return;
						}
					}
					$(reflection).css({display: "block", border: 0});

					wrapper = $(/^a$/i.test(img.parentNode.tagName) ? "<span />" : "<div />").insertAfter(img).append([img, reflection])[0];
					wrapper.className = img.className;
					$.data(img, "reflected", wrapper.style.cssText = img.style.cssText);
					$(wrapper).css({width: imageWidth, height: imageHeight + reflectionHeight, overflow: "hidden"});
					img.style.cssText = "display: block; border: 0px";
					img.className = "reflected";
				}

				if (img.complete) doReflect();
				else $(img).load(doReflect);
			}
		});
	},

	unreflect: function() {
		return this.unbind("load").each(function() {
			var img = this, reflected = $.data(this, "reflected"), wrapper;

			if (reflected !== undefined) {
				wrapper = img.parentNode;
				img.className = wrapper.className;
				img.style.cssText = reflected;
				$.removeData(img, "reflected");
				wrapper.parentNode.replaceChild(img, wrapper);
			}
		});
	}
});

})(jQuery);


/**
 * Cobalt Elements for jQuery
 * lightbox - This sets up a number of images as a "lightbox" popup.
 *
 * These methods are build on the jQuery and interface foundation to provider
 * client functionality to the Cobalt CMS system.
 *
 * Copyright (c) 2008 Scorpion Design, Inc.
 *
 */
(function($) {

	// Create the cobalt namespace if it has not already been done.
	$.cobalt = $.cobalt || {};
	$.cobalt.lightbox = function(el){

		if (!el) return;
		this.element = $(el);
		this.init();

	};

	// Initialize these prototype methods.
	$.extend($.cobalt.lightbox.prototype, {

		active : false,
		image : 1,
		width : 0,
		height : 0,
		items : [],
		root : '/',
		r_img : /\.(?:gif|bmp|png|jpg|jpe|jpeg|tif|tiff)$/i,

		// Show the lightbox and its image.
		showLightbox : function(e)
			{
				var link = $(this);
				var l = link.data('lightbox');
				var item = link.data('item');
				l.active = true;

				// Position the main display div.
				var wn = $(window).absPosition();
				var left = parseInt((wn.width-l.owidth)/2);
				var top = parseInt((wn.height-l.oheight)/2);
				var css = {
					position:'fixed',
					left:left,
					top:top
				};
				if ($.browser.msie6)
				{
					css.position = 'absolute';
					css.top += wn.top;
					css.left += wn.left;
				}
				l.display.css(css);

				// Set the images based on the current position.
				l.index = item.index;
				l.image = 1;
				var href = l.items[l.index].href;
				l.ithis1.css({backgroundImage:'url('+l.getUrl(href)+')',opacity:0,display:'block'});
				l.ithis2.css({opacity:0});
				l.caption.children('div').html(l.items[l.index].title||'');

				// Fade in the overlay, then the display div.
				var fn = function(l){
					return function(){
						l.display.css({display:'block'});
						l.ithis1.stop().animate({opacity:1},400,'linear');
						l=null;
					};
				}(l);
				$.pageOverlay().stop();
				$.overlayIn(400,.6,fn);

				// Cancel clicking on the link.
				return false;
			},

		// Fade the button in.
		showButton : function(e)
			{
				$(this).children().stop().animate({opacity:1},400,'swing');
			},

		// Fade the button out.
		hideButton : function(e)
			{
				$(this).children().stop().animate({opacity:0},400,'swing');
			},

		// Move to the next image.
		nextImage : function(e)
			{
				var l = this.nextImage ? this : $(this).data('lightbox');
				var img1 = l['ithis'+l.image];
				l.image = l.image==1?2:1;
				var img2 = l['ithis'+l.image];
				l.index++;
				if (l.index>=l.items.length) l.index=0;
				var href = l.items[l.index].href;
				img2.css({backgroundImage:'url('+l.getUrl(href)+')',opacity:0,display:'block'});
				img1.stop().animate({opacity:0},300,'linear');
				img2.stop().animate({opacity:1},300,'linear');
				l.caption.children('div').html(l.items[l.index].title||'');
			},

		// Move to the previous image.
		prevImage : function(e)
			{
				var l = this.nextImage ? this : $(this).data('lightbox');
				var img1 = l['ithis'+l.image];
				l.image = l.image==1?2:1;
				var img2 = l['ithis'+l.image];
				l.index--;
				if (l.index<0) l.index = l.items.length-1;
				var href = l.items[l.index].href;
				img2.css({backgroundImage:'url('+l.getUrl(href)+')',opacity:0,display:'block'});
				img1.stop().animate({opacity:0},300,'linear');
				img2.stop().animate({opacity:1},300,'linear');
				l.caption.children('div').html(l.items[l.index].title||'');
			},

		// Return a thumbnail url.
		getUrl : function(href)
			{
				return 'images/thumbnail.aspx?I='+$.encode(this.root+href)+'&W='+this.width+'&H='+this.height;
			},

		// Close the lightbox.
		closeLightbox : function()
			{
				var l = this.closeLightbox ? this : $(this).data('lightbox');
				if (!l.active)
					return;
				else
					l.active = false;

				// Fade out the lightbox followed by the overlay.
				var fn = function(l){
					return function(){
						l['ithis'+l.image].css({display:'none'});
						l.display.css({display:'none'});
						l = null;
						$.pageOverlay().stop();
						$.overlayOut();
					};
				}(l);
				$.pageOverlay().stop();
				l.ithis1.stop().animate({opacity:0},400,'linear',fn);
			},

		// Initialize the lightbox links.
		init : function()
			{
				// Get all of the elements.
				var l = this;
				
				// Get the site root.
				l.root = $(document.body).attr('_root')||'/';

				// Get the width and height of the lightbox.
				l.width = $.toInt(l.element.attr('_lightwidth'))||600;
				l.height = $.toInt(l.element.attr('_lightheight'))||600;

				// Get each of the elements.
				l.items = [];
				l.element.find('a').each(function(i){
					var link = $(this);
					var href = link.attr('href');
					if (l.r_img.test(href))
					{
						var item = {
							index:l.items.length,
							href:href,
							title:link.attr('title')
						};
						link.data('lightbox',l).data('item',item).click(l.showLightbox);
						l.items.push(item);
						$.preload(l.getUrl(href));
					}
				});
				
				// If we don't have any items to show, stop here.
				if (!l.items.length) return;

				// Create the display box.
				l.display = $('\
<div class="ilight_box">\
	<div class="ilight_image ithis1"></div>\
	<div class="ilight_image ithis2"></div>\
	<div class="ilight_close"><span>X</span></div>\
	<div class="ilight_gonext"><div>NEXT &gt;</div></div>\
	<div class="ilight_goprev"><div>&lt; PREV</div></div>\
	<div class="ilight_caption"><div></div></div>\
</div>')
					.css({display:'none',position:'absolute',zIndex:100})
					.appendTo(document.body);

				// Get the previous, this and next images.
				var css = {
					position:'absolute',
					opacity:0,
					width:l.width,
					height:l.height,
					left:0,
					top:0,
					backgroundRepeat:'no-repeat',
					backgroundPosition:'center center'
				};
				l.ithis1 = l.display.children('.ithis1').css(css);
				l.ithis2 = l.display.children('.ithis2').css(css);

				// Get the caption and set its width.
				l.caption = l.display.children('.ilight_caption').css({width:l.width});

				// Record the outer width.
				l.owidth = l.width
					+$.toInt(l.ithis1.css('borderLeftWidth'))
					+$.toInt(l.ithis1.css('borderRightWidth'))
					+$.toInt(l.ithis1.css('marginLeft'))
					+$.toInt(l.ithis1.css('marginRight'));

				// And outer height (adding the size of the caption box as well)
				l.oheight = l.height
					+$.toInt(l.ithis1.css('borderTopWidth'))
					+$.toInt(l.ithis1.css('borderBottomWidth'))
					+$.toInt(l.ithis1.css('marginTop'))
					+$.toInt(l.ithis1.css('marginBottom'))
					+$.toInt((l.caption.css('height')||35))
					+$.toInt(l.caption.css('borderTopWidth'))
					+$.toInt(l.caption.css('borderBottomWidth'))
					+$.toInt(l.caption.css('marginTop'))
					+$.toInt(l.caption.css('marginBottom'));

				// Set the outer width.
				l.display.css({width:l.owidth,height:l.oheight});

				// Set up the page buttons.
				l.gonext = l.display.children('.ilight_gonext').data('lightbox',l).hover(l.showButton,l.hideButton).click(l.nextImage);
				l.goprev = l.display.children('.ilight_goprev').data('lightbox',l).hover(l.showButton,l.hideButton).click(l.prevImage);
				l.gonext.children().css({opacity:0});
				l.goprev.children().css({opacity:0});

				// Set the page overlay as click-to-close;
				var fn = function(l){
					return function(e){
						l.closeLightbox();
					};
				}(l);
				$.pageOverlay().click(fn);
				l.display.children('.ilight_close').click(fn);
			}
	});

	// Set up lightbox images.
	$.fn.lightbox = function() {

		return this.each(function(i){
		
			if (!this._lightbox)
				this._lightbox = new $.cobalt.lightbox(this);
		
		});

	};

})(jQuery);
