/* ------------------------------------------------------------------------------------------------------------ */

$(function(){
	$( document ).click(function( ev ) {
		var isLeftButton;
		ev = ev || window.event;
		if ( ev.which ) {
			isLeftButton = ( ev.which == 1 );
		} else if ( ev.button ) {
			isLeftButton = ( ev.button == 0 );
		}

		if(
			isLeftButton &&
			$( '.colorpicker:not(":hidden")' ).size() == 0
		) {
			$( '.settings-on' ).find( '.showSettings a' ).html( window.button[ 'show' ] );
			$( '.settings-on' ).removeClass( 'settings-on' ).find( '.paramInformer' ).attr({ 'style': 'display: none;' });
		}
	});

	$( '.informerContainer' ).click(
		function( ev ) {
			ev = ev || window.event;
			ev.stopPropagation ? ev.stopPropagation() : ev.cancelBubble = true;
		}
	);

	$( '.getcode input' ).change(function(){
		var parent = $( this ).parents( 'li' );
		if( $( this ).is( ':checked' ) ) {
			parent.removeClass( 'required' );
		} else {
			parent.addClass( 'required' );
		}
	});

	$( '.off a' ).click(
		function( ev ) {
			ev = ev || window.event;
			ev.preventDefault ? ev.preventDefault() : ev.returnValue = false;
			$( this ).parents( '.getcode' ).find( 'input:not(":checked")' ).parents( 'li' ).addClass( 'required' )
		}
	);

	$( '.showSettings a' ).click(
		function( ev ) {
			ev = ev || window.event;
			ev.preventDefault ? ev.preventDefault() : ev.returnValue = false;

			var container = $( this ).parents( '.informerContainer' );
			var a = container.find( '.showSettings a' );
			
			if( window.GAO ) {
				GAO.trackPageview( a.attr( 'href' ) );
			} else if( window.pageTracker ) {
				window.pageTracker._trackPageview( a.attr( 'href' ) );
			}

			if( container.hasClass( 'settings-on' ) ) {
				container.find( '.paramInformer' ).slideUp(
					400, 
					function() {
						container.removeClass( 'settings-on' );
					}
				);
				a.html( window.button[ 'show' ] );
				a.attr( 'href', a.attr( 'href' ).replace( '-hide', '-build' ) );
			} else {
				$( '.settings-on' ).find( '.showSettings a' ).html( window.button[ 'show' ] );
				$( '.settings-on' ).removeClass( 'settings-on' ).find( '.paramInformer' ).attr({ 'style': 'display: none;' });
				
				container.addClass( 'settings-on' ).find( '.paramInformer' ).slideDown( 400, function() {
					var bottom = container.find( '.bottom-border' );
					var bottomOffset = container.find( '.bottom-border' ).offset();
				});
				a.html( window.button[ 'hide' ] );
				a.attr( 'href', a.attr( 'href' ).replace( '-build', '-hide' ) );
			}
		}
	);
});

/* ------------------------------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings = function( options ) {
	if(
		!options ||
		!options[ 'id' ] ||
		!options[ 'script' ] ||
		!options[ 'path' ]
	) return false;
	
	InformerSettings.superClass.apply( this, arguments );
	this.element = this.getEl( options[ 'id' ] );
	this.informerWrap = $( this.element ).find( '.showInformer' );
	this.settingsWrap = $( this.element ).find( '.paramInformer' );
	this.codeWrap = this.settingsWrap.find( 'textarea.source' );

	this.domain = 'http://informer.test1.alpari.dom';
	/*'http://www..ru';*/
	/*window.location.protocol + '//' + window.location.host; */

	this.link = options[ 'link' ] || 'http://www.alpari.ru';
	this.path = '/ru' + options[ 'path' ] || '/';
	this.scriptPath = options[ 'script' ];

	this.params = options[ 'params' ] || new Object();
	this.params[ 'site' ] = window.location.host;
	this.word = options[ 'word' ] || 'Forex';

	if( options[ 'labels' ] ) {
		this.labels = options[ 'labels' ];
	}

	this.addControls();
	this.addAgreement();

	this.updateInformer();
}
InformerSettings.inheritsFrom( Glyph );

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.addControls = function() {
	this.addValueControls();
	this.addToolControls();
	this.addGraphControls();
	this.addColorControls();
	this.addFlagControls();
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.addFlagControls = function() {
	var _self = this;
	this.settingsWrap.find( 'input.flag' ).each(function() {
		_self.updateParams( $( this ).attr( 'name' ), $( this ).is( ':checked' ) );
		$( this ).change(function() {
			_self.updateParams( $( this ).attr( 'name' ), $( this ).is( ':checked' ) );
		});
	});
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.addValueControls = function() {
	var _self = this;
	for( var c in this.labels ) {
		var input = this.settingsWrap.find( '.' + c );
		input.each(function() {
			var label = _self.labels[ $( this ).attr( 'name' ) ];
			$( this ).val( label );
			$( this ).focus(function(){
				if( $( this ).val() == label ) {
					$( this ).val( '' );
				}
			});
			$( this ).blur(function(){
				if( $( this ).val() == '' ) {
					$( this ).val( label );
				}
			});
			$( this ).change(function(){
				if( $( this ).val() ) {
					_self.updateParams( $( this ).attr( 'name' ), $( this ).val() );
				}
			});
		});
	}
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.addToolControls = function() {
	var _self = this;
	this.settingsWrap.find( '.tool' ).each(function() {
		var _id = $( this ).attr( 'id' );
		if( _id ) {
			var list = new CheckList( _id );
			if( list ) {
				list.attachObserver( 'Changed', function( params, list ) {
					this.updateParams( list.element.getAttribute( 'name' ), list.getValue().join( ',' ) );
				}, _self );
				_self.updateParams( list.element.getAttribute( 'name' ), list.getValue().join( ',' ) );
			}
		}
	});
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.addGraphControls = function() {
	var _self = this;
	this.settingsWrap.find( '.graph' ).each(
		function() {
			function selectGraph() {
				var parent = $( this ).parents( '.graph' );
				var isChecked = $( this ).is( ':checked' );
				parent.find( '.color' ).css({
					'visibility': isChecked ? 'visible' : 'hidden'
				});
				isChecked ? parent.removeClass( 'disabled' ) : parent.addClass( 'disabled' );
				_self.updateParams( $( this ).attr( 'name' ), isChecked );
			}

			var input = $( this ).find( 'input[ type="checkbox" ]' );
			input.click( selectGraph );
			selectGraph.call( input.get( 0 ) );
		}
	);
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.addColorControls = function() {
	var _self = this;
	this.settingsWrap.find( '.color' ).each(
		function() {
			var _current = $( this );
			var _input = $( '#' + _current.attr( 'id' ) + '_input' );
			var _color = _input.val();

			$( this ).css( 'backgroundColor', '#' + _color );
			
			$( this ).ColorPicker({
				color: '#' + _color,
				onShow: function ( colpkr ) {
					$( colpkr ).fadeIn( 500 );
					return false;
				} ,
				onHide: function ( colpkr ) {
					$( colpkr ).fadeOut( 500 );
					return false;
				},
				onChange: function ( hsb, hex, rgb ) {
					_current.css( 'backgroundColor', '#' + hex );
					_input.val( hex );
					_self.updateParams( _input.attr( 'name' ), hex + '' );
				}
			});
			_self.updateParams( _input.attr( 'name' ), _color );
		}
	);
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.addAgreement = function() {
	var _self = this;
	var wrap = this.settingsWrap.find( '.getcode' );
	this.button = wrap.find( 'a' );
	
	this.button.click(function( ev ){
		ev = ev || window.event;
		ev.preventDefault ? ev.preventDefault() : ev.returnValue = false;

		if( window.GAO ) {
			GAO.trackPageview( $( this ).attr( 'href' ) );
		} else if( window.pageTracker ) {
			window.pageTracker._trackPageview( $( this ).attr( 'href' ) );
		}

		_self.setCode();
	});

	this.agreements = wrap.find( 'input' );

	this.agreements.change(function() {
		if( _self.isValid() ) {
			$( wrap ).removeClass( 'off' );
		} else {
			$( wrap ).addClass( 'off' );
		}
	});
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.updateParams = function( key, value ) {
	if( value ) {
		this.params[ key ] = value;
	} else {
		delete this.params[ key ];
	}
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.updateSource = function() {
	var id = 'ai' + this.getRnd();
	var code =	'<!-- alpari_informer_start --><div id="' + id + '">' +
				'<a href="' + this.link + '">' + /*this.title + ' ' +*/ this.word + '</a>' +
				'<script type="text/javascript">' +
				'document.write( decodeURI( "%3Cscript src=\'' + this.scriptPath + '?enc=" + ( document.charset || document.characterSet ) + "&path=' + this.domain + this.path + this.getParams() + '&id=' + id + '\' type=\'text/javascript\'%3E%3C/script%3E" ));' +
				'</script>' +
				'</div><!-- alpari_informer_end -->';

	this.codeWrap.val( code );
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.updateInformer = function() {
	var id = 'ai' + this.getRnd();

	var code =	'<!-- alpari_informer_start --><div id="' + id + '">' +
				'<a href="' + this.link + '">' + /*this.title + ' ' +*/ this.word + '</a>' +
				'<script type="text/javascript" src="' + this.scriptPath + '?enc=utf-8&path=' + this.domain + this.path + this.getParams() + '&id=' + id + '" type="text/javascript"></script>' +
				'</div><!-- alpari_informer_end -->';
	this.informerWrap.html( code );
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.setCode = function() {
	if(
		!this.isValid ||
		!this.isValid()
	) return false;

	this.updateSource();
	this.updateInformer();
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.getParams = function() {
	var paramsToStr = '';

	for( var c in this.params ) {
		paramsToStr += ( c + '/' + this.params[ c ] + '/' );
	}
	return paramsToStr;
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.isValid = function() {
	var isValid = true;
	$( this.agreements ).each(function(){
		if( !$( this ).is( ':checked' ) ) isValid = false;
	});
	return isValid;
}

/* ------------------------------------------------------------------------------------------------------------ */

InformerSettings.prototype.getRnd = function() {
	return Math.round( Math.random() * ( 9999 - 1000 ) + 1000 );
}
