/**
 * @author nd
 *
 * major initiation script. Here start's the magic
 */

// Logging Function
var jsLog = {
	
	// Get console object
	conObj: null,
	
	init: function() {
		
		if (window.gecko && typeof(console) == 'object') {
		// FF Logging
			console.info("FireFox found with FireBug onBoard .. using Native Console");
			console.debug("Logging was set @ >>> ");
		} else if (!this.conObj) {
			// IE + XXX Logging
			this.conObj = new Element('div', {
				'id': "jsLOG",
				'styles': {
					'background': '#FFffFF',
					'position':   'absolute',
					'overflow':   'auto',
					'z-index':    '10000',
					'top':        '75px',
					'left':       '325px',
					'width':      '600px',
					'height':     '100px',
					'opacity':    '0.8',
					'border':     '3px red solid',
					'padding':    '10px',
					'cursor':     'move'
				}
			}).injectInside($E('body'));
			var DradDrop = new Drag.Move(this.conObj);
		} else {
			this.output("DEV Error >> Console was allready loaded!");
		}
	},
	
	output: function(content) {
		if (window.gecko && typeof(console) == 'object') {
		// FF Logging
			console.debug(content);
		} else {
		// IE + XXX Logging
			// Get old content
			var oldContent = this.conObj.innerHTML;
			// Set old + NEW content
			var timeHor = new Date().getHours();
			var timeMin = new Date().getMinutes();
			var timeSec = new Date().getSeconds();
			var curTime = timeHor + ":" + timeMin + ":" + timeSec;
			this.conObj.innerHTML = oldContent + '<p class="jsLog" style="font-size:9px;padding:0;margine:0;">' + curTime + " // " + content + '</p>';
		}
	},
	
	clear: function() {
		// Get console object
		if (window.gecko && typeof(console) == 'object') {
			// Do nothing in FF > FB
		} else {
		// IE + XXX Logging
			// Get console object
			this.conObj.innerHTML = "";
		}
	}
};

// Main Class Function
var Main = new Class({

	options: {

	},

	initialize: function(options){
		
		// Initialize Main Options
		this.setOptions(options);

		// Initialize Menu
		this.theMenu = new AlexMenu();

		// Initialize Content - mainly enabling AJAX-Content-Reload.
		this.theContent = new Content();

		// Initialize clickManager
		this.theClickManager = new ClickManager();

		// Initialize JS-Scroller
		this.theScroller = new ExtScroller();

		// Initialize Google Analytics controller
		this.theUtmMan = new UtmMan();

		// Initialize Browser BackBTN for AJAX
		this.theBackBTN = new BackBTN();

		// Initialize sIFR fonts
		//this.theSiFR = new sIFRman();

		// Initialize Floor + ClickManager
		this.theFloor = new Floor( this, {
			onStart: function(element){
				theMain.theClickManager.activate();
			},
			onComplete: function(element){
				theMain.theClickManager.deactivate();
				theMain.theScroller.update();
			}
		});
		
		
	},

	preLoader: function() {

		var imgSet = [];
		var elBG;

		// Get all BG images
		$$('div', 'span', 'a', 'ul', 'li', 'table', 'td', 'tr', 'input').each(function(el){
			elBG = el.getStyle('background-image');
			if(elBG != "none") {
				var cleanBGurl;
				cleanBGurl = elBG.match(/http:\/\/.+[a-zA-Z]/);
				if (cleanBGurl) {
					imgSet.include(cleanBGurl[0]);
				}
			}
		});
		// Get all images
		$$('img').each(function(el){
			imgSet.include(el.getProperty('src'));
		});

		// PreLoad image set
		var preIMG = new Asset.images(imgSet, {
		    onComplete: function(){
		        theMain.firstStart();
		    }
		});

		// Add IE styles
		if (window.ie) {
			var newCSSfile = new Asset.css('fileadmin/templates/alexgastro/css/patches/ie_patches.css', {
				id: 'myStyle',
				title: 'myStyle'
			});
		}
	},

	firstStart: function() {
		// apply JS "topping"
		theMain.theFloor.getTimendEmotions();
		this.theContent.checkFurnishing(location.href);
		
		var baseUrl = hashDealer.getBaseUrl();
		var Suche = baseUrl.search(/bar-cologne.de+/);
		if(Suche == -1) this.theContent.setTitle();

		Reflection.addFromClass();
		this.fixPNG();


		// Initialize Content - Videoplayer
		swfObjectLoader.init();
		$$('body')[0].setStyle('visibility','visible');

		// Replace usual Text with sIFR
		sIFRman.replace();

		// Move Floor down if needed
		this.theFloor.controlAutoHide();

		// IE 6 set the height of the content_wrapper 
		if (window.ie6){
			if (this.theFloor.floorState){
				// set correct size of the content_wrapper div
				// current content_wrapper height - Scale height from the floor Class
				$('content_wrapper').setStyle('height', $('content_wrapper').getSize().size.y - 100);
			}
		}		

		// View Site
		this.siteIntro(par.preloader.siteID);

		// Load/Start sounds for the alexgastro-site
		var baseUrl = hashDealer.getBaseUrl();
		var Suche = baseUrl.search(/bar-cologne.de+/);
		if(Suche == -1) soundObj.init();
	},

	siteIntro: function(objID) {
        var introEFX = new Fx.Styles(objID, {
        	duration: 1000,
        	transition: Fx.Transitions.Circ.easeIn
        });
        introEFX.start({
		    'opacity': [1, 0]
		}).chain( function() {
			// If the Preloader is 100% opacit remove the div
			$(objID).setStyle('display','none');
		});
	},

	fixPNG: function(){
		if (window.ie6){
			var baseURL	= hashDealer.getBaseUrl();

			// 
			var ifLocatioFinder = $$('#location-finder').length > 0 ? true:false;
			
			// Get all BG images
			if (!ifLocatioFinder) {
				$$('div', 'span', 'a', 'ul', 'li', 'table', 'td', 'tr', 'input', 'label').each(function(el){
					if (el.getStyle('background-image').test(".png")) {
						var bgURL = el.getStyle('background-image');
						if(bgURL != 'none') {
							var bgMatch1 = bgURL.match(/http:\/\/.+[a-zA-Z]/);
							var bgMatch2 = bgURL.match(/url\((.+)\)/);
							if ( bgMatch1 ) {
								bgURL = bgMatch1[0];
							} else if ( bgMatch2 ) {
								bgURL = bgMatch2[1];
							}
						}
						el.setStyles({
							background: 'none',
							filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='scale', src='" + bgURL + "')"
						});
					}
				});
			}


			$$('img', 'input').each(function(el){
							
				if ( (el.getTag() == "img" || el.getTag() == "input") && el.getProperty('src').test(".png") && !el.hasClass('reflect')) {
					var bgURL  = el.getProperty('src');
					var height = el.getProperty('height');
					var width  = el.getProperty('width');
					
					if( bgURL && bgURL !== '' && bgURL !== null && bgURL !== 'none') {
						var bgMatch3 = bgURL.match(/http:\/\/.+[a-zA-Z]/);
						if ( bgMatch3 ) {
							bgURL = bgMatch3[0];
						} else if (bgURL.substr(0,1) != '/') {
							bgURL = '/' + bgURL;
						}
					}
					el.setStyles({
						background: 'none',
						filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=0)"
					});
					el.setProperty('src',    '/clear.gif');
					el.setProperty('width',  width);
					el.setProperty('height', height);

					// Insert new span
					tempDiv = new Element('span', {
					    'styles': {
							filter:  'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled="true", sizingMethod="crop", src="'+ bgURL + '")',
							display: "inline-block"
						}
					});
					tempDiv.setProperty('width',  width);
					tempDiv.setProperty('height', height);

					// Images with full width must be scaled
					if (el.getStyle('width') == "100%") {
						tempDiv.setStyles({ filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled="true", sizingMethod="scale", src="'+ bgURL + '")'});
					}

					if (el.getTag() == "input") {
						el.setStyles({ filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled="true", sizingMethod="scale", src="'+ bgURL + '")'});
					} else {
						tempDiv.injectAfter(el).adopt(el);
					}

				}

			}.bind(this));
		}
	},

	globalResizeEvent: function() {
		this.theScroller.update();
	}

});

Main.implement(new Events());
Main.implement(new Options());

// If all DOM Elements are loaded start the Main Class.
	window.addEvent('domready', function() {
		previewFix.checkURL();
		theMain = new Main();
		theMain.preLoader();
	});

	window.addEvent('resize', function() {
		theMain = new Main();
		theMain.globalResizeEvent();
	});
