
/**********************************************
 * @author sjorek
 * @copyright www.artplan21.de
 * @author    stephan Jorek <jorek@artplan21.de>
 **********************************************/

/**********************************************
 * Original Implementation:
 * 
 * PageEar advertising CornerAd by Webpicasso Media
 * Leave copyright notice.  
 *
 * Lizenzvereinbarung / License agreement
 * http://www.webpicasso.de/blog/lizenzvereinbarungen-license-agreements/
 *
 * @copyright www.webpicasso.de
 * @author    christian harz <pagepeel-at-webpicasso.de>
 **********************************************/

(function($){

	application.factory.Hexear = function(appId,appOpts){

		var app = function(id,opts){
			if (typeof(opts)=='object') {
				this.options = $.extend(1,this.options,opts);
			}
			if (swfobject.hasFlashPlayerVersion(String(this.options.requiredMajorVersion))) {
				this.id=application.control(id,this);
				// Check direction 
				if(this.options.setDirection == 'lt') {
					this.options.xPosBig = 'left:-1000px';
					this.options.xPos = 'left';
					var className = 'tx-hexear-left';
				} else {
					this.options.xPosBig = 'right:1000px';
					this.options.xPos = 'right';
					var className = 'tx-hexear-right';
				}
				this.render(id,className);
			}
		}
		app.prototype.id = null;
		app.prototype.options={
			carrier:'body',
			smallId: 'tx-hexear-small',
			bigId: 'tx-hexear-big',
			// URL zum kleinen Bild / URL to small image 
			smallImg: 'pageear_s.jpg', 
			// URL zu pageear_s.swf / URL to small pageear swf
			smallSwf: 'pageear_s.swf', 
			// URL zum großen Bild / URL to big image
			bigImg: 'pageear_b.jpg', 
			// URL zu pageear_b.swf / URL to big pageear swf
			bigSwf: 'pageear_b.swf', 
			// Wackelgeschwindigkeit der Ecke 1-4 (2=Standard) 
			// Movement speed of small pageear 1-4 (2=Standard)
			speedSmall: 1, 
			// Bild spiegelt sich in der aufgeschlagenen Ecke ( true | false )
			// Mirror image ( true | false )
			mirror: 'true', 
			// Farbe der aufgeschlagenen Ecke wenn mirror false ist
			// Color of pagecorner if mirror is false
			color: 'ffffff',  
			// Zu öffnende URL bei klick auf die geöffnete Ecke
			// URL to open on pageear click
			jumpTo: 'http://www.webpicasso.de/blog/',
			// Öffnet den link im neuen Fenster (new) oder im selben (self)
			// Browser target  (new) or self (self)
			openLink: 'new', 
			// Öffnet das pagepeel automatisch wenn es geladen ist (false:deaktiviert | 0.1 - X Sekunden bis zum öffnen) 
			// Opens pageear automaticly (false:deactivated | 0.1 - X seconds to open) 
			openOnLoad: 3, 
			// Sekunden bis sich das pagepeel wieder schließt, funktioniert nur im Zusammenhang mit der openOnLoad-Funktion 
			// Second until pageear close after openOnLoad
			closeOnLoad: 3, 
			// Ecke in der das Pagepeel erscheinen soll (lt: linke obere Ecke | rt: rechte obere Ecke )
			// Set direction of pageear in left or right top browser corner (lt: left | rt: right )
			setDirection: 'rt', 
			// Weiches einblenden des pageear wenn Bild geladen (0-5: 0=aus, 1=langsam, 5=schnell )
			// Fade in pageear if image completly loaded (0-5: 0=off, 1=slow, 5=fast )
			softFadeIn: 1, 
			// Hintergrundsound einmalig abspielen (false:deaktiviert | URL:Mp3 File z.b. www.domain.de/meinsound.mp3)
			// Plays background music once abspielen (false:deactivated | URL:Mp3 File e.g. www.domain.de/mysound.mp3) 
			playSound: 'false',
			// Spielt Sound beim öffnen (false:deaktiviert | URL:Mp3 File z.b. www.domain.de/meinsound.mp3)
			// Play sound on opening peel (false:deactivated | URL:Mp3 File e.g. www.domain.de/mysound.mp3) 
			playOpenSound: 'false', 
			// Spielt Sound beim schließen (false:deaktiviert | URL:Mp3 File z.b. www.domain.de/meinsound.mp3)
			// Play sound on closing peel (false:deactivated | URL:Mp3 File e.g. www.domain.de/mysound.mp3) 
			playCloseSound: 'false', 
			// Wird das peel geöffnet, bleibt es solange offen bis auf das schließen-Symbol geklickt wird
			// Peel close first if button close will be clicked
			closeOnClick: 'false',
			// Text neben schließen-Symbol
			// Close text 
			closeOnClickText: 'Close',
			// Flash check vars
			requiredMajorVersion: 6,
			requiredMinorVersion: 0,
			requiredRevision: 0,

			// Copyright
			copyright: 'Webpicasso Media, www.webpicasso.de',
//			copyright: 'CSS/Javascript: @rtplan21 - webconcepts & solutions GmbH., www.artplan21.de | Original Implementation & Flash: Webpicasso Media, www.webpicasso.de',

			// Size small peel 
			smallWidth: 100,
			smallHeight: 100,

			// Size big peel
			bigWidth: 500,
			bigHeight: 500,

			// Css style default x-position
			xPos: 'right',
			templateSmall: {
				url:'...',
				id:'',
				width:'',
				height:'',
				flash:'6',
				xi:'/fileadmin/gpd/basics/javascript/swfobject.2.2/expressInstall.swf',
				flashvars:null,
				parameters:{
					allowFullScreen:'false',
					allowScriptAccess:'samedomain',
					wmode:'transparent',
					quality:'high',
					name:'..._flash',
					scale:'noscale',
					salign:'tr',
					align:'middle'
				},
				attributes:null
			},
			templateBig: {
				url:'...',
				id:'',
				width:'',
				height:'',
				flash:'6',
				xi:'/fileadmin/gpd/basics/javascript/swfobject.2.2/expressInstall.swf',
				flashvars:null,
				parameters:{
					allowFullScreen:'false',
					allowScriptAccess:'samedomain',
					wmode:'transparent',
					quality:'high',
					name:'..._flash',
					scale:'noscale',
					salign:'tr',
					align:'middle'
				},
				attributes:null
			}
		};
		app.prototype.getQueryParams = function(){

			// GET - Params
			var queryParams = '?pagearSmallImg='+escape(this.options.smallImg); 
			queryParams += '&pagearBigImg='+escape(this.options.bigImg); 
			queryParams += '&pageearColor='+escape(this.options.color); 
			queryParams += '&jumpTo='+escape(this.options.jumpTo); 
			queryParams += '&openLink='+escape(this.options.openLink); 
			queryParams += '&mirror='+escape(this.options.mirror); 
			queryParams += '&copyright='+escape(this.options.copyright); 
			queryParams += '&speedSmall='+escape(this.options.speedSmall); 
			queryParams += '&openOnLoad='+escape(this.options.openOnLoad); 
			queryParams += '&closeOnLoad='+escape(this.options.closeOnLoad); 
			queryParams += '&setDirection='+escape(this.options.setDirection); 
			queryParams += '&softFadeIn='+escape(this.options.softFadeIn); 
			queryParams += '&playSound='+escape(this.options.playSound); 
			queryParams += '&playOpenSound='+escape(this.options.playOpenSound); 
			queryParams += '&playCloseSound='+escape(this.options.playCloseSound);  
			queryParams += '&closeOnClick='+escape(this.options.closeOnClick); 
			queryParams += '&closeOnClickText='+escape(application.lib.utf8.encode(this.options.closeOnClickText)); 
			queryParams += '&lcKey='+escape(Math.random()); 
			queryParams += '&bigWidth='+escape(this.options.bigWidth); 
			queryParams += '&thumbWidth='+escape(this.options.smallWidth);

			return queryParams;
		};
		app.prototype.renderSection = function(id,className){
			var div = $(document.createElement('div'));
			div.attr('id',id);
			if (className) div.addClass(className);
			return div;
		};
		app.prototype.render = function(id,className){
			var self = this;
			$(self).bind('open.hexear',function(){self.open();});
			$(self).bind('close.hexear',function(){self.close();});

			var div = this.renderSection(id,className);
			var bigDiv = this.renderSection(this.options.bigId,'') 
			var smallDiv = this.renderSection(this.options.smallId,'') 
			bigDiv.append(this.renderSection(this.options.bigId+'-object',''))
			smallDiv.append(this.renderSection(this.options.smallId+'-object',''))
			div.append(bigDiv);
			div.append(smallDiv);

			$(this.options.carrier).prepend(div);

			var bigOpts = this.options.templateBig;
			var smallOpts = this.options.templateSmall;

			bigOpts.url = this.options.bigSwf + this.getQueryParams();
			bigOpts.id = this.options.bigId + '-object';
			bigOpts.width = this.options.bigWidth;
			bigOpts.height = this.options.bigHeight;
			bigOpts.parameters.name = this.options.bigId + '-flash';
			bigOpts.parameters.bgcolor = '#' + this.options.color;
			bigOpts.attributes = {id: this.options.bigId+'-flash-object'};

			smallOpts.url = this.options.smallSwf + this.getQueryParams();
			smallOpts.id = this.options.smallId + '-object';
			smallOpts.width = this.options.smallWidth;
			smallOpts.height = this.options.smallHeight;
			smallOpts.parameters.name = this.options.smallId + '-flash';
			smallOpts.parameters.bgcolor = '#' + this.options.color;
			smallOpts.attributes = {id: this.options.smallId+'-flash-object'};

			$(document).ready(function(){
/*
				console.log(bigOpts.url);
				console.log(bigOpts.id);
				console.log(bigOpts.width);
				console.log(bigOpts.height);
				console.log(bigOpts.flash);
				console.log(bigOpts.flashvars);
				console.log(bigOpts.parameters);
				console.log(bigOpts.attributes);
*/
				swfobject.embedSWF(
					bigOpts.url,
					bigOpts.id,
					bigOpts.width,
					bigOpts.height,
					bigOpts.flash,
					bigOpts.xi,
					bigOpts.flashvars,
					bigOpts.parameters,
					bigOpts.attributes
				);
				$('#'+bigOpts.attributes.id).hover(function(e){$(e.target).blur();$(this).blur();});
				$('#'+bigOpts.attributes.id).click(function(e){$(e.target).blur();$(this).blur();});
/*
				console.log(smallOpts.url);
				console.log(smallOpts.id);
				console.log(smallOpts.width);
				console.log(smallOpts.height);
				console.log(smallOpts.flash);
				console.log(smallOpts.flashvars);
				console.log(smallOpts.parameters);
				console.log(smallOpts.attributes);
*/
				swfobject.embedSWF(
					smallOpts.url,
					smallOpts.id,
					smallOpts.width,
					smallOpts.height,
					smallOpts.flash,
					smallOpts.xi,
					smallOpts.flashvars,
					smallOpts.parameters,
					smallOpts.attributes
				);
				$('#'+smallOpts.attributes.id).hover(function(e){$(e.target).blur();$(this).blur();});
				$('#'+smallOpts.attributes.id).click(function(e){$(e.target).blur();$(this).blur();});
			});
		};
		app.prototype.open = function(){
			$('#'+this.options.bigId).css({
				'top': '0px',
				'z-index': '9999'
			});
			$('#'+this.options.bigId).children().blur();
			$('#'+this.options.smallId).css({
				'top': '-1000px',
				'z-index': '9998'
			});
			$('#'+this.options.smallId).children().blur();
			//console.log('Hexear Opened');
		};
		app.prototype.close = function(){
			$('#'+this.options.smallId).css({
				'top': '0px',
				'z-index': '9999'
			});
			$('#'+this.options.smallId).children().blur();
			$('#'+this.options.bigId).css({
				'top': '-1000px',
				'z-index': '9998'
			});
			$('#'+this.options.bigId).children().blur();
			//console.log('Hexear Closed');
		};
		return new app(appId,appOpts);
	};

})(jQuery);

function openPeel(){
	//console.log('Hexear Open Raw');
	jQuery(application.control('tx-hexear')).trigger('open.hexear');
}

function closePeel(){
	//console.log('Hexear Close Raw');
	jQuery(application.control('tx-hexear')).trigger('close.hexear');
}
