
/*** Function Chains ***/

var chainDomLoaded = new CodeCompany.FunctionChain('domLoaded');
var chainWindowLoaded = new CodeCompany.FunctionChain('windowLoaded');
var chainSessionReady = new CodeCompany.FunctionChain('sessionReady');

document.observe('dom:loaded', chainDomLoaded.executeChain.bind(chainDomLoaded));
Event.observe(window, 'load', chainWindowLoaded.executeChain.bind(chainWindowLoaded));


/*** Site Class ***/

ID.Site = Class.create(ID, {
	_identity: 'Site',
	initialize: function(options) {
		this.options = Object.extend({
			logEnabled: true,
			forceFlash: undefined
		},options || {});

		chainDomLoaded.addFunction(this.onDomLoaded.bind(this),'Site object tweaks',-1);

		this.viaProperties = new Hash();

		this.testMode = ( window.location.hostname.match(/test/) !== null );

		this._log('Site object initialized');
	},
	onDomLoaded: function() {
		// Get lastclick
		var tmp = CodeCompany.Cookie.read('lastclick');
		if ((tmp!==undefined) && (tmp!='')) {
			site.lastClick = parseInt(tmp);
		}
	},
	setViaProperty: function(key,value) {
		this.viaProperties.set(key,value);
	},
	getViaProperty: function(key) {
		return this.viaProperties.get(key);
	},
	htmlEscape: function(str) {
		return str;
		/*
		return str
			.replace(/</,'&lt;')
			.replace(/>/,'&gt;')
			.replace(/\n/,'<br/>');
			*/
	},
	log: function(str,subsystem) {
		if (subsystem===undefined) { subsystem = 'General'; }
		this._log('['+subsystem+'] '+str);
	},
	hasFlash: function() {
		// Flash forced on or off?
		if (this.options.forceFlash !== undefined) {
			return this.options.forceFlash;
		}
		// Cached result?
		if (this._hasFlash !== undefined) {
			return this._hasFlash;
		}
		// Got SWFObject?
		if (typeof(swfobject)=='undefined') {
			return false;
		}

		// Detect flash
		this._hasFlash = swfobject.hasFlashPlayerVersion("9.0.18");
		if (this._hasFlash) {
			this._flashVersion = swfobject.getFlashPlayerVersion();
			this.log("Detected Flash "+this._flashVersion.major+"."+this._flashVersion.minor+" release "+this._flashVersion.release);
		} else {
			this.log("Flash not detected");
		}

		// Return result
		return this._hasFlash;
	},
	isTest: function() {
		return null !== document.location.host.match(/test/);
	},
	getURLParameter: function(paramName, defaultValue)
	{
		var matches = window.location.search.match(new RegExp('[?&]'+paramName+'(=([a-zA-Z0-9_%-]*))?'));
		if ( matches !== null )
		{
			return matches[2];
		}
		else
		{
			return defaultValue;
		}
	},
	hasURLParameter: function(paramName)
	{
		return window.location.search.search(new RegExp('[?&]'+paramName)) !== -1;
	},
	showOverlay: function(element) {
		this.log('Please use an action class element to open overlays instead of calling site.showOverlay() if possible.');
		document.fire('overlaywindow:show',{'element':element});
	},
	hideOverlay: function() {
		this.log('Please use an action class element to open overlays instead of calling site.hideOverlay() if possible.');
		document.fire('overlaywindow:show');
	}
});

var site = new ID.Site({
	logEnabled: (window.location.search.match(/[?&]nologging/)===null) && ((window.location.hostname.match(/test/)!==null) || (window.location.search.match(/[?&]logging/i)!==null)),
	forceFlash: (window.location.search.match(/[?&]noflash/)!==null) ? false : ((window.location.search.match(/[?&]forceflash/)!==null) ? true : undefined)
});


/*** Load Observer ***/

LoadObserver = Class.create({
	initialize: function(selector,callback) {
		this.callback = callback;

		this.elementsLeft = 0;
		this.elements = $$(selector);

		this.onLoadBound = this.onLoad.bindAsEventListener(this);
		this.finishedWrapped = Prototype.emptyFunction();
		this.elements.each(function(element){
			if (!element.complete) {
				this.elementsLeft++;
				element.observe('load',this.onLoadBound);
			}
		},this);
		this.finishedWrapped = this.finished;

		if (this.elementsLeft==0) { this.finishedWrapped(); }
	},
	onLoad: function(evt) {
		Event.element(evt).stopObserving('load',this.onLoadBound);
		this.elementsLeft--;
		if (this.elementsLeft==0) { this.finishedWrapped(); }
	},
	finished: function() {
		this.callback();
	}
});


/*** Image Overlays ***/

var ImageOverlays = Class.create({
	log: function(str) {
		site.log(str,'ImageOverlays');
	},
	initialize: function(options) {
		this.options = Object.extend({
			'elementSelector':		'IMG.overlayable',
			'closeLabel':			'» Luk vindue',
			'altText':				'Se stort foto'
		},options || {});

		// Got elements?
		var elements = $$(this.options.elementSelector);
		if (elements.length == 0) {
			this.log('No matching elements ('+this.options.elementSelector+')');
			return;
		}

		// Subscribe elements
		elements.each(function(imageElement){
			this.subscribeElement(imageElement);
		},this);
		site.log('ImageOverlays is observing '+elements+' elements selected by "'+this.options.elementSelector+'"');

		this.template = new Template(
			'<div class="imageoverlay_image">'+
				'<img src="#{imageSource}" />'+
			'</div>'+	
			'<div class="imageoverlay_close coloredlink">'+
				'<a href="#" class="action_overlaywindowclose">#{closeLabel}</a>'+
			'</div>'
		);

		// Observe visible-event from overlaywindow handler
		document.observe('overlaywindow:visible_imageoverlay_overlay',this._onOverlayVisible.bindAsEventListener(this));

		// Create element
		this.overlayElement = new Element('div',{'id':'imageoverlay_overlay'});
		this.overlayElement
			.addClassName('overlaywindow')
			.addClassName('imageoverlay');

		$$('body').first().insert({'bottom':this.overlayElement});
		(function(){ document.fire('custom:contentrefresh'); }).defer();

		site.log("initialized");
	},
	onClickImage: function(event,imageElement) {
		Event.stop(event);

		// Calculate image source
		var imageSource = imageElement.src;
		// Special miljomini rule
		if (imageSource.match(/miljomini\d+\./) !== null) {
			imageSource = imageSource.replace(/(miljo)mini(\d+\.)/,'$1$2');
		}
		// Apply some URL magic
		var imageFilename = imageSource.substring(imageSource.lastIndexOf("/"));
		var imageSource = "/files/ide.dk/inspiration/fryd/768x384"+imageFilename;

		// Update content
		this.overlayElement.update(this.template.evaluate({
			'imageSource':	imageSource,
			'closeLabel':	this.options.closeLabel
		}));
		this._onElementContentRendered.bind(this).defer();
	},
	_onElementContentRendered: function() {
		new LoadObserver('.imageoverlay_image img',(function(){
			site.showOverlay(this.overlayElement);

			// Check if overlaid image is an image map
			(function(){
				this.log("Checking for imagemaps..");
				new CodeCompany.ImageMapLoader({
					'elementSelector': '.imageoverlay IMG'
				});
			}).bind(this).defer();
		}).bind(this));
	},
	_onOverlayVisible: function() {

	},
	subscribeElement: function(imageElement) {
		imageElement.writeAttribute('alt', this.options.altText);

		// Add imagemap to indicate overlayability
		if (imageElement.src.match(/miljomini\d+\./) !== null) {
			var im = new CodeCompany.ImageMap(imageElement);
			var point = im.addPoint({
				'xPosition':	30,
				'yPosition':	30,
				'xOffset':		-20,
				'yOffset':		-20,
				'popupHtml':	'Klik og se detaljer om produkterne'
			});
			point.getContainerElement().observe('click',this.onClickImage.bindAsEventListener(this,imageElement));
		}

		// Observe clicks
		var observeElement = imageElement;
		imageElement.observe('click',this.onClickImage.bindAsEventListener(this,observeElement));
	}
});


/*** New Overlay Handler ***/

chainDomLoaded.addFunction(function () {
	new ID.ActionClickHandler();
},'ID.ActionClickHandler',1);

chainDomLoaded.addFunction(function () {
	new ID.OverlayHandler();
},'ID.OverlayHandler',1000);


/*** HoverObserver ***/

var HoverObserver = Class.create({
	initialize: function(options) {
		this.options = Object.extend({
			'elementSelector':		'.hoverable',
			'className':			'hover',
			'startEvent':			'mouseenter',
			'stopEvent':			'mouseleave',
			'watchClassName':		'hoverable-observed'
		},options || {});
		var elements = 0;
		$$(this.options.elementSelector).each(function(element){
			if (!element.hasClassName(this.options.watchClassName)) {
				Event.observe(element, this.options.startEvent, this.onStart.bindAsEventListener(this,element));
				Event.observe(element, this.options.stopEvent, this.onStop.bindAsEventListener(this,element));
				elements++;
				element.addClassName(this.options.watchClassName);
			}
		},this);
		site.log('HoverObserver is observing '+elements+' elements selected by "'+this.options.elementSelector+'"');
	},
	onStart: function(event,element) { element.addClassName(this.options.className); },
	onStop: function(event,element) { element.removeClassName(this.options.className); }
});

chainDomLoaded.addFunction(function(){
	// Observe mouse-enter/leave
	new HoverObserver();
},'HoverObserver');


/*** Window Resizer - Fixes background images ***/

var WindowResizer = Class.create({
	log: function(str) {
		site.log(str,'WindowResizer');
	},
	initialize: function() {
		// .maincontainer -> .maincontainerstretcher -> .maincontainerinner
		this.initialized = false;
		this.stretcherElement = $('maincontainerstretcher');
		if (null===this.stretcherElement) {
			this.log('Initialized without #maincontainerstretcher');
			return;
		}
		this.innerElement = $('maincontainerinner');
		if (null===this.innerElement) {
			this.log('Initialized without #maincontainerinner');
			return;
		}
		this.layerElements = $$('DIV.pagebackground');
		this.lastHeight = undefined;
		this.initialized = true;
		//this.resize();

		// Observe events
		Event.observe(window, 'resize',this.onResize.bindAsEventListener(this));
		document.observe("custom:contentrefresh",this.onContentRefresh.bindAsEventListener(this));
	},
	resize: function(logStr) {
		if (!this.initialized) {
			this.log('Call to resize() without proper initialization');
			return;
		}

		// Height of viewport
		var viewport = document.viewport.getDimensions();
		var viewportHeight = viewport['height'];

		// Height of inner element
		var innerHeight = this.innerElement.getHeight();

		// Set height of stretcher + layers
		var height = Math.max(viewportHeight,innerHeight);

		// Skip if height is the same as last time
		if (this.lastHeight===height) { return; }
		this.lastHeight = height;

		this.log('Resizing to height '+height+' ('+logStr+')');
		var heightStr = ''+height+'px';

		if (Prototype.Browser.IE) {
			this.stretcherElement.style.height = heightStr;
			this.layerElements.each(function(element){ element.style.height = heightStr; });
		} else {
			this.stretcherElement.style.minHeight = heightStr;
			this.layerElements.each(function(element){ element.style.minHeight = heightStr; });
		}
	},
	deferredResize: function(logStr) {
		this.resize.bind(this,logStr).defer();
	},
	onResize: function(e) {
		this.deferredResize('Triggered by Window Resize');
	},
	onContentRefresh: function(e) {
		this.deferredResize('Triggered by custom:contentrefresh');
	}
});

chainWindowLoaded.addFunction(function(){
	new WindowResizer();
},'Instantiating WindowResizer',1);

chainWindowLoaded.addFunction(function(){
	document.fire('custom:contentrefresh');
},'Sending custom:contentrefresh event',999);


/*** Image Map ***/

chainWindowLoaded.addFunction(function(){
	new CodeCompany.ImageMapLoader();
	new ImageOverlays();
},'ImageMap/Overlays');



/*** ? ***/

function fixProductLinksPosition() {
	var source = $('produktlinks');
	var destination = $('prodlinksplaceholder');
	if (source==null || destination==null) { return; }
	destination.replace(source);
}


/*** Hook on chains ***/

chainDomLoaded.addFunction(function () {
	document.fire('custom:systemfontreplace');
},'Font-replace');

chainDomLoaded.addFunction(function(){
	fixProductLinksPosition();
},'FixProductLinksPosition');

chainWindowLoaded.addFunction(function(){
	$$('.forsideartikel_bottomimage').each(function(element){
		var imagelayer = element.select('.imagelayer').first();
		if (!imagelayer) { return; }
		var linklayer = element.select('.linklayer').first();
		if (!linklayer) { return; }

		// site.log('ImageLayer: '+imagelayer.getWidth()+'x'+imagelayer.getHeight());
		// site.log('LinkLayer: '+linklayer.getWidth()+'x'+linklayer.getHeight());

		var width = imagelayer.getWidth();
		var height = imagelayer.getHeight();

		var tmp;
		if ((tmp=linklayer.getWidth())>width) { width=tmp; }
		if ((tmp=linklayer.getHeight())>height) { height=tmp; }

		site.log('Setting element '+element.toString()+' to '+width+'x'+height);

		// Set main container size
		element.style.height = height+'px';
		element.style.width = width+'px';

		// Set linklayer size
		linklayer.style.height = height+'px';
		linklayer.style.width = width+'px';

		// Set imagelayer size
		imagelayer.style.height = height+'px';
		imagelayer.style.width = width+'px';

	});
},'LinkImage Tweak');


// Tracking

//var pageTracker = new CodeCompany.PageTracker();

chainWindowLoaded.addFunction(function(){
	var sslEnabled = document.location.protocol=='https:';
	var urlprefix = document.location.protocol+'//';
	var logPrefix = 'Tracking';
	
	// Illumi logging
	var viaElementId = site.getViaProperty('ElementId');
	if (viaElementId !== undefined) {
		var loggingUrl = '/logging/page/'+viaElementId;
		LazyLoader.load(loggingUrl,function(){
			site.log('Page logged ('+viaElementId+')',logPrefix);
		});
	} else {
		site.log('Page not logged (missing ElementId)',logPrefix);
	}

	// Globase Tracking
	var globaseUrl = urlprefix+'pagetracking.globase.com/tracking.php?guid=b516476c';
	LazyLoader.load(globaseUrl,function(){
		site.log('Globase tracking loaded',logPrefix);
	});

	// Yahoo
	try {
		var opts = {
			logFunction: function(str){site.log(str,'PageTrackerYahoo');}
		};
		var value;
		if ((value=site.getViaProperty('yahooDocumentGroup'))!='') {
			opts.defaultDocumentGroup = value;
		}
		if ((value=site.getViaProperty('yahooDocumentName'))!='') {
			opts.defaultDocumentName = value;
		}
		new CodeCompany.PageTrackerYahoo('10001510305370',opts);
	} catch(e) {
		site.log('Failed to instantiate: '+e.message,'PageTrackerYahoo');
	}

	// Google
	try {
		new CodeCompany.PageTrackerGoogle('UA-12685907-1',{
			logFunction: function(str){site.log(str,'PageTrackerGoogle');}
		});
	} catch(e) {
		site.log('Failed to instantiate: '+e.message,'PageTrackerGoogle');
	}

},'Tracking',100000);


/*** Service Ready Chain ***/

var servicetoken;
chainWindowLoaded.addFunction(function(){
	var logPrefix = 'GetSession';

	new Ajax.Request('/systemservice/getservicetoken',{
		// POST request:
		// parameters: { 'jsonreq': 1 },
		
		// GET request:
		parameters: { 'docid': 1 },
		method: 'get',

		onSuccess: function() {
			var sessionSID = CodeCompany.Cookie.read('sid');
			var sessionGUID = CodeCompany.Cookie.read('guid');
			if ((sessionSID=="") || (sessionGUID=="")) {
				site.log('Error - Empty SID/GUID returned from server',logPrefix);
				return;
			}
			servicetoken = sessionSID+','+sessionGUID;
			site.log('Service token: '+servicetoken,logPrefix);
			chainSessionReady.executeChain();
		},
		onError: function() {
			site.log('Async request for SID/GUID failed',logPrefix);
		}
	});
},'GetSession',200);


/*** ImageFrames ***/

chainWindowLoaded.addFunction(function(){
	new CodeCompany.ImageFrames();
},'ImageFrames');


/*** Class for pop-up windows - kan evt. udbygges med bekræftelse af nyt vindue ***/

var popWindow = Class.create({
    initialize: function (idName,linkType,linkDest) {
        // code to execute
        site.log('popWindow('+ linkDest +','+ linkType +','+ idName +') : initialized');
        if (linkType == "_blank"){
            Event.observe(idName, 'click', function(event) {
               site.log('popWindow: clicked');
               window.open( linkDest );
            });
		} else {
			Event.observe(idName, 'click', function(event) {
				site.log('popWindow: clicked');
				window.location = linkDest;
			});
		}
	}
});


/*** DateValidation ***/

function isDate(year, month, day) {
	month = month - 1; // javascript month range 0 - 11
	var tempDate = new Date(year,month,day);
	return ((year == tempDate.getFullYear()) && (month == tempDate.getMonth()) && (day == tempDate.getDate()));
}


/*** Google analytics goal-tracking shortcut ***/

function trackEvent(goal){
	// pageTracker._trackPageview(goal);
	document.fire('custom:googletrack', { trackUrl: goal });
	site.log('tracking goal: '+goal);
}

/*** Delivery dialog ***/
chainDomLoaded.addFunction(function() {
	new ID.DeliveryDialog();
}, 'ID.DeliveryDialog');

