//menu Accordion
//author: Marghoob Suleman
//Date: 05th Aug, 2009
//Version: 1.0
//web: www.giftlelo.com | www.marghoobsuleman.com
;(function($){
	$.fn.msAccordion = function(options) {
		options = $.extend({
					currentDiv:'1',
					previousDiv:'',
					vertical: false,
					defaultid:0,
					currentcounter:0,
					intervalid:0,
					autodelay:0,
					event:"click",
					alldivs_array:new Array()
			}, options);
		$(this).addClass("accordionWrapper");
		$(this).css({overflow:"hidden"});
		//alert(this);
		var elementid = $(this).attr("id");
		var allDivs = this.children();
		if(options.autodelay>0)  {
			$("#"+ elementid +" > div").bind("mouseenter", function(){
														   pause();
														   });
			$("#"+ elementid +" > div").bind("mouseleave", function(){
																  startPlay();
																  });
		}
		//set ids
		allDivs.each(function(current) {
								 var iCurrent = current;
								 var sTitleID = elementid+"_msTitle_"+(iCurrent);
								 var sContentID = sTitleID+"_msContent_"+(iCurrent);
								 var currentDiv = allDivs[iCurrent];
								 var totalChild = currentDiv.childNodes.length;
								 var titleDiv = $(currentDiv).find("div.title a");
								 titleDiv.attr("id", sTitleID);
								 var contentDiv = $(currentDiv).find("div.content");
								 contentDiv.attr("id", sContentID);
								 options.alldivs_array.push(sTitleID);
								 //$("#"+sTitleID).click(function(){openMe(sTitleID);});
								 $("#"+sTitleID).bind(options.event, function(){pause();openMe(sTitleID);});
								 });
		
		//make vertical
		if(options.vertical) {makeVertical();};
		//open default
		openMe(elementid+"_msTitle_"+options.defaultid);
		if(options.autodelay>0) {startPlay();};
		//alert(allDivs.length);
		function openMe(id) {
			var sTitleID = id;
			var iCurrent = sTitleID.split("_")[sTitleID.split("_").length-1];
			options.currentcounter = iCurrent;
			var sContentID = id+"_msContent_"+iCurrent;
			if($("#"+sContentID).css("display")=="none") {
				if(options.previousDiv!="") {
					closeMe(options.previousDiv);
				};
				if(options.vertical) {
					$("#"+sContentID).slideDown({duration: 900, easing: 'easeOutCubic'});
				} else {
					$("#"+sContentID).show(900);
				}
				options.currentDiv = sContentID;
				options.previousDiv = options.currentDiv;
			};
		};
		function closeMe(div) {
			if(options.vertical) {
				$("#"+div).slideUp({duration: 800, easing: 'easeOutCubic'});
			} else {
				$("#"+div).hide(800);
			};
		};	
		function makeVertical() {
			$("#"+elementid +" > div").css({display:"block", float:"none", clear:"both"});
			$("#"+elementid +" > div > div.title").css({display:"block", float:"none", clear:"both"});
			$("#"+elementid +" > div > div.content").css({clear:"both"});
		};
		function startPlay() {
			options.intervalid = window.setInterval(play, options.autodelay*1000);
		};
		function play() {
			var sTitleId = options.alldivs_array[options.currentcounter];
			openMe(sTitleId);
			options.currentcounter++;
			if(options.currentcounter==options.alldivs_array.length) options.currentcounter = 0;
		};
		function pause() {
			window.clearInterval(options.intervalid);
		}; 
		



/*
   ------------------------ jQuery General Navigation Functionality ------------------------
*/	

/* *** stop the slide show & fading phrases and set slide 21 as the background *** */
    $('a').click(function(){
       $('#all-phrases').hide();
       api.goTo(21);		
       if (!vars.is_paused){
       vars.is_paused = true; 
      }
	
    });
		

		
/* *** main navigation left-right hover animation *** */	
  $('a.main-nav').hover(function () {
    $(this).animate({opacity: 0.85,marginLeft: '2px'}, 250)
  }, 
  function () {
    $(this).animate({
                opacity: 1,
                marginLeft: '0px'
            }, 250)
  });
  
 /* *** sub navigation left-right hover animation *** */
    $('a.sub-nav').hover(function () {
      $(this).animate({opacity: 0.75,marginLeft: '2px'}, 250)
    }, 
    function () {
      $(this).animate({
                  opacity: 1,
                  marginLeft: '0px'
              }, 250)
    });    

/* *** photo gallery navigation active/inactive states *** */  
  $('a.photo-gallery-thumb').click(function(){
    if (!$(this).hasClass('photo-gallery-thumb')) {
      $('a.photo-active').removeClass('photo-active').addClass('photo-gallery-thumb');
      $(this).removeClass('photo-gallery-thumb').addClass('photo-active');
    }

  }); 	
	
	function set_mainnav(id) {
	  var div_id;
		$('a.main-active').removeClass('main-active').addClass('main-nav');
		$('div.title-active').removeClass('title-active').addClass('title');
		
		if(id == 'about-sola-salons')
		 {	
		   div_id = 0;
		 }	
		if(id == 'join-sola-salons')
		 {	
		   div_id = 1;
		 }	
		if(id == 'locate-sola-salons')
		 {	
		   div_id = 2;
		 }			 		 
		   $('#accordion3_msTitle_'+div_id).removeClass().addClass('main-active');
		   $('#accordion3_msTitle_'+div_id).parent('.title').removeClass('title').addClass('title-active');
		   var linktitle = $('#accordion3_msTitle_'+div_id).attr('title');
           $.address.title(linktitle + " | " + basetitle);

    }
	
	function set_openClosed(setid) {
		$('#sola-open').attr('id', 'sola-closed');
		$('div').hasClass('setid').attr('id', 'sola-open');
    }	
	
	function clear_mainnav() {
        $('a.main-active').removeClass('main-active').addClass('main-nav');
    }
	
	function set_subnav(id) {
        $('a.sub-active').removeClass('sub-active').addClass('sub-nav');
		if(id)
		 {
           $('#'+id).removeClass('sub-nav').addClass('sub-active');
		   var linktitle = $('#'+id).attr('title');
           $.address.title(linktitle + " | " + basetitle);
		 }
    }
	
	function clear_subnav() {
        $('a.sub-active').removeClass('sub-active').addClass('sub-nav');
		$('a.sub-active-white').removeClass().addClass('footer-nav');
    }
	
	function set_footernav(id) {
		$('a.sub-active-white').removeClass().addClass('footer-nav');
		if(id)
		 {
           $('#'+id).removeClass('footer-nav').addClass('sub-active-white');
		   var linktitle = $('#'+id).attr('title');
           $.address.title(linktitle + " | " + basetitle);		   
		 }
    }	
   function clear_footernav() {
		$('a.sub-active-white').removeClass().addClass('footer-nav');
    }
	function set_title(id) {
		if(id)
		 { 
		   linktitleid = '#'+id;
		   linktitle = $(linktitleid).attr('title');
           $.address.title(linktitle + " | " + basetitle);
		   //alert("Linktitle = " + linktitle);
		 }
    }
	
	function set_title_byname(myname) {
	
           $.address.title(myname + " | " + basetitle);
    }
	
	function madeit(section) {
	   /* this is a test function to determine variable scope. ok to delete */
       alert(section + ' ' + 'Made it!');
         }

	function set_tabstate(mytab) {
	   //alert(mytab);
	    if(mytab == '1')
		 {
		  $('div#content1').removeClass('hide');
		  $('div#content2').removeClass().addClass('tab-content').addClass('hide');
		  $('li#tab1').removeClass().addClass('option').addClass('active');
		  $('li#tab2').removeClass().addClass('option')  		  		  		  
		 }
		else if(mytab == '2')
		 {
		  $('div#content2').removeClass('hide');
		  $('div#content1').removeClass().addClass('tab-content').addClass('hide');
		  $('li#tab2').removeClass().addClass('option').addClass('active');
		  $('li#tab1').removeClass().addClass('option')	  	 
		 }
    }


/*
   ------------------------ jQuery Address Functionality ------------------------
*/

$("document").ready(function(){

  // alert($.address.value());
  // alert($.address.history());
  // alert($.address.path());
  // alert($.address.parameter(name));
  // alert($.address.path(value));
  // alert($.address.pathNames());
  // alert($.address.queryString());
  // alert($.address.state());
  // alert($.address.strict());
  // alert($.address.hash());
  // alert($.address.value());
  // alert($.address.autoUpdate());
  // alert($.address.baseURL());

  /*$('a').address();*/
  $("a:not(a#noaddress)").address();
  $('div.set').address();

  basetitle = "Sola Salon Studios";

  $('a.main-nav').click(function(){
    $('.shortLContent').css('width', '225px');
    $('.shortRContent').css('width', '775px');
	clear_footernav();
    $('.stylist-form').fadeIn(200);						    
    /*$('#demoContainer-store').mb_changeContainerContent2('map-main-level/');*/ 	
  });

  $('a.footer-nav').click(function(){
     openMe("accordion3_msTitle_2");		
  });
  
  myContainerId = $('#demoContainer-store div.mbcontainercontent').attr('id');
  //alert('My Id = ' + myContainerId);
  


/*  jQuery Address on Change "recording" functions  */
$.address.change(function(event) {
  
  path = event.value.substr (1);
  paths = path.split ('/');
  sect0 = paths[0];
  sect1 = paths[1];
  sect2 = paths[2];
  sect3 = paths[3];
  sect4 = paths[4];
  sect5 = paths[5];
  sect6 = paths[6];
  sect7 = paths[7];
  sect8 = paths[8];
  sect9 = paths[9];
  
  if(sect1)
   {
    api.goTo(21);
    $('#all-phrases').fadeOut();
    vars.is_paused = true; 
    api.options.slideshow = false;
   } 	
								
  if ( paths[1] && paths[1].length < 1 ) 	paths[1] = undefined;
  path_len = paths.length;
  /*alert('path =' + ' ' + path + ' ' + 'sect0 =' +  ' ' + sect0 + ' ' + 'sect1 =' +  ' ' + sect1 + ' ' + 'sect2 =' +  ' ' + sect2 + ' ' + 'sect3 =' +  ' ' + sect3 + ' ' + 'sect4 =' +  ' ' + sect4);*/
				
  //alert($.address.value());

if ( $.address.value() ==  '/home') {
    window.location = "http://www.solasalonstudios.com/";
}	
			
	$('a').click(function() {
        var linktitle = $(this).attr('title');
        $.address.title(linktitle + " | " + basetitle);
    });
	
 /* About Section */
                       if (sect0 == 'about-sola-salons') 
                          {   
							  set_mainnav(sect0);
							  
                              $('#demoContainer-store div.mbcontainercontent').attr('id','about-store');
							  if(sect1)
							     {
							       $('#demoContainer-about').mb_changeContainerContent(sect1);
								   set_subnav(sect1)
							     }
							  else
								 { 
								   clear_subnav(); 
								   $('#demoContainer-about').mb_changeContainerContent(sect0);
								 }
                          }// close if sect0 = about-sola-salons

 /* Join Us Section */ 
					   else if (sect0 == 'join-sola-salons') 
                          {
						     set_mainnav(sect0);
							 
							 $('#demoContainer-store div.mbcontainercontent').attr('id','join-store');
                             if(sect1)
							   {   
							     if (!sect2)
								   {  
							         //madeit('Section 2 Join Sola Salons');
							         $('#demoContainer-join').mb_changeContainerContent2(sect1);
								     set_subnav(sect1);
								   }
								 if (sect2)
								   {
								    if(sect1 == 'salon-testimonials')
		                             { 
		                              $('#demoContainer-testimonials').mb_changeContainerContent2('container-testimonial/' + sect2 + '/' + sect3 + '/');
									  /*$('#demoContainer-testimonials-text').mb_changeContainerContent2('container-testimonial-text/' + sect2 + '/' + sect3 + '/');*/ 
									  set_title(sect3);
		                             }
									if(sect1 == 'salon-tours')
		                             {
									  if(sect2 == 'photo-gallery' && sect3)
									     { 
		                                  $('#demoContainer-gallery').mb_changeContainerContent2('container-gallery-image/' + sect3 + '/');
									      set_title(sect4);
		                                 }
									  if(sect2 == 'photo-gallery' && !sect3)
										 { 
										  set_tabstate('1');
										  set_title_byname('Photo Gallery');
										 }
		                               if(sect2 == 'video-gallery' && sect3)
		                                 {  
		                                  $('#demoContainer-video').mb_changeContainerContent2('container-video-player/' + sect2 + '/' + sect3 + '/' + sect4 + '/'); 
									      set_title(sect4);
		                                 }
									   if(sect2 == 'video-gallery' && !sect3)
										 { 
										  set_tabstate('2');
										  set_title_byname('Video Gallery');
										 }
									 } // close if salon-tours
								   } // close if sect2
							    }// close if sect1
								
  					         else if(sect0 == 'join-sola-salons' && !sect1)
						        { 
							     clear_subnav(); 
							     $('#demoContainer-join').mb_changeContainerContent(sect0);
						        }
						  }// close if sect0 = join-sola-salons	  
						  							  
 /* Locate Us Section */
					       
						else if (sect0 == 'locate-sola-salons') 
                           { 
						   	    set_mainnav(sect0);
								
								$('#demoContainer-store div.mbcontainercontent').attr('id','store-mbcontainer');
								$('.shortLContent').css('width', '225px');			
                                $('.shortRContent').css('width', '775px');
								$('.stylist-form').fadeIn(200);
								$('#demoContainer-store').show(300);
								
						      if(!sect1)
								  {
							        $('#demoContainer-locate').mb_changeContainerContent2(sect0);
							        $('#demoContainer-store').mb_changeContainerContent2('map-main-level/',$('#map_canvas').gmap('refresh'));
								  }
							  else if(sect1)
							      {  
                                   if (sect1 == 'map-state')
                                    {
                                      //var locate_linktitle = $('#demoContainer-locate div.mbcontainercontent a#'+sect2).html();
                                      //$.address.title(locate_linktitle + " | " + basetitle);
									  
									  /*
									  -- sect2 is the state name - required to draw the right map
									  -- sect3 is the state id - required to set the list of locations on the left
									  */
                                      $('#demoContainer-store').mb_changeContainerContent2('map-state/' + sect2, $('#demoContainer-locate').mb_changeContainerContent2('city-list/' + sect3));
                                    }

                                   if (sect1 == 'metro')
                                     {  
                                      set_subnav(sect2);
									  /*
									  -- sect3 is the metro cat id - required to display the correct content
									  -- sect3 is the state id - required to draw the right map
									  */
                                      $('#demoContainer-store').mb_changeContainerContent2(sect1 + '/' + sect2 + '/' + sect3 + '/' + sect4 + '/'); 
									 }
									
								   if (sect1 == 'city')
                                     {
                                      set_subnav(sect2);
									  set_tabstate('1');
									  /*set_title('storetab');*/
                                      $('#demoContainer-store').mb_changeContainerContent2('city/' + sect3);
									  //alert($('#demoContainer-store div.mbcontainercontent').attr('id'));
                                     }
								   if (sect1 == 'store')
                                     {      
                                      set_subnav(sect1);
									  set_tabstate('1');
									  set_title('storetab');
                                      $('#demoContainer-store').mb_changeContainerContent2('city/' + sect3);
                                     }
                                   if (sect1 == 'directory' || sect1 == 'stylistdirectory')
                                     {  
                                      set_tabstate('2');
									  set_title('directorytab');
									  //alert($('#directorytab').attr('title'));
                                      $('#demoContainer-stylist').mb_changeContainerContent2('container-stylist/' + sect3);
                                     } 								   
								   if (sect1 == 'stylist')
                                     {
									  set_tabstate('2');
                                      $('#demoContainer-stylist').mb_changeContainerContent2('container-stylist-details/'  + sect2 + '/' + sect3 + '/' + sect4 + '/' + sect5 + '/');
									  set_title(sect2);    
                                      //set_subnav(sect2);
									  //set_tabstate('content2');
                                     } 									 
								   if(sect1 == 'search-results')
                                     {
                                      $('#demoContainer-store').mb_changeContainerContent2('container-locate-stylist/'  + sect5 + '/' + sect3 + '/' + sect4 + '/');
                                     }
								   if(sect1 == 'search-stylists')
                                     {  
									  set_title_byname('Search Results'); 
                                     }									 									 

    							} // close else if sect1

                          } // close if sect0 = locate-sola-salons 

 /* Footer Section */							 
						else if (sect0 == 'contact-sola-salons') 
                          {
						   	   $('.stylist-form').hide();
							   set_mainnav('locate-sola-salons');
							   set_footernav(sect0);
                               $('.shortLContent').css('width', '570px');			
                               $('.shortRContent').css('width', '430px');
							
                                 $('#demoContainer-locate').delay(300).fadeIn(300).mb_changeContainerContent2('form-contact-main/');
								 $('#demoContainer-locate div.mbcontainercontent').attr('id','contact-locate');
								 $('#demoContainer-store div.mbcontainercontent').attr('id','contact-store');
  

								 $('#demoContainer-store').mb_changeContainerContent2('category-city-phonebook/'+sect2);
								 


						   }
						 else if (sect0 == 'sola-salons-franchise-opportunity') 
                           {
						   	   $('.stylist-form').hide();
							   set_mainnav('locate-sola-salons');
							   set_footernav(sect0);
                               $('.shortLContent').css('width', '570px');			
                               $('.shortRContent').css('width', '430px');							    
                               $('#demoContainer-locate').mb_changeContainerContent2('container-franchise/');
							   $('#demoContainer-store').fadeOut(); 
                            } 
						   

            });			
       
 /*  Browser Back/Forward/Cut/Paste Functionality  */
 $.address.externalChange(function(event) {
 

if ( $.address.value() ==  '/#/home' )
  {
    window.location = "http://www.solasalonstudios.com/";
   }

if (sect0)
  {	
        api.goTo(21);
        $('#all-phrases').fadeOut();
        vars.is_paused = true; 
        api.options.slideshow = false;
  }
  			 
 /* About Section */
                        if (sect0 == 'about-sola-salons') 
                         {
                              openMe("accordion3_msTitle_0");
                              set_mainnav(sect0);
							  $('#demoContainer-store div.mbcontainercontent').attr('id','about-mbcontainer');
							  
                         }
									  
/* Join Section */
                       else if (sect0 == 'join-sola-salons') 
                        {
                              openMe("accordion3_msTitle_1");
                              set_mainnav(sect0);
							  $('#demoContainer-store div.mbcontainercontent').attr('id','join-mbcontainer');

									  if(sect2 == 'photo-gallery')
										 { 
										  set_tabstate('1');
										 }
									   if(sect2 == 'video-gallery')
										 { 
										  set_tabstate('2');
										 }
						}
							  
							 
                                 
 /* Locate Section */
                       else if (sect0 == 'locate-sola-salons') 
                        {
                              openMe("accordion3_msTitle_2");
							  set_mainnav(sect0);  
							  $('#contact-locate').hide();
							  $('#contact-store').hide();
                              if(!sect1)
								  {
							        $('#demoContainer-locate').mb_changeContainerContent2(sect0);
							        $('#demoContainer-store').mb_changeContainerContent2('map-main-level/',$('#map_canvas').gmap('refresh'));
								  }
							 
						  if(sect1 == 'stylist')
						    { 
							  //madeit("Yes, store-mbc");
							  set_tabstate('2');
							  $('#demoContainer-store').mb_changeContainerContent2('city-direct/C' + sect4 + '/'  + sect2 + '/' + sect3 + '/' + sect5 + '/');
							  $('#demoContainer-stylist').mb_changeContainerContent2('container-stylist-details-direct/'  + sect2 + '/' + sect3 + '/' + sect4 + '/' + sect5 + '/');
                              							  
							  var cityListId = $('#cityList').html();
							  if(cityListId == null)
							   {  
							    $('#demoContainer-locate').mb_changeContainerContent2('city-list-2/'  + sect4 + '/');
								
							   } 
							   else 
							    { 
								  var compareTitle = 'state' + sect4;
								  var cityListTitle = $('#cityList').attr('title');
								  if(compareTitle != cityListTitle)
								   {
								    $('#demoContainer-locate').mb_changeContainerContent2('city-list-2/'  + sect4 + '/');
								   }
								 }
							}
							
                          if (sect1 == 'directory' || sect1 == 'stylistdirectory')
                            {  
                              set_tabstate('2');
							  //set_subnav(sect2);
							  set_title('storetab');
                              $('#demoContainer-stylist').mb_changeContainerContent2('container-stylist/' + sect3);
                            } 
						}
						
 /* Footer Section */						
					   else if (sect0 == 'contact-sola-salons') 
                        {
                              openMe("accordion3_msTitle_2");
							  set_mainnav('locate-sola-salons');  
						}
					   else if (sect0 == 'sola-salons-franchise-opportunity') 
                        {
                              openMe("accordion3_msTitle_2");
							  set_mainnav('locate-sola-salons');
						}


                });
          });
	
  }
})(jQuery);
