if( window.location.href.indexOf( "localhost" ) > -1  ) {
	var REMOTE_BASE = "http://localhost/_clients/sourcedmarket/deploy/www/v01_1/";
}else{
	var REMOTE_BASE = "http://www.sourcedmarket.com/";
}

var M_HOME = "";
var M_GALLERY = "gallery";
var sourced = null;
backgroundFixed = false;

var ApplicationFacade = 
{
	domreadyHandler: function()
	{
		sourced = new Main(); 
		switch( getCurrentSite() ) 
		{
			case M_HOME : 
				break;
			case M_GALLERY :
				new SourcedGallery( $("galleryContainer2007"), galleryData2007 );
				new SourcedGallery( $("galleryContainer2008"), galleryData2008 );
				new SourcedGallery( $("galleryContainer2009"), galleryData2009 );
				break;
			default:
				break;
		}
	},
	loadHandler: function()
	{
		
	}
};


/**
 * TODO: SWFIFF 2 check
 */ 
var Main = new Class
({
	backgroundFixed: false,
	
	
	initialize: function()
	{
		window.addEvent( "resize", this.resize.bind( this ) );
		
		 this.backgroundFixed = backgroundFixed;
		
		var bgList = [
		    'bg_01.jpg',
		    'bg_02.jpg',
		    'bg_03.jpg',
		    'bg_04.jpg'
		];
		
		
		var bgIndex = Cookie.read( "bg" );
		var bgIndex;
		if( !$chk( bgIndex ) ) {
			bgIndex = parseInt( Math.random() * (bgList.length) );
			Cookie.write( "bg", bgIndex, {path:"/", duration:0 } );
		}
		bgIndex = parseInt( bgIndex );
		var bgUrl = bgList[ bgIndex ];

		
		
		if( SwiffExt.getMajor() > 8 ) {
			var params = {
					swLiveConnect: false,
					allowScriptAccess: "sameDomain",
					wMode: "transparent",
					scale : "noScale",
					salign : "lt",
					bgcolor:"#000000"
			};
			var fVars = {
				bgColor:"#549D92",
				//bgTargetSmall: REMOTE_BASE + "gfx/sitebg.small.jpg",
				bgTarget: REMOTE_BASE + "gfx/backgrounds/" + bgUrl,
				offsetX:0,
				offsetY:0,
				fade:true
			};
			var so = new Swiff
			(
				"swf/bg.swf",
				{
					id: "swfElement",
					width: "100%",
					height: "100%",
					params: params,
					vars: fVars,
					container: $("swfContainer"),
					callBacks: {
						
					}
				}
			);
		}else{
			document.body.setStyle( "background-image", "url( gfx/sitebg.jpg )");
		}
		
		
		if( this.backgroundFixed  ) {
			$("swfContainer").setStyles( { position: "fixed", width:"100%" } ); 
		}
		
		
		this.resize();
		this.handleNl();
	},
	resize: function()
	{
		if( this.backgroundFixed  ) {
			var s = window.getSize();
		}else{
			var s = document.getScrollSize();
		}
		
		
		$("swfContainer").setStyle( "height", s.y  );
	},
	handleNl: function()
	{
		if( $chk( $("iSignupMail") ) ) {
			$("iSignupMail").getParent("form").addEvent( "submit", this.nlSubmitHandler.bind( this ) );
			$("iSignupMail").addEvent("focus", this.nlFocusHandler );
			$("iSignupMail").addEvent("blur", this.nlBlurHandler );
			if( getRequestParameter( "sent" ) ) {
				$$(".nugget.signup").addClass( "sent" );
			}
		}
	},
	nlSubmitHandler: function( e )
	{
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		var m = $("iSignupMail").get("value");
		if( !emailPattern.test( m ) ) {
			new Event( e ).stop();
			alert("Please enter a valid email address");
			return;
		}
	},
	nlFocusHandler: function()
	{
		var m = $("iSignupMail").get("value");
		if( m == "email address") {
			$("iSignupMail").set("value", "" );
		}
	},
	nlBlurHandler: function()
	{
		var m = $("iSignupMail").get("value");
		if( !$chk( m ) ) {
			$("iSignupMail").set("value", "email address" );
		}else{
			//$("iSignupMail").getParent("form").submit();
		}
	}
});




/**
 * TODO: methods for positioning:
 * fill: scale, dont pay attention to dimension ratio,
 * cropfill: scale to the smallest edge, position opposite to the top, mask the opposite
 * cropfillCenter: see cropfill, but position the opposite centered
 * cropFillBottom: see cropfill, but position the opposite bottom
 * tile: inject css bg to idShell element
 * 
 */
var FullsizeDhtmlBackground = new Class
({
	idShell: "fullscreenBgShell",
	id: "fullscreenBg",
	initialized: false,
	contentElement:null,
	
	initialize: function( bgUrl, contentWrapper )
	{
		this.contentElement = $(contentWrapper);
		window.addEvent( "resize", this.update.bind(this) );
		document.body.style.padding = '0';
		document.body.style.margin = '0';
		var overlay = new Element( "div" ).setStyles( { position: "absolute", top: "0", left: "0", height:"100%", width:"100%" } ).set( "html", document.body.get("html") );
		var bg = new Element( "div", {id:this.idShell} ).setStyles( {overflow: "hidden"} ).adopt( new Element("img", {id:this.id, width:10, height:10, src:bgUrl } ).setStyles( {left:0, top:0, "z-index" : 0 } ) );
		document.body.set("html", "").adopt( overlay );
		document.body.adopt( bg );
		this.initialized = true;
		this.update();
	},
	update: function()
	{
		var cSize = $("content").getSize();
		var bSize = window.getScrollSize();
		var sWidth = bSize.x;
		var sHeight = bSize.y;
		$( this.idShell ).setStyles( {width:sWidth, height:sHeight} );
		var ratio = 1024 / 768;
		if (sWidth / ratio < sHeight) {
			sWidth = sHeight * ratio;
		}else{
			sHeight = sWidth / ratio;
		}
		$( this.id ).setStyles( { top:0, left:0, width: sWidth, height: sHeight } );
	}
});




var SwiffExt = new Class
({
	Extends: Swiff,
	initialize: function()
	{
		this.contentStack = new Object();
		this.parent();
	},
	setVersionContent: function( versionMajor, versionSwf )
	{
		var versionMinor = 0;
		var versionRev = 0;
		if( typeof versionMajor == 'object' ) {
			var v = versionMajor;
			versionMajor = v[0];
			versionMinor = v[1];
			versionRev = v[2];
		}
		this.contentStack['V'+versionMajor] = versionSwf;
		this.contentStack['r'+versionMajor] = versionRev;
		this.contentStack['m'+versionMajor] = versionMinor;
	},
	getOptimalContent: function()
	{
		var v = this.getVersionDetails();
		var thisMajor = v[0];
		var thisRev = v[2];
	
		var minDiff = 1000;
		for(var i in this.contentStack) {
			var n =  Number( i.substr(1, 3) );
			var tmpDiff = thisMajor - n;
			if(tmpDiff < minDiff && tmpDiff > -1)  {
				if( tmpDiff === 0 ) {
					if( this.contentStack['r'+thisMajor] <= thisRev ) {
						minDiff = tmpDiff;
					}
				}else{
					minDiff = tmpDiff;
				}
			}
		}
		if( tmpDiff <= 0 ) return { fname: this.contentStack['V'+n], version:n };
		return ( thisMajor - minDiff <= thisMajor ) ? { fname: this.contentStack['V'+(thisMajor - minDiff)].toString(), version: thisMajor} : null;
	},
	getVersionDetails: function()
	{
		return SwiffExt.getVersionDetails();
	},
	getMajor: function()
	{
		return SwiffExt.getMajor();
	}
});
SwiffExt.getVersionDetails = function()
{
	var version;
	if (navigator.plugins && navigator.mimeTypes.length) {
		version = navigator.plugins["Shockwave Flash"];
		if (version && version.description) 
			version = version.description;
		version = version.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split(".");
	}
	else {
		try {
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		} 
		catch (e) {
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				version = [6, 0, 21];
				axo.AllowScriptAccess = "always";
			} 
			catch (e) {
				if (version[0] == 6) {
					return version;
				}
			}
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			} 
			catch (e) {
			}
		}
		if (axo != null) {
			version = axo.GetVariable("$version").split(" ")[1].split(",");
		}
	}
	return version;
};
SwiffExt.getMajor = function()
{
	return SwiffExt.getVersionDetails()[ 0 ];
};
function getRequestParameter(param) 
{
	var q = document.location.search || document.location.hash;
	if(q) {
		var pairs = q.substring(1).split("&");
		for (var i=0; i < pairs.length; i++) {
			if (pairs[i].substring(0, pairs[i].indexOf("=")) == param) {
				return pairs[i].substring((pairs[i].indexOf("=")+1));
			}
		}
	}
	return "";
}
function getRequestParametersArray()
{
	var q = document.location.search || document.location.hash;
	var res = {};
	if(q) {
		var pairs = q.substring(1).split("&");
		for (var i=0; i < pairs.length; i++) {
			res[ pairs[i].substring(0, pairs[i].indexOf("=")) ] = pairs[i].substring((pairs[i].indexOf("=")+1));
		}
	}
	return res;
}
function getCurrentSite()
{
	var loc = window.location;
	loc = loc.toString().replace( REMOTE_BASE, "" );
	loc = loc.toString().replace( /\.html?((\?|#)[^\/]*)?$/, "" );
	if( !$chk( loc ) ) loc = "index";
	else loc = loc.match( /[^\/]*$/ );
	return $type( loc ) == "array" ? loc[0] : loc;
}




