/*
    Document   : platformNav
    Created on : 03/08/2010, 16:08:11
    Author     : Nadav
    Description:
        Purpose of the js: code for platform navigation tabs and buttons.
*/

var PlatformNav =  {
	container: null,
        currentCategory: null, //binary options, binary builder, oneTouch
        currentAssetType: null, // top picks, stocks, currencies...
        platformMode: null, //fast, pro
        
        toolbar:{
           bottomBarIsOut: true, //bottom bar state
           hotTips:{totlLines: null,
                   currentLine: 1}
        },

        init: function(){
                PlatformNav.container = $('#mainContainer');

                var platformNavPane = PlatformNav.container.find('#platformNav');
                platformNavPane.find("ul.mainCategories li").click(this.onClickNavTabs);
                platformNavPane.find("div.assetTypes div.assetButton").click(this.onClickassetType);
                this.currentCategory = platformNavPane.find("ul.mainCategories li.selected:eq(0)").attr('id').split("_",2)[1];
                this.currentAssetType = platformNavPane.find("div.assetTypes div.selected:eq(0)").attr('id').split("_",2)[1];
                this.platformMode = AppData.platformMode;


                /*toolbar hot tips*/
                //total amout of lines = hotlistUl.height() / 20 (line height)
                //PlatformNav.toolbar.hotTips.totalLines = (PlatformNav.container.find('#hotTipsUl').height() / 20);
                //get it height, and then hide it
                //PlatformNav.container.find('#hotTipsUl').parent().addClass('hidden')
         
                //setInterval ( 'PlatformNav.hotTipScroll()', 5000 );

                //see if customer had a cookie stored preference for bottom bar
                //if (iOkun.readCookie('bottomBarIsOut') == 'false') {
                //        PlatformNav.container.find("div#platformBottomBar").css('right', '-1033px');
                // }
                /*if( General.getCookie('bottomBarIsOut') == 'false'){
                      PlatformNav.container.find("div#platformBottomBar").css('right', '-1033px');
                }*/
                //bottom bar
                $("#platformBottomBar ul.tabs li").click(this.onClickBottomBarTabs);
                $("#platformBottomBar div.inOutControl").click(this.onClickinOutBottomBar);
                $("#platformBottomBar a.academyBtn").click(PlatformNav.onClickAcadamy);
                // NOT THIS BUTTON $("div#platformBottomBar a.academyBtn").click(this.onClickinOutBottomBar);

                PlatformNav.container.find('#topBarDemo').click(PlatformNav.onPlayDemoVideo);
                //PlatformNav.container.find('#topBarLearn').click(PlatformNav.onClickTopBarLearn);

                
                PlatformNav.container.find('#homePromoBanner input.closePromotion').click(PlatformNav.onClosePromotion);

                

          //in no regular binary options, show one touch
          if( !AppData.isBinaryOptions && AppData.isOneTouchOptions)
              //needs a delay for stuff to load
              setTimeout("$('#category_oneTouch').click()",1);
              

           //handle autoplay of demo after being redirected 
          if(General.getURLParam('autoPlayDemo') != ''){
              PlatformNav.handleAutoPlayDemo();
          }
                
                
          var timer = setInterval(function(){
                                             if (!$('ul#optionsList li.loading').length){
                                                 /* The options already loaded */
                                                 
                                                 switch (AppData.defaultTab){
                                                     case 'regular':
                                                         $('li#category_binaryOptions').click();
                                                     break;
                                                         
                                                     case 'onetouch':
                                                         $('li#category_oneTouch').click();
                                                     break;

                                                     case 'optionBuilder':
                                                         $('li#category_optionsBuilder').click();
                                                     break;

                                                     case 'sixtySeconds':
                                                         $('li#category_sixtySeconds').click();
                                                     break;
                                                 }
                                                 
                                                 clearInterval(timer);
                                             }else{
                                                 /* not loaded yet */
                                             }
                                          }, 1000);
          },
          
        onClickNavTabs: function(event){
            /* 
             * posibilities are: binaryOptions, optionsBuilder ,oneTouch
             */
            var platformNavPane = PlatformNav.container.find('div#platformNav');

            var newCategory = event.target.id.split("_",2)[1];//get the info we need from category_XXX format

            /* check if all options already loaded */
            if ($('ul#optionsList > li:not(.customOption)').filter('.loading').length){
                /* in not all loading check if we want to open Binary Options tab*/
                if (newCategory!='binaryOptions')
                    return;
            }
            
            //if (newCategory == 'optionsBuilder' && !AppData.isLoggedIn) return;
            
            /* sometimes the assetChart_0 div is not created */
            if (newCategory == 'optionsBuilder' && !$('div#assetChart_0').length) {
                Graphs.loadChartData('assetChart_0');
                //return;
            }

            var oldCategory = PlatformNav.currentCategory;
            if (oldCategory == newCategory)
                return;

            //if a demo video is playing, remove it
            PlatformNav.demoVideoClose();
            
            PlatformNav.currentCategory = newCategory;
            //change image of tabs
            if (AppData.optionsBuilderEnabled)
                platformNavPane.removeClass(oldCategory +'_tab').addClass(newCategory +'_tab');
            else
                platformNavPane.removeClass(oldCategory +'_tab_old').addClass(newCategory +'_tab_old');
            
            platformNavPane.find("ul.mainCategories li.selected").removeClass('selected');
            platformNavPane.find("ul.mainCategories li#category_" + newCategory).addClass('selected');
           
            var topBar = PlatformNav.container.find('div#topPlatformBar');
            topBar.removeClass('topBar_' + oldCategory).addClass('topBar_' + newCategory)
                      .find('span.title').text(AppData.langHome[newCategory]);
           


            PlatformNav.noTradesMsg('hide');//hide no tradeable options availible message

            $('#' +oldCategory + 'Banner' ).hide();
            $('#' +newCategory + 'Banner' ).show();
             switch (oldCategory)
             {
                 case 'binaryOptions':
                  Home.showOptions('show');
                  break;
                case 'optionsBuilder':
                 if (AppData.numberOfOptionBuilderAssets){
                    PlatformNav.container.find('#optionsBuilderBoxMessage').hide();
                    PlatformNav.container.find('#optionsBuilderBox').show();
                 }
                 else{
                     PlatformNav.container.find('#optionsBuilderBoxMessage').show();
                     PlatformNav.container.find('#optionsBuilderBox').hide();
                 }
                 
                  platformNavPane.find('div.assetTypes').show();
                  PlatformNav.container.find('#optionsList').show();
                  break;
                case 'oneTouch':
                 //pro removes the option, fast hides them
                     platformNavPane.find('div.assetTypes').show();
                     if(typeof OneTouch == 'object'){
                         OneTouch.hideOptions();
                     }
                  break;
             }
             switch (newCategory) /* newCategory = name of the clicked tab */
             {
                 case 'binaryOptions':
                   PlatformNav.container.find('#optionsBuilderBoxMessage').hide();
                   PlatformNav.container.find('#optionsBuilderBox').hide();

                   /* hide custom options and show regular options */
                   PlatformNav.container.find('#optionsList li.optionBox').removeClass('hidden');
                   PlatformNav.container.find('#optionsList li.customOption').hide();
                   PlatformNav.container.find('#optionsList li.customOption ul.actions *').show();
                   PlatformNav.container.find('#optionsList div.positionTitle ul li').show();

                   PlatformNav.container.find('div.platformModeSwitch').show();
                 
                  if( AppData.isBinaryOptions){//are there any option
                     Home.showOptions();
                     $('ul#optionsList > li.customOption').hide();
                 }
                 else
                    PlatformNav.noTradesMsg();
                  break;
                case 'optionsBuilder':
                   /* load the chart for the option builder in case its not loaded */
                   Graphs.loadChartData('assetChart_0');
                   if (!$('div#assetChart_0').length){
                       //$('li#category_optionsBuilder').click();
                       Graphs.loadChartData('assetChart_0');
                   }
                   $('ul#optionsList > li.customOption').show();

                  /* if this is the compact page then redirect to pro trader and click on options builder */
                  if (AppData.platformMode == 'fast'){
                      document.location = AppData.url + 'home/pro/builder';
                      return;
                  }

                 /* set all custom options as loading */
                 //$('ul#optionsList').children('li.customOption').addClass('loading');
		$('#optionsList').removeClass('hidden');
                 PlatformNav.container.find('div.platformModeSwitch').hide();
                 if (AppData.numberOfOptionBuilderAssets != 0){
                    PlatformNav.container.find('#optionsBuilderBoxMessage').hide();
                    PlatformNav.container.find('#optionsBuilderBox').show();
		    
                 }
                 else{
                     PlatformNav.container.find('#optionsBuilderBoxMessage').show();
                     PlatformNav.container.find('#optionsBuilderBox').hide();
                 }
                 
                 platformNavPane.find('div.assetTypes').hide();

                 /* hide regular options and show custom options */
                 PlatformNav.container.find('#optionsList li:not(.customOption)').hide();
                 PlatformNav.container.find('#optionsList li.customOption').show();
                 PlatformNav.container.find('#optionsList li.customOption ul.actions *').show();

				 //Load the asset graph of the custom option
				 OptionsBuilder.loadGraph();

                 //var customOptions = PlatformNav.container.find('#optionsList li.customOption');
                 //$.each(customOptions,function(index,element){
                 //   Home.completeLoadingBoxList($(this));
                 //});

                 OptionsBuilder.getOptionsEndDate();

                 break;
                case 'oneTouch':
                    /* hide options builder */
                    PlatformNav.container.find('#optionsBuilderBoxMessage').hide();
                    PlatformNav.container.find('#optionsBuilderBox').hide();

                   /* hide custom options and regular options */
                   PlatformNav.container.find('#optionsList li.customOption').hide();

                   PlatformNav.container.find('div.platformModeSwitch').show();

                 //pro removes the option, fast hides them
                 //show OT promo, hide regular promo. hide regular items
//                 /$('#optionsList li.regular').hide();//
                    Home.showOptions('hide');
                 platformNavPane.find('div.assetTypes').hide();
                 if( AppData.isOneTouchOptions){//are there any OT options           
                          OneTouch.onTabClick();
                      }else //object not loaded, no OT options
                          PlatformNav.noTradesMsg();
                  break;
             }
             //trigger 'how to trade' in sidebar
             PlatformNav.container.find('div.leftSide div.howToTradeTabs ul li.'+newCategory+'Off').click();

             
            
        },
        onClickassetType: function(event){
            //relevant only for binary options
            if (PlatformNav.currentCategory != 'binaryOptions') return;

            var platformNavPane = PlatformNav.container.find('div#platformNav');
            var newAssetType = this.id.split("_",2)[1];//get the info we need from assetType_XXX format
            var oldAssetType = PlatformNav.currentAssetType;
            if (oldAssetType == newAssetType)
                    return

            PlatformNav.currentAssetType = newAssetType;

            //change images
            platformNavPane.find("div#assetType_"+oldAssetType).removeClass('selected');
            platformNavPane.find("div#assetType_"+newAssetType).addClass('selected');

			//change asset types - load options from specific
            Home.changeAssetType(newAssetType);
            
        },
        //functions for bottom bar
        onClickBottomBarTabs : function(event){
         var bottomBar = $("div#platformBottomBar");
         var oldItem = bottomBar.find("ul.tabs li.selected:eq(0)").attr('id').split("_",2)[1];
         var newItem = this.id.split("_",2)[1];
         if (oldItem == newItem) return

         bottomBar.find("ul.tabs li.selected:eq(0)").removeClass('selected');
         $(this).addClass('selected');
         //find the tab number and insert proper class for tabs image according to index number
         bottomBar.find('ul.tabs li').each(function(index){
             if(this.id.split("_",2)[1] == newItem ){
                 bottomBar.addClass('platformBottomBar_tab' + (index+1));

            }else if(this.id.split("_",2)[1] == oldItem )
                 bottomBar.removeClass('platformBottomBar_tab' + (index+1));
         })
         //hide other contents and make the right one visible
         bottomBar.find('div.displayPane div.'+ oldItem ).css('display','none');
         bottomBar.find('div.displayPane div.'+ newItem ).css('display','block');

        },
        onClickinOutBottomBar : function(event){
         var bottomBar = $("div#platformBottomBar");
         // PlatformNav.bottomBarIsOut keeps record of current state
         if(PlatformNav.toolbar.bottomBarIsOut){
             //pull in
              bottomBar.animate({right: -1033}, 'slow');
              PlatformNav.toolbar.bottomBarIsOut = false;
              bottomBar.find('div.inOutControl').attr('title','Click here to open');
         }else{
             //pull out
             bottomBar.animate({right: 0}, 'slow');
             PlatformNav.toolbar.bottomBarIsOut = true;
             bottomBar.find('div.inOutControl').attr('title','Click here to close');
         }
         //save selection to cookie
         document.cookie = "bottomBarIsOut =" + PlatformNav.toolbar.bottomBarIsOut ;
        },
        onClickTopBarLearn: function(){
            alert('Learn about ' + PlatformNav.currentCategory + ' on '+PlatformNav.platformMode +' trader')
        },
        noTradesMsg: function(action){
            if (action == 'hide')
             PlatformNav.container.find('div#noTradingMessage').addClass('hidden');
            else
             PlatformNav.container.find('div#noTradingMessage').removeClass('hidden');
        },
        onCloseSuggest: function(event){
		var saggestElem = $(event.target).parent().parent();

		saggestElem.animate({
				height:'0px'
            }, 1500, function(){$(this).css('display', 'none')});

	},

	onClosePromotion: function(event){
		var promotionElem = $(event.target).parent().parent();
		promotionElem.animate({
				height:'0px'
      	}, 1500 , function(){$("#homePromoBanner").remove();
                                Home.container.removeClass('promoBannerPresent');
                            });
               document.cookie = "promoBannerClosed = 1";
               // NOT RIGHT iOkun.createCookie('promoBannerClosed', '1');
	},
         onPlayDemoVideo: function(category){
          //plays only on home page and fast mode, otherwise redirect
          if (typeof category !== 'string')
              category = PlatformNav.currentCategory; //binaryOptions, optionsBuilder, oneTouch

        switch(category){
            case 'binaryOptions':
                if(PlatformNav.platformMode == 'fast'){
                    //problem, video made for pro, must redirect
                    General.redirect("home/pro", null, "autoPlayDemo=binaryOptions");
                    return; //dont continue function
                }
                var width = 760;
                var height = 600;
                var top = 50;
                var left = -72;
                break;
            case 'optionsBuilder':
                var width = 800;
                var height = 600;
                var top = 86;
                var left = -61;
                break;
            case 'oneTouch':
                if(PlatformNav.platformMode == 'pro'){
                    //problem, video made for fast, must redirect
                    General.redirect('home/fast', null, "autoPlayDemo=oneTouch")
                    return; //dont continue function
                }
                //just name sure its the right tab
                var width = 750;
                var height = 600;
                var top = 50;
                var left = -20;
                break;
        }
			

            //just name sure its the right tab
            $('#category_'+category).click();

          //languages other then english
          //if we dont have it, show english
          var languages = [,'ar', 'fr', 'de' , 'ru', 'tr'];
         if($.inArray(AppData.langId, languages) == -1){
             var videoLang = 'en';
         }else{
            var videoLang = AppData.langId;
         }

		var flashvars = false;

		var attributes = {};
		attributes.align = "middle";
		var params = {
			  wmode: "transparent",
			  loop: false,
			  menu: true
		}

                    $('<div/>').attr('id', 'demoVideo')
                       .html('<div id="demoVideoPlaceholder"></div>')
                        .appendTo('#platformNav');

         /*console.log("appProxy/demos/demoVideo_"+PlatformNav.currentCategory
                    +"_"+videoLang+".swf");*/
         $('ul#optionsList li').css('position', 'static');
         //scrollTo ul#optionlist so video is seen better
          $.scrollTo('#platformNav', 1000)
		 swfobject.embedSWF("appProxy/demos/demoVideo_"+PlatformNav.currentCategory
                    +"_"+videoLang+".swf",
                      "demoVideoPlaceholder", width, height, "9.0.0", "expressInstall.swf", flashvars, params, attributes);
                         
                  if(videoLang == 'ar')// arabic is RTL, turn in around
                      $('#demoVideo').css({top: top, right: left});
                  else
                      $('#demoVideo').css({top: top , left: left});
                  //$('#demoVideo').css('border','1px solid red');
         },
         demoVideoClose: function(){
            $('#demoVideo').remove();
            $('ul#optionsList li').css('position', 'relative');
        },
        /*
         * for the binary options demo video, this will simulate a click on
         * the PUT button of the first pro option box
         */
        demoAction_binaryOptionsClickPut: function(){
        	
            $('#Put_position_1').click();
        },
        handleAutoPlayDemo: function(){   
            var category = General.getURLParam('autoPlayDemo');
            if(! category) {alert('error');return}
          switch(category){
            case 'binaryOptions':
                //show a message
                General.jnotification({
            //TODO:message:  AppData.demoVideoIsLoading,
			message			 : 'Please wait, demo video is loading',
			msgType		     : 'success',
            time             :  10000
            });
                //delay to make sure its loaded
                setTimeout("PlatformNav.onPlayDemoVideo('binaryOptions')",10000);
                break;
            case 'optionsBuilder':
              ;
              break;
        case 'oneTouch':
            //delay need for options to load
                            //show a message
                General.jnotification({
            //TODO:message:  AppData.demoVideoIsLoading,
			message			 : 'Please wait, demo video is loading',
			msgType		     : 'success',
            time             :  4000
            });
            setTimeout("PlatformNav.onPlayDemoVideo('oneTouch')",400);
              break;
            }
        }
        
}
$(document).ready(function() {
	PlatformNav.init();
});




