var JsDialog = {
    //strings already translated via ajax
    translations: {},

	init: function() {

            //show important messagse sent from the server, if any
            this.displayFlashMessages();

            //url comman to play
            if(General.getURLParam('watchTutorial'))
                this.playTutorial();
                       
	},

          /* a generic dialog box */
 dialog: function(message, title, options) {
 //keep script from breaking
        if(typeof title == 'undefined' || title == null )
            title == '';

		if(typeof options != 'object' )
            options = {};

		if (typeof(message) === 'undefined')
            message = ''


        var defaults = {
			closeHTML: "<a href='#' title='Close' class='modal-close'>&nbsp;</a>",
			position: ["20%",],
			overlayId: 'modal-dialog-overlay',
			containerId: 'modal-dialog-container',
			overlayClose: true,
			minHeight: 180,
			minWidth: 420,
            addClass: '',
			buttons: [
				{text:'Close', className: 'simplemodal-close'}
			],
			onOpen: function (dialog) {
				$('.message', dialog.data[0]).append(message).css('height',dialog.container.height()-96);
				if (typeof title != 'undefined')
					$('.modalHeader span.title', dialog.data[0]).text(title);

				/*function(dialog){
				$('.buttonsWrapper', dialog.data[0]).html(<a>);
				}*/
					dialog.overlay.fadeIn('fast', function () {
					dialog.data.show();
					dialog.container.fadeIn('fast');
				});
			},

            onClose: function (dialog) {
                dialog.container.fadeOut('slow', function () {
                    dialog.overlay.fadeOut('fast');
                    $.modal.close();
                });
            }
	}
	options = $.extend({}, defaults, options);
     var msgType = 'modal-dialog-'+ options.msgType
		var dialogBox ='<div id="modal-dialog" class="'+msgType +' '+options.addClass+'">';
		dialogBox += '<div class="modalHeader"><span class="title"></span></div>';
		dialogBox += '<div class="message"></div>';
		dialogBox += '<div class="buttonsWrapper">';
		$.each(options.buttons , (function(){
			dialogBox += '<a href="';
			if (this.href)
				dialogBox += this.href;

			dialogBox += '" class="button';
			if (this.className)
				dialogBox += ' ' + this.className;
			dialogBox += '" ';
			if (this.onclick)
				dialogBox += ' onclick="' + this.onclick + '" ';
			dialogBox += '> ' + this.text + '</a>';
		}));
		dialogBox += '</div></div>';
	$(dialogBox).modal(options);

	},

       stopPropagation: function(event){
         event.stopPropagation();
       },

	 loginDialog: function(sInstructions){
            var loginDialog = $('#loginDialog');
			 if(typeof sInstructions != 'undefined'){
             $('div.instructions',loginDialog).html(sInstructions);
			 loginDialog.find('input:submit').addClass('button');
           }
           JsDialog.dialog( loginDialog, AppData.generalLang.startTrading, {
                minHeight: '276',
                title: AppData.generalLang.startTrading,
                buttons: [{text: AppData.generalLang.openAccount,  href:'OpenAccount'},
                           {text: "Forgot Password?", href:'ForgotPassword'}]
                }
            );
       },

        /*
	* useage
	*   General.jnotification({
			removebutton     : false, //if to show close button. default false
			message			 : 'error reading the file!', //your message
			time			 : 4000, //timeout. default 5000
			msgType		     : 'error', // can be notice, success, warning, error. default value 'notice'.
			className        : 'myClass' //custom class if needed
		})});
	*/
	jnotification: function(options) {
		var defaults = {
		removebutton     	: false,
		time			 	: 5000,
		msgType             : 'notice',
		className			: ''
		};

		var o = $.extend({}, defaults, options);
				if(!$('.jnotification').length){    
					var timeout = setTimeout('removebar()',o.time);
					var _message_span = $(document.createElement('span')).addClass('jnotification-content').html(o.message);
					var _wrap_bar;
					_wrap_bar	  = $(document.createElement('div')).addClass('jnotification jnotification_'+o.msgType) ;
					if(o.className) _wrap_bar.addClass(o.className);

					if(o.removebutton){
						var _remove_cross = $(document.createElement('a')).addClass('jnotification-cross');
						_remove_cross.click(function(e){removebar();})
					}
					else{
						_wrap_bar.css({"cursor"	: "pointer"});
						_wrap_bar.click(function(e){removebar();})
					}
					_wrap_bar.append(_message_span).append(_remove_cross).insertBefore($('.container'))//.fadeIn('fast');
					.css({top:'-50px'}).animate({top: 0}, 200);
				}
		removebar = function () {
			if($('.jnotification').length){
				clearTimeout(timeout);
				$('.jnotification')//.fadeOut('fast',function(){
					.animate({top: '-50px'}, 200, function(){
					$(this).remove();
				});
			}
		};
	},
        /*display flash messages shows after server redirect */
    displayFlashMessages: function() {

            if(AppData.flashMessage != undefined){
                var len = AppData.flashMessage.length;
                for (i=0; i <len ;i++){
                    if (AppData.flashMessage[i].options.msgDisplay == 'notification'){
                        var params = $.extend({}, AppData.flashMessage[i].options , {message: AppData.flashMessage[i].message});
                        JsDialog.jnotification(params);
                    }else{ //fallback option is dialog
                       if(typeof AppData.flashMessage[i].options.title == 'string')
                            var title = AppData.flashMessage[i].options.title;
                        else
                            var title = '';
                        JsDialog.dialog(AppData.flashMessage[i].message, title ,AppData.flashMessage[i].options);
                    }
                }
            }
    },
    getTextDialog:function(dailogId, options){
		var defaults = {minHeight: 530 , minWidth:720};
          $.getJSON('rpcProxy/getTextDialog/'+AppData.langId+'/'+dailogId,
		  function(data) {
               //window dimentions might be coming from db
              if(data.dimentions){
                  var dimentions = { minWidth: data.dimentions.width ,
                                minHeight: data.dimentions.height}
              }else
                  var dimentions = {};

              var extendedOptions = $.extend({}, defaults, dimentions ,options);
              var textDiv = document.createElement('div');
          JsDialog.dialog( $(textDiv).html(data.content), data.title, extendedOptions );
        });
    },
    getTemplate:function(templateName, options){
		var defaults = {minHeight: 530 , minWidth:620};
		var extendedOptions = $.extend({}, defaults, options);
          $.get( 'DialogRPC/'+templateName ,
		  {pageId:AppData.pageId , langId:AppData.langId},
		  function(data) {
              var textDiv = document.createElement('div');
          JsDialog.dialog( $(textDiv).html(data),'', extendedOptions );
        });
    },
              /* a platform gpraphs dialog box */
 graphDialog: function(message, title, options) {
 //keep script from breaking
        if(typeof title == 'undefined' || title == null )
            title == '';

		if(typeof options != 'object' )
            options = {};

		if (typeof(message) === 'undefined')
            message = ''


        var defaults = {
			closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
			position: ["20%",],
			overlayId: 'modal-dialog-overlay',
			containerId: 'modal-Graphdialog-container',
			overlayClose: true,
			minHeight: 180,
			minWidth: 420,
			buttons: [
				{text:'Close', className: 'simplemodal-close'}
			],
			onOpen: function (dialog) {
				$('.message', dialog.data[0]).append(message).css('height',dialog.container.height()-96);
				if (typeof title != 'undefined')
					$('.modalHeader span.title', dialog.data[0]).text(title);

				/*function(dialog){
				$('.buttonsWrapper', dialog.data[0]).html(<a>);
				}*/
					dialog.overlay.fadeIn('fast', function () {
					dialog.data.show();
					dialog.container.fadeIn('fast');
				});
			},

            onClose: function (dialog) {
                dialog.container.fadeOut('slow', function () {
                    dialog.overlay.fadeOut('fast');
                    $.modal.close();
                });
            }
	}
	options = $.extend({}, defaults, options);
        var msgType = 'modal-dialog-'+ options.msgType
		var dialogBox ="<div id='modal-dialog' class='"+msgType+"'>";
		dialogBox += '<div class="modalHeader"><span class="title"></span></div>';
		dialogBox += '<div class="message"></div>';
		dialogBox += '<div class="buttonsWrapper">';
		$.each(options.buttons , (function(){
			dialogBox += '<a href="';
			if (this.href)
				dialogBox += this.href;

			dialogBox += '" class="button';
			if (this.className)
				dialogBox += ' ' + this.className;
			dialogBox += '" ';
			if (this.onclick)
				dialogBox += ' onclick="' + this.onclick + '" ';
			dialogBox += '> ' + this.text + '</a>';
		}));
		dialogBox += '</div></div>';
	$(dialogBox).modal(options);

	},

       stopPropagation: function(event){
         event.stopPropagation();
       },

	 loginDialog: function(sInstructions){
            var loginDialog = $('#loginDialog');
			 if(typeof sInstructions != 'undefined'){
             $('div.instructions',loginDialog).html(sInstructions);
			 loginDialog.find('input:submit').addClass('button');
           }
           JsDialog.dialog( loginDialog, AppData.generalLang.startTrading, {
                minHeight: '276',
                title: AppData.generalLang.startTrading,
                buttons: [{text: AppData.generalLang.openAccount,  href:'OpenAccount'},
                           {text: "Forgot password?", href:'ForgotPassword'}]
                }
            );
       },
      playTutorial: function(){
          //first remder the video
          var html='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
+'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,16,0"'
+'width="800" height="668" >'
+'<param name="movie" value="appProxy/tutorialVideo.swf">'
+'<param name="quality" value="high">'
+'<param name="play" value="true">'
+'<param name="LOOP" value="false">'
+'<embed src="appProxy/tutorialVideo.swf" width="800" height="668" play="true" loop="false" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer"'
+'type="application/x-shockwave-flash">'
+'</embed>'
+'</object>';


          if(jQuery.browser.webkit){
              JsDialog.jnotification({message:'Tutorial Video is loading', time:4000});
          }
          var o = {
            position: ["1%",],
            minHeight: 765,
			minWidth: 870,
            buttons: [
				{text:'Close', className: 'simplemodal-close'}
			]
            
          };

        JsDialog.dialog(html,'OneTwoTrade.com Tutorial Video',o);
}



}
$(document).ready(function() {
    
	JsDialog.init();
});

//shortCut
function playTutorial(){JsDialog.playTutorial()};
