Advertisement
Guest User

Untitled

a guest
Aug 13th, 2011
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Horizontal Accordion script
  2. * Created: Oct 27th, 2009. This notice must stay intact for usage
  3. * Author: Dynamic Drive at http://www.dynamicdrive.com/
  4. * Visit http://www.dynamicdrive.com/ for full source code
  5. */
  6.  
  7.  
  8. var haccordion={
  9.     //customize loading message if accordion markup is fetched via Ajax:
  10.     ajaxloadingmsg: '<div style="margin: 1em; font-weight: bold"><img src="ajaxloadr.gif" style="vertical-align: middle" /></div>',
  11.  
  12.     accordioninfo: {}, //class that holds config information of each haccordion instance
  13.  
  14.     expandli:function(accordionid, targetli){
  15.         var config=haccordion.accordioninfo[accordionid]
  16.         var $targetli=(typeof targetli=="number")? config.$targetlis.eq(targetli) : (typeof targetli=="string")? jQuery('#'+targetli) : jQuery(targetli)
  17.         if (typeof config.$lastexpanded!="undefined") //targetli may be an index, ID string, or DOM reference to LI
  18.             config.$lastexpanded.stop().animate({width:config.paneldimensions.peekw}, config.speed) //contract last opened content
  19.         $targetli.stop().animate({width:$targetli.data('hpaneloffsetw')}, config.speed) //expand current content
  20.         config.$lastexpanded=$targetli
  21.     },
  22.  
  23.  
  24.     urlparamselect:function(accordionid){
  25.         var result=window.location.search.match(new RegExp(accordionid+"=(\\d+)", "i")) //check for "?accordionid=index" in URL
  26.         if (result!=null)
  27.             result=parseInt(RegExp.$1)+"" //return value as string so 0 doesn't test for false
  28.         return result //returns null or index, where index is the desired selected hcontent index
  29.     },
  30.  
  31.     getCookie:function(Name){
  32.         var re=new RegExp(Name+"=[^;]+", "i") //construct RE to search for target name/value pair
  33.         if (document.cookie.match(re)) //if cookie found
  34.             return document.cookie.match(re)[0].split("=")[1] //return its value
  35.         return null
  36.     },
  37.  
  38.     setCookie:function(name, value){
  39.         document.cookie = name + "=" + value + "; path=/"
  40.     },
  41.  
  42.  
  43.     loadexternal:function($, config){ //function to fetch external page containing the entire accordion content markup
  44.         var $hcontainer=$('#'+config.ajaxsource.container).html(this.ajaxloadingmsg)
  45.         $.ajax({
  46.             url: config.ajaxsource.path, //path to external content
  47.             async: true,
  48.             error:function(ajaxrequest){
  49.                 $hcontainer.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
  50.             },
  51.             success:function(content){
  52.                 $hcontainer.html(content)
  53.                 haccordion.init($, config)
  54.             }
  55.         })
  56.     },
  57.  
  58.  
  59.     init:function($, config){
  60.             haccordion.accordioninfo[config.accordionid]=config //cache config info for this accordion
  61.             var $targetlis=$('#'+config.accordionid).find('ul:eq(0) > li') //find top level LIs
  62.             config.$targetlis=$targetlis
  63.             config.selectedli=config.selectedli || [] //set default selectedli option
  64.             config.speed=config.speed || "normal" //set default speed
  65.             $targetlis.each(function(i){
  66.                 $targetlis.each(function(i){
  67.     var $target=$(this).data('pos', i) //give each li an index #
  68.  
  69.     var lclMaxWidth     = maxWidth + $target.find ('.hpanel:eq(0)').outerWidth (true);
  70.     $target.css ('width', config.paneldimensions.fullw);
  71.  
  72.     //get offset width of each .hpanel DIV (config.dimensions.fullw + any DIV padding)
  73.     var hpaneloffsetw   = $target.find ('.hpanel:eq(0)').outerWidth (true);
  74.     if (hpaneloffsetw > lclMaxWidth)
  75.         hpaneloffsetw   = lclMaxWidth;
  76.  
  77.     $target.data('hpaneloffsetw', hpaneloffsetw);
  78.     $target.css ('width', '');
  79.                 /*if (config.collapsecurrent){ //if previous content should be contracted when expanding current
  80.                     $target.click(function(){
  81.                         $(this).stop().animate({width:config.paneldimensions.peekw}, config.speed) //contract previous content
  82.                     })
  83.                 }*/            
  84.             }) //end $targetlis.each
  85.             var selectedli=haccordion.urlparamselect(config.accordionid) || ((config.selectedli[1] && haccordion.getCookie(config.accordionid))? parseInt(haccordion.getCookie(config.accordionid)) : config.selectedli[0])
  86.             selectedli=parseInt(selectedli)
  87.             if (selectedli>=0 && selectedli<config.$targetlis.length){ //if selectedli index is within range
  88.                 config.$lastexpanded=$targetlis.eq(selectedli)
  89.                 config.$lastexpanded.css('width', config.$lastexpanded.data('hpaneloffsetw')) //expand selected li
  90.             }
  91.             $(window).bind('unload', function(){ //clean up and persist on page unload
  92.                 haccordion.uninit($, config)
  93.             }) //end window.onunload
  94.     },
  95.  
  96.     uninit:function($, config){
  97.         var $targetlis=config.$targetlis
  98.         var expandedliindex=-1 //index of expanded content to remember (-1 indicates non)
  99.         $targetlis.each(function(){
  100.             var $target=$(this)
  101.             $target.unbind()
  102.             if ($target.width()==$target.data('hpaneloffsetw'))
  103.                 expandedliindex=$target.data('pos')
  104.         })
  105.         if (config.selectedli[1]==true) //enable persistence?
  106.             haccordion.setCookie(config.accordionid, expandedliindex)
  107.     },
  108.  
  109.     setup:function(config){
  110.         //Use JS to write out CSS that sets up initial dimensions of each LI, for JS enabled browsers only
  111.         document.write('<style type="text/css">\n')
  112.         document.write('#'+config.accordionid+' li{width: '+config.paneldimensions.peekw+';\nheight: '+config.paneldimensions.h+';\n}\n')
  113.         document.write('#'+config.accordionid+' li .hpanel{width: '+config.paneldimensions.fullw+';\nheight: '+config.paneldimensions.h+';\n}\n')
  114.         document.write('<\/style>')
  115.         jQuery(document).ready(function($){ //on Dom load
  116.             if (config.ajaxsource) //if config.ajaxsource option defined
  117.                 haccordion.loadexternal($, config)
  118.             else
  119.                 haccordion.init($, config)
  120.         }) //end DOM load
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement