﻿// JScript File

jQuery.fn.ChartInteraction = function(options){
 var config = {
     ProcessMethod: "ProcessMethods.aspx/ProcessTestGraph",
     ImageClass:"TestImage",
     ImageWidth:940,
     ImageHeight:295,
     ToolBarConfig: {
         PercentNumber:{
             Enabled:true,
             TopOffSet:0,
             RightOffSet:0,
             DefaultNdx:0,
             Items:{
                    Percent:{
                       Name:"Percent",
                       ImageOn:"Images/percent_on.gif",
                       ImageOff:"Images/percent.gif",
                       ClickHandlerParams:{ShowNumberData: false}
                    },
                    Number:{
                       Name:"Number",
                       ImageOn:"Images/number_on.gif",
                       ImageOff:"Images/number.gif",
                       ClickHandlerParams:{ShowNumberData: true}
                    }
                   }
         }
      }
 };
 
 jQuery.extend(true, config, options);
 
 
 var Container = $(this);
 
 Container.css({ 
         position:"relative",
         margin:0,
         padding:0      
 });
 
 var oImage = $(this).find("img." + config.ImageClass);
 
 if(oImage.width){
   jQuery.extend(config, {ImageWidth:oImage.width()});   
 }
 
 oImage.css({
             "z-index":"1"
            });
         
 Container.extend({
   //Need this to capture runtime attributes of image
   GetImageAttributes:function(){
       var imgRef = Container.find("img." + config.ImageClass).get(0);
       return {
                top:             $(imgRef).offset().top,
                left:            $(imgRef).offset().left,
                TestType:        ($(imgRef).attr("TestType") ? $(imgRef).attr("TestType") : "") ,
                TrendType:       ($(imgRef).attr("TrendType") ? $(imgRef).attr("TrendType") : "") ,
                Tuples:          ($(imgRef).attr("Tuples") ? $(imgRef).attr("Tuples") : "") ,
                ContextTypeVal:  ($(imgRef).attr("ContextTypeVal") ? $(imgRef).attr("ContextTypeVal") : ""),
                ContextID:       ($(imgRef).attr("ContextID") ? $(imgRef).attr("ContextID"): "" ),
                GraphTitle:      ($(imgRef).attr("GraphTitle") ? $(imgRef).attr("GraphTitle") : ""),
                GradeName:       ($(imgRef).attr("GradeName") ? $(imgRef).attr("GradeName") : ""),
                GradeCode:       ($(imgRef).attr("GradeCode") ? $(imgRef).attr("GradeCode") : ""),
                SubjectCode:     ($(imgRef).attr("SubjectCode") ? $(imgRef).attr("SubjectCode"): ""),
                SubjectName:     ($(imgRef).attr("SubjectName") ? $(imgRef).attr("SubjectName") : ""),
                SubGroupCode:    ($(imgRef).attr("SubGroupCode") ? $(imgRef).attr("SubGroupCode") : ""),
                SubGroupName:    ($(imgRef).attr("SubGroupName") ? $(imgRef).attr("SubGroupName") : ""),
                YearName:        ($(imgRef).attr("YearName") ? $(imgRef).attr("YearName") : ""),
                YearCode:        ($(imgRef).attr("YearCode") ? $(imgRef).attr("YearCode") : ""),
                CompareIDs:      ($(imgRef).attr("CompareIDs") ? $(imgRef).attr("CompareIDs") : ""),
                IsAdvanced:      ($(imgRef).attr("IsAdvanced") ? $(imgRef).attr("IsAdvanced") : ""),
                IsNumberData:    ($(imgRef).attr("IsNumberData") ? $(imgRef).attr("IsNumberData") : ""),
                IsClustered:     ($(imgRef).attr("IsClustered") ? $(imgRef).attr("IsClustered") : "")
             }
      },
     GetProcessParameters: function(){
     
        var imageAttributes = Container.GetImageAttributes();
        
        if(config.ProcessMethod.indexOf("ProcessTestGraph") != -1){
          return {
                     TestType:          imageAttributes.TestType,
                     ContextTypeVal:    imageAttributes.ContextTypeVal,
                     ContextID:         imageAttributes.ContextID,
                     GraphTitle:        imageAttributes.GraphTitle,
                     GradeName:         imageAttributes.GradeName,
                     GradeCode:         imageAttributes.GradeCode,
                     SubjectCode:       imageAttributes.SubjectCode,
                     SubjectName:       imageAttributes.SubjectName,
                     SubGroupCode:      imageAttributes.SubGroupCode,
                     SubGroupName:      imageAttributes.SubGroupName,
                     YearName:          imageAttributes.YearName,
                     YearCode:          imageAttributes.YearCode,
                     IsAdvanced:        imageAttributes.IsAdvanced,
                     ShowNumberData:    imageAttributes.IsNumberData,
                     ShowClusteredData: imageAttributes.IsClustered    
                 };
        }
       else if(config.ProcessMethod.indexOf("ProcessCompareTestGraph") != -1){
          return {
                     TestType:       imageAttributes.TestType,
                     ContextTypeVal: imageAttributes.ContextTypeVal,
                     ContextID:      imageAttributes.ContextID,
                     GraphTitle:     imageAttributes.GraphTitle,
                     GradeName:      imageAttributes.GradeName,
                     GradeCode:      imageAttributes.GradeCode,
                     SubjectCode:    imageAttributes.SubjectCode,
                     SubjectName:    imageAttributes.SubjectName,
                     SubGroupCode:   imageAttributes.SubGroupCode,
                     SubGroupName:   imageAttributes.SubGroupName,
                     YearName:       imageAttributes.YearName,
                     YearCode:       imageAttributes.YearCode,
                     CompareIDs:     imageAttributes.CompareIDs,
                     ShowNumberData: imageAttributes.IsNumberData          
                 };
       }
      else if(config.ProcessMethod.indexOf("ProcessCohortsGraph") != -1){
         return {
                     Tuples:         imageAttributes.Tuples,
                     ContextTypeVal: imageAttributes.ContextTypeVal,
                     ContextID:      imageAttributes.ContextID,
                     GraphTitle:     imageAttributes.GraphTitle,
                     SubGroupCode:   imageAttributes.SubGroupCode,
                     SubGroupName:   imageAttributes.SubGroupName,
                     ShowNumberData: imageAttributes.IsNumberData               
                };
       }
      else if(config.ProcessMethod.indexOf("ProcessTrendsGraph") != -1){
         return {
                     TrendType:      imageAttributes.TrendType,
                     ContextTypeVal: imageAttributes.ContextTypeVal,
                     ContextID:      imageAttributes.ContextID,
                     GraphTitle:     imageAttributes.GraphTitle,
                     GradeName:      imageAttributes.GradeName,
                     GradeCode:      imageAttributes.GradeCode,
                     SubjectCode:    imageAttributes.SubjectCode,
                     SubjectName:    imageAttributes.SubjectName,
                     SubGroupCode:   imageAttributes.SubGroupCode,
                     SubGroupName:   imageAttributes.SubGroupName,
                     YearName:       imageAttributes.YearName,
                     YearCode:       imageAttributes.YearCode,
                     ShowNumberData: imageAttributes.IsNumberData         
                };
       }
     }
   });
  
  var oImageAttributes = Container.GetImageAttributes();
  
  //config.ToolBarRightOffset = (config.ImageWidth + oImageAttributes.left - Container.offset().left) - (config.ImageWidth > 550 ? (160 + ((config.ImageWidth - 550)/10)) : 130) - config.ToolBarRightOffset  ;
  
 var adjustedToolBarRightOffset, adjustedToolBarTopOffset;
 var ToolBar;
 var Tab;
 var oClickHandler, oToolBar;
 
 //loop through the toolBars
 jQuery.each(config.ToolBarConfig, function(toolBarNdx){ 
      oToolBar = config.ToolBarConfig[toolBarNdx];
      
      if(oToolBar.Enabled){
              adjustedToolBarRightOffset = (config.ImageWidth + oImageAttributes.left - Container.offset().left);
              adjustedToolBarRightOffset = adjustedToolBarRightOffset - oToolBar.RightOffSet - (config.ImageWidth / 2);
              
              adjustedToolBarTopOffset =   (config.ImageHeight + oImage.offset().top - Container.offset().top) + oToolBar.TopOffSet;
           
              var ToolBar = jQuery("<div />").addClass("ToolBar")
                                             .css({
                                                    position:"absolute",
                                                    top:  adjustedToolBarTopOffset,
                                                    left: adjustedToolBarRightOffset,
                                                    "z-index": "2"
                                                 })
                                              .append(jQuery("<ul />"))
                                              .show();
             Container.append(ToolBar);
             
              ToolBar.extend({
                 FindToolBarItem: function(ItemName){
                     return $(this).find("ul:first").find("li[name=" + ItemName + "] :first");
                 },                 
                 AddToolBarItem: function(ItemName, StateImagePaths, ClickParams, ClickHandler, RollBackHandler){
                     if(ToolBar.FindToolBarItem(ItemName).length <= 0){
                        var ToolBarItem = jQuery("<li />").addClass("ToolBarItem")
                                                          .attr("name", ItemName)
                                                          .addClass((ToolBar.find("li").length == oToolBar.DefaultNdx ? "In" : "Out"))                                              
                                                          .append(jQuery("<img />").attr("src", (ToolBar.find("li").length == oToolBar.DefaultNdx ? StateImagePaths.In : StateImagePaths.Out))
                                                                                   .attr("alt",ItemName)
                                                                                   .attr("title",ItemName)); 
                                                           
                        ToolBarItem.click(function(){                            
                                  var oIcon = $(this).find("img:first");    
                                  var oItem = $(this);                                                          
                                  ClickHandler($(this).hasClass("In") ? true : false, 
                                  ClickParams, function(){     
                                          //Default Click Handling                                                                                                                                     //Default Click Handling                                                             
                                          if(oItem.hasClass("Out")){                              
                                                  oItem.parent().find("li[class*='In']").trigger("click");
                                                  oItem.removeClass("Out").addClass("In")
                                                  oIcon.attr("src", StateImagePaths.In);                              
                                          }
                                          else
                                          {
                                                oItem.removeClass("In").addClass("Out"); 
                                                oIcon.attr("src", StateImagePaths.Out);                          
                                          }      
                                     });
//                                  //Default Click Handling                                                             
//                                  if($(this).hasClass("Out")){                              
//                                          $(this).parent().find("li[class*='In']").trigger("click");
//                                          $(this).removeClass("Out").addClass("In")
//                                          oIcon.attr("src", StateImagePaths.In);                              
//                                  }
//                                  else
//                                  {
//                                        $(this).removeClass("In").addClass("Out"); 
//                                        oIcon.attr("src", StateImagePaths.Out);                          
//                                  }
                                });
                            
                                if(RollBackHandler){
                                    ToolBarItem.bind("RollBack", function(){
                                        RollBackHandler();
                                    });
                               }
                               $(this).find("ul:first").append(ToolBarItem);                                                                        
                     }
                 }
             });  
             
             //loop through the toolBar items     
             jQuery.each(oToolBar.Items, function(ItemNdx){        
                  Tab = config.ToolBarConfig[toolBarNdx].Items[ItemNdx];
                  oClickHandler = function(IsClicked, ClickParams, CallBack){  
                                     if(!IsClicked){                                                                               
                                                var Params = jQuery.extend(Container.GetProcessParameters(), ClickParams);                                                                                   
                                                $(this).ScriptLibrary().LoadContent(Container,
                                                                                     config.ProcessMethod,
                                                                                     JSON.stringify(Params),
                                                                                     function(objTarget, dataResponse){                                                                                                                             
                                                                                         if(dataResponse.Status[0].StatusCode){
                                                                                           var oImage = $(objTarget.find("img." + config.ImageClass).get(0));
                                                                                           var cloneObj = oImage.clone();                                                                                                                                   
                                                                                           var arDataResponse = dataResponse.Status[0].StatusMessage.split("#");                                                                   
                                                                                           var attributes;
                                                                                           cloneObj.attr("src", arDataResponse[0]);                                                                                  
                                                                                           if (arDataResponse.length > 1){                                                                       
                                                                                                cloneObj.css({
                                                                                                              height: arDataResponse[1] + "px"
                                                                                                             });
                                                                                              }
                                                                                          
                                                                                           if (arDataResponse.length > 2){
                                                                                              for(i = 2; i < arDataResponse.length; i ++){
                                                                                                   attributes = arDataResponse[i].split(":");
                                                                                                   if(attributes.length > 1){
                                                                                                     cloneObj.attr(attributes[0],attributes[1]);
                                                                                                   }
                                                                                                 }
                                                                                           }                                                                  
                                                                                           oImage.replaceWith(cloneObj);
                                                                                           
                                                                                           if(CallBack && $.isFunction(CallBack))
                                                                                                                  CallBack(); 
                                                                                         
                                                                                                                                                                 
                                                                                         }
                                                                                        else if(dataResponse.Status[0].StatusMessage.length > 0){
                                                                                           alert(dataResponse.Status[0].StatusMessage);
                                                                                        }
                                                                                        
                                                                                        
                                                                                       });
                                           }
                                        else if(CallBack && $.isFunction(CallBack))
                                             CallBack();
                 };
                 
                  
                ToolBar.AddToolBarItem(Tab.Name,{Out:Tab.ImageOff, In:Tab.ImageOn}, Tab.ClickHandlerParams,oClickHandler, function(){
                                                                                                      });        
             }) //End looping through the ToolBar Items              
             
      } //End Check for ToolBar Enabled     

 });//End looping through the Toolbars

 return this.each(function(){
 });
 
};

