/**
 * General utility functions
 * @author Scott Murphy
 * 
 * Copyright (c) 2008 Pictage Inc.
 * ALL RIGHTS RESERVED
 */

var IFrame1 = new Native({

	name: 'IFrame1',

	generics: false,

	initialize: function(){
		var params = Array.link(arguments, {properties: Object.type, iframe: $defined});
		var props = params.properties || {};
		var iframe = $(params.iframe) || false;
		var onload = props.onload || $empty;
		delete props.onload;
		props.id = props.name = $pick(props.id, props.name, iframe.id, iframe.name, 'IFrame_' + $time());
		iframe = new Element(iframe || 'iframe', props);
		var onFrameLoad = function(){
			var host = $try(function(){
				return iframe.contentWindow.location.host;
			});
/*			if (host && host == window.location.host || !host){
				var win = new Window(iframe.contentWindow);
				new Document(iframe.contentWindow.document);
				$extend(win.Element.prototype, Element.Prototype);
			} 
			onload.call(iframe.contentWindow, iframe.contentWindow.document);*/
		};
		(window.frames[props.id]) ? onFrameLoad() : iframe.addListener('load', onFrameLoad);
		return iframe;
	}

});

Window.implement({
	$E: function(selector){
		return this.document.getElement(selector);
	}
});

function displayDDLicenseAgreement()  {
	DDLicenseAgreePopup = new PopUp({width:540,content:'DDLicenseAgreement', title:'Personal Use License Agreement', closeAction:function() {}});
	DDLicenseAgreePopup.show();
}

// swapFullImage and getStandardImage are used in Books Pages and Canvas Prints
function swapFullImage(imageCSS) {
	$('fullImage').set('class', imageCSS);
}
	
function getStandardImage(product) {
	$('fullImage').set('class', ( product !== 'memory' ) ? product : product+'OneImage');
}

function displayReminderPopup(contentId, titleTxt) { 
	new PopUp( {	width:540,
				content: contentId, 
				title:titleTxt, 
				closeAction:function() {}
			}).show();
}

// Blocks clicks on element while function is processed
function blockClicks( el, func )
{
	el = $(el); // ie6
	if ( el.retrieve('clicked') )
		return;
	el.store('clicked', true);
	func();
	el.store('clicked', false);
}

/**
 *	Causes the inputs form to submit if the enter key is pressed.
 *  form parameter should not normally be passed
 */
function enableEnterSubmit( input, form )
{
	input.addEvent('keypress', function(event) {
		if (event.key == 'enter') {
			if ($defined(form))
				form.submit();
			else
				input.form.submit();
		}
	}); 	
}

window.addEvent("domready", function() {
	if($defined($('ceeDivLayer')))
		displayReminderPopup('ceeDivLayer', 'Extend Event');

	if(!$defined('ceeDivLayer') && $defined(releaseReminderTitle))
		displayReminderPopup('releaseReminder', releaseReminderTitle);
	
	if ( $defined( $E('div#studioCard .studioLogo') ) && studioWebSite != '' ) {
		$E('div#studioCard .studioLogo').setStyle('cursor','pointer');
		$E('div#studioCard .studioLogo').addEvent('click', function() {
			window.open(studioWebSite);
		});
	}
	
	$$('span.lblPagination').each( function(el) {
		el.addEvent('click', function(event) {
			var scrollSize = window.getScrollSize();
			var elementLocation = el.getCoordinates();
			var elementSize = el.getSize();
			var span = $(event.target);
			
			function doJump() {
				var pageNum = jump.getElement('input[type=text]').get('value');
				if ( pageNum.trim() != '' ) {
					pageNum = pageNum.toInt();
					if (pageNum)
						window.location = baseUrl+'&page='+(pageNum - 1);
				}
			}
			
			var jump = new Element('div', {
				'class':'windowJumpToPage',
				'html':'<ul><li>Jump to:</li><li><input type="text" id="pageNumber"/></li><li><input type="button" class="button" value="Go"/></li></ul>',
				'styles': {
					'position':'absolute',
					'z-index':'1001',
					'top': (elementLocation.top + elementSize.y) + 5,
					'left': (elementLocation.left - ( elementSize.x / 2) - 3),
					'opacity': '0'
				},
				events	 : {
					'keydown' : function(event) {
						if (event.key == "enter")
							doJump();
					}
				}					
			}).inject( document.body );
			jump.setStyle('visibility','visible');
			
			jump.getElement('input[type=button]').addEvent('click', doJump );
				  
			var cover = new Element('div', {
				'class':'paginationCover',
				'styles': {
					'position':'absolute',
					'z-index':'999',
					'opacity':'0',
					'top':'0',
					'left':'0',
					'width': scrollSize.x + 'px',
					'height': scrollSize.y + 'px'
				},
				'events': {
					'click': function(event) {
						jump.dispose();	
						this.dispose();
					}
				}
			}).inject( document.body );
			cover.setStyle('visibility','visible');
			jump.fade('1'); 
			jump.getElement('input[type=text]').focus();		
		});
	});
});