(function () { //Protect global namespace

   var readCookie = function (name) {
   	var nameEQ = name + "=";
   	var ca = document.cookie.split(';');
   	for(var i=0;i < ca.length;i++) {
   		var c = ca[i];
   		while (c.charAt(0)==' ') c = c.substring(1,c.length);
   		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   	}
   	return null;
   }
   
   var createCookie = function (name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


   ddaccordion.init({
   	headerclass: "expandable", //Shared CSS class name of headers group that are expandable
   	contentclass: "categoryitems", //Shared CSS class name of contents group
   	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
   	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
   	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
   	defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
   	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
   	animatedefault: false, //Should contents open by default be animated into view?
   	persiststate: false, //persist state of opened contents within browser session?
   	toggleclass: ["", "expanded"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
   	togglehtml: ["none", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
   	animatespeed: "normal", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
   	oninit:function(expandedindices){ //custom code to run when headers have initalized
   		//do nothing
		var dapage = window.location.href.split("/").pop();
		if(dapage != ""){
	  		$('.categoryitems a[href*='+ '/' + dapage +']').addClass('currentpage').parent().parent().prev().click().next().stop(true,true);
		}
   	},
   	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
   		//do nothing
   	}
   })
   // ddaccordion.init({
   //    revealtype: "click",
   //    collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
   //    defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content
   //    animatedefault: false, //Should contents open by default be animated into view?
   //    persiststate: true, //persist state of opened contents within browser session?
   //    toggleclass: ["", "openheader"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
   //    togglehtml: ["prefix", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
   //    animatespeed: "normal" //speed of animation: "fast", "normal", or "slow"
   // });
   // var expanded = readCookie('expandable');
   // $(document).ready(function () {ddaccordion.toggleone('expandable', expanded.substring(0, expanded.length-1 ));});


   
   $(document).ready( function () {
      $('ul.gallery').each(function () {
         $(this).find('a').colorbox({
         
         });
      });
	  
	  
	  // maintain highlight on selected link between pages:
	 /*
	  $('.arrowlistmenu a').click(function () {
		 createCookie('clickedpage',$('.arrowlistmenu a').index(this));
	  });
	  
	  var theanchor = parseInt(readCookie('clickedpage'),10);
	  if(theanchor >= 0){
			$('.arrowlistmenu a').eq(theanchor).addClass('currentpage');
	  }
	  */
	 
	  
	  
	  
      // For CSR rotation on product detail pages.
      $('.csr-box-email a').each(function () {
         var $this = $(this);
         var url = $this.attr('href');
         $this.attr('href', url.replace('contact.cfm', 'contact-ajax.cfm'));
      }).colorbox({
         iframe: true,
         fixedWidth: 900,
         fixedHeight: 570
      });
      
       var parseQuery = function (qstr) {
          var pairs = qstr.split('&');
          var qobject = {};
          for (var i = pairs.length - 1; i >= 0; i--){
             var name = pairs[i].split('=')[0];
             var value = pairs[i].split('=')[1];
             qobject[name] = value;
          };
          return qobject;
       }

       var serializeObject = function (qobj) {
          var qstr = ""
          for( i in qobj ) {
             qstr = [ qstr, i, '=', qobj[i], '&' ].join('');
          }
          return qstr
       };
      window.qstring = {};
      window.qstring.parse = parseQuery;
      window.qstring.serialize = serializeObject; 


      $(".csr-box .csr-box-reps").cycle({
         timeout: 6000,
         // cleartype: true,
         after: function () {
                  var name = escape($(this).find('.rep-name').text());
                  var href = $('.csr-box .csr-box-email a').attr('href');
                  var linkBase = href.split('?')[0];
                  var queryString = href.split('?')[1];
                  queryString = qstring.serialize($.extend(qstring.parse(queryString), {staff: name, part: $('.part-number').text() }));
                  var newHref = [linkBase, '?', queryString].join('');
                  $('.csr-box .csr-box-email a').attr('href', newHref);
          }
      });
      
      
      
   });

})();