/* 
   Simple JQuery Accordion menu.
   HTML structure to use:

   <ul id="menu">
     <li><a href="#">Sub menu heading</a>
     <ul>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       ...
       ...
     </ul>
     <li><a href="#">Sub menu heading</a>
     <ul>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       ...
       ...
     </ul>
     ...
     ...
   </ul>

Copyright 2007 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free for non-commercial use
*/

/*
 * Function is used to set a cookie name and its value.
 */
 function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value + '; path=/';	
}

var str = location.pathname;
var path_supllied = str;
if( path_supllied == " " ){
       setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
}

/*
 * Function is used to read the featured page name from cookie.
 */
function readCookie( featured_page){ 
	var nameEQ = featured_page+ "="; 
	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; 
}

/*
 * Following code will set a cookie for index menu's Film link is clicked.
 */
 
$('ul li a').live('click', function() {
	setCookie('restore_index', 'yes', 1);
});


/*
 * Following code will set a cookie for index menu's module anchor.
 * Following code is used to display the open and closed icon.
 */

 $(document).ready(function(){
	$('#grt_dir_mod').click(function() {  
		setCookie('menu_anchor', 'grt_dir_mod', 1);	
	});
	$('#title_az_mod').click(function() {  
		setCookie('menu_anchor', 'title_az_mod', 1);
	});
	$('#by_director_mod').click(function() {  
		setCookie('menu_anchor', 'by_director_mod', 1);
	});
	$('#by_country_mod').click(function() {  
		setCookie('menu_anchor', 'by_country_mod', 1);
	});
	$('#by_subject_mod').click(function() {  
		setCookie('menu_anchor', 'by_subject_mod', 1);
	});
	$('#in_th_mod').click(function() {  
		setCookie('menu_anchor', 'in_th_mod', 1);
	});
	$('#dvd_blu_ray_mod').click(function() {	
		setCookie('menu_anchor', 'dvd_blu_ray_mod', 1);
	});
	$('#digital_mod').click(function() {  
		setCookie('menu_anchor', 'digital_mod', 1);
	});
	$('#non_th_mod').click(function() {  
		setCookie('menu_anchor', 'non_th_mod', 1);
	});	
}); 


/*
 * If Horizontal menu or footer link for horizontal menu is clicked then menu state flag will reset.
 */	
$(document).ready(function(){
	$('#reset_index_menu0').click(function() {
		setCookie('featured_page', 'home', 1);                
		setCookie('first_level', 'grt_dir_menu', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', null, 1);		//Unsetet the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
		setCookie('menu_anchor', 'grt_dir_mod', 1); // This will set the open or cloase status of the menu.                
	});	
	$('#reset_index_menu1').click(function() {	
		setCookie('featured_page', 'in_theater', 1);          
		setCookie('first_level', 'default_inth', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', 'in_th_np', 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.		
		setCookie('menu_anchor', 'in_th_mod', 1); // This will set the open or cloase status of the menu.                
	});
	$('#reset_index_menu2').click(function() {	
		setCookie('featured_page', 'dvd_blue_ray', 1);
		setCookie('first_level', 'default_dvd', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', null, 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
		setCookie('menu_anchor', 'dvd_blu_ray_mod', 1); // This will set the open or cloase status of the menu.
	});
	$('#reset_index_menu3').click(function() {	
		setCookie('featured_page', 'digital', 1);
		setCookie('first_level', 'default_digital', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', 'digital_nr', 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
		setCookie('menu_anchor', 'digital_mod', 1); // This will set the open or cloase status of the menu. 
	});
	$('#reset_index_menu4').click(function() {	
		setCookie('featured_page', 'non_theaters', 1);                
		setCookie('first_level', 'default_nonth', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', 'non_th_nr', 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
		setCookie('menu_anchor', 'non_th_mod', 1); // This will set the open or cloase status of the menu.                
	});
	$('#reset_index_menu5').click(function() {	
		setCookie('featured_page', 'in_theater', 1);          
		setCookie('first_level', 'default_inth', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', 'in_th_np', 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.		
		setCookie('menu_anchor', 'in_th_mod', 1); // This will set the open or cloase status of the menu.
	});
	$('#reset_index_menu6').click(function() {	
		setCookie('featured_page', 'dvd_blue_ray', 1);              
		setCookie('first_level', 'default_dvd', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', null, 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
		setCookie('menu_anchor', 'dvd_blu_ray_mod', 1); // This will set the open or cloase status of the menu.                
	});
	$('#reset_index_menu7').click(function() {	
		setCookie('featured_page', 'digital', 1);
		setCookie('first_level', 'default_digital', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', 'digital_nr', 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
		setCookie('menu_anchor', 'digital_mod', 1); // This will set the open or cloase status of the menu.                
	});
	$('#reset_index_menu8').click(function() {		
		setCookie('featured_page', 'non_theaters', 1);                
		setCookie('first_level', 'default_nonth', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', 'non_th_nr', 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
		setCookie('menu_anchor', 'non_th_mod', 1); // This will set the open or cloase status of the menu.
	});	
}); 

/*
 * Following code will initilize the index menu.
 */
function initMenu() {
  $('#menu ul').hide();
	var page_name = readCookie( 'featured_page');

	//Following code block is used to set the cookie varaible for the index menu level clicked.	
	var var_restore_index = readCookie( 'restore_index');
	var var_first_level = readCookie( 'first_level');
	var var_second_level = readCookie( 'second_level');
	var var_third_level = readCookie( 'third_level');
	var var_menu_anchor = readCookie( 'menu_anchor');
	
 // alert(var_restore_index);
 // alert(var_first_level);
 // alert(var_second_level);
 // alert(var_third_level);
				
if(var_restore_index == 'yes'){
	if(var_third_level != null){	
		//Following code block is used to display the selected menu block.
		// If menu is reloaded after selectign a film link.
		$("#menu").find('.'+var_first_level).show();
		$("#menu").find('#'+var_second_level).show();
		$("#menu").find('#'+var_third_level).show();
		
		$('#menu').find('.opened').addClass('closed');
		$('#menu').find('.opened').removeClass('opened');

		if ( $('#'+var_menu_anchor).hasClass('closed') ) {
			$('#'+var_menu_anchor).removeClass('closed');
			$('#'+var_menu_anchor).addClass('opened');
		}		
	}else {
		//Following code block is used to display the selected menu block.
		// If menu is reloaded after selectign a film link.	
		$("#menu").find('.'+var_first_level).show();
		$("#menu").find('#'+var_second_level).show();
		
		$('#menu').find('.opened').addClass('closed');
		$('#menu').find('.opened').removeClass('opened');

		if ( $('#'+var_menu_anchor).hasClass('closed') ) {
			$('#'+var_menu_anchor).removeClass('closed');
			$('#'+var_menu_anchor).addClass('opened');
		}
	}

}else{
  switch(page_name){
	case 'in_theater' :
		$("#menu").find('#in_th').show();
		$('ul.display_inth').show();
		
		setCookie('first_level', 'default_inth', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', 'in_th_np', 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.		
		setCookie('menu_anchor', 'in_th_mod', 1); // This will set the open or cloase status of the menu.
		
		if ( $('#in_th_mod').hasClass('closed') ) {
			$('#in_th_mod').removeClass('closed');
			$('#in_th_mod').addClass('opened');
		}		
		break;
	case 'dvd_blue_ray' :	
		$('#menu').find('.default_dvd').show();
		
		setCookie('first_level', 'default_dvd', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', null, 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
		setCookie('menu_anchor', 'dvd_blu_ray_mod', 1); // This will set the open or cloase status of the menu.
		
		if ( $('#dvd_blu_ray_mod').hasClass('closed') ) {
			$('#dvd_blu_ray_mod').removeClass('closed');
			$('#dvd_blu_ray_mod').addClass('opened');
		}		
		break;
	case 'digital' :	
		$("#menu").find('#digital_menu').show();
		$('ul.display_digital').show();
		
		setCookie('first_level', 'default_digital', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', 'digital_nr', 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
		setCookie('menu_anchor', 'digital_mod', 1); // This will set the open or cloase status of the menu.		
		
		if ( $('#digital_mod').hasClass('closed') ) {
			$('#digital_mod').removeClass('closed');
			$('#digital_mod').addClass('opened');
		}		
		break;
	case 'non_theaters' :	
		$("#menu").find('#non_th_menu').show();
		$('ul.display_nonth').show();
		
		setCookie('first_level', 'default_nonth', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', 'non_th_nr', 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
		setCookie('menu_anchor', 'non_th_mod', 1); // This will set the open or cloase status of the menu.		
		
		if ( $('#non_th_mod').hasClass('closed') ) {
			$('#non_th_mod').removeClass('closed');
			$('#non_th_mod').addClass('opened');
		}		
		break;
	case 'metro' :
		$("#menu").find('#metro_menu').show();
		if ( $('#metro_mod').hasClass('closed') ) {
			$('#metro_mod').removeClass('closed');
			$('#metro_mod').addClass('opened');
		}		
		break;
	default :	
		$("#menu").find('#grt_dir_menu').show();
		
		setCookie('first_level', 'grt_dir_menu', 1);		//Set the cookie value to restore the index menu.
		setCookie('second_level', null, 1);		//Unsetet the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
		setCookie('menu_anchor', 'grt_dir_mod', 1); // This will set the open or cloase status of the menu.		
		
		if ( $('#grt_dir_mod').hasClass('closed') ) {
			$('#grt_dir_mod').removeClass('closed');
			$('#grt_dir_mod').addClass('opened');
		}
		
  }
 } 
  
  $('#menu li a').click(
  
    function() {
      var checkElement = $(this).next();
      if((checkElement.hasClass('level_three')) && (checkElement.is(':visible'))) {
        checkElement.slideUp('normal');
          return false;
      }
      if((checkElement.hasClass('level_two')) && (checkElement.is(':visible'))) {	 	  
        checkElement.slideUp('normal');
          return false;
      }
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        checkElement.slideUp('normal');
		if ( $(this).hasClass('closed') ) {
			$(this).removeClass('closed');
			$(this).addClass('opened');
		} else {
			$(this).removeClass('opened');
			$(this).addClass('closed');
		}
        return false;
        }	  
      if((checkElement.hasClass('level_three'))) {	  	  
        $('#menu ul li ul li ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
		var id_third_level = checkElement.attr("id");
		setCookie('third_level', id_third_level, 1);		//Set the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
        return false;
      }	 
      if((checkElement.hasClass('level_two'))) {
        $('#menu ul li ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
		var id_second_level = checkElement.attr("id");
		setCookie('second_level', id_second_level, 1);		//Set the cookie value to restore the index menu.
		setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
		setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
        return false;
      }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('#menu ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
		var class_first_level = checkElement.attr("class");

		if(class_first_level == 'default_inth'){		
			setCookie('first_level', 'default_inth', 1);		//Set the cookie value to restore the index menu.
			setCookie('second_level', 'in_th_np', 1);		//Set the cookie value to restore the index menu.
			setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
			setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
			setCookie('menu_anchor', 'in_th_mod', 1); // This will set the open or cloase status of the menu.
		}else if(class_first_level == 'default_digital'){
			setCookie('first_level', 'default_digital', 1);		//Set the cookie value to restore the index menu.
			setCookie('second_level', 'digital_nr', 1);		//Set the cookie value to restore the index menu.
			setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
			setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
			setCookie('menu_anchor', 'digital_mod', 1); // This will set the open or cloase status of the menu.
		}else if(class_first_level == 'default_nonth'){
			setCookie('first_level', 'default_nonth', 1);		//Set the cookie value to restore the index menu.
			setCookie('second_level', 'non_th_nr', 1);		//Set the cookie value to restore the index menu.
			setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
			setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
			setCookie('menu_anchor', 'non_th_mod', 1); // This will set the open or cloase status of the menu.
		}else if(class_first_level == 'default_expand'){
			setCookie('first_level', 'default_expand', 1);		//Set the cookie value to restore the index menu.
			setCookie('second_level', null, 1);		//Set the cookie value to restore the index menu.
			setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
			setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
			setCookie('menu_anchor', 'title_az_mod', 1); // This will set the open or cloase status of the menu.
		}else if(class_first_level == 'by_subject'){
			setCookie('first_level', 'by_subject', 1);		//Set the cookie value to restore the index menu.
			setCookie('second_level', null, 1);		//Set the cookie value to restore the index menu.
			setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
			setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
			setCookie('menu_anchor', 'by_subject_mod', 1); // This will set the open or cloase status of the menu.
		}else if(class_first_level == 'by_director'){
			setCookie('first_level', 'by_director', 1);		//Set the cookie value to restore the index menu.
			setCookie('second_level', null, 1);		//Set the cookie value to restore the index menu.
			setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
			setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
			setCookie('menu_anchor', 'by_director_mod', 1); // This will set the open or cloase status of the menu.
		}else if(class_first_level == 'by_country'){
			setCookie('first_level', 'by_country', 1);		//Set the cookie value to restore the index menu.
			setCookie('second_level', null, 1);		//Set the cookie value to restore the index menu.
			setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
			setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
			setCookie('menu_anchor', 'by_country_mod', 1); // This will set the open or cloase status of the menu.
		}else if(class_first_level == 'default_dvd'){
			setCookie('first_level', 'default_dvd', 1);		//Set the cookie value to restore the index menu.
			setCookie('second_level', null, 1);		//Set the cookie value to restore the index menu.
			setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
			setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
			setCookie('menu_anchor', 'dvd_blu_ray_mod', 1); // This will set the open or cloase status of the menu.
		}else{		
			setCookie('first_level', class_first_level, 1);		//Set the cookie value to restore the index menu.
			setCookie('second_level', null, 1);		//Unsetet the cookie value to restore the index menu.
			setCookie('third_level', null, 1);		//Unset the cookie value to restore the index menu.
			setCookie('restore_index', null, 1); // This will unset the cookie variable for not restoring the menu.
			setCookie('menu_anchor', 'grt_dir_mod', 1); // This will set the open or cloase status of the menu.
		}		

		
		 if((checkElement.hasClass('default_digital'))) {
			$('ul.display_digital').show();
		 }
		 if((checkElement.hasClass('default_nonth'))) {
			$('ul.display_nonth').show();
		 }
		 if((checkElement.hasClass('default_inth'))) {
			$('ul.display_inth').show();
		 }		 
		$('#menu').find('.opened').addClass('closed');
		$('#menu').find('.opened').removeClass('opened');
		
		if ( $(this).hasClass('closed') ) {
			$(this).removeClass('closed');
			$(this).addClass('opened');
		} else {
			$(this).removeClass('opened');
			$(this).addClass('closed');
		}		 
        return false;
      }
      }
    );
  }
$(document).ready(function() {initMenu();});
