/***********************************************************************************************************************
DOCUMENT: includes/javascript.js
DEVELOPED BY: Ryan Stemkoski
COMPANY: Zipline Interactive
EMAIL: ryan@gozipline.com
PHONE: 509-321-2849
DATE: 3/26/2009
UPDATED: 3/25/2010
DESCRIPTION: This is the JavaScript required to create the accordion style menu.  Requires jQuery library
NOTE: Because of a bug in jQuery with IE8 we had to add an IE stylesheet hack to get the system to work in all browsers. I hate hacks but had no choice :(.
************************************************************************************************************************/
$(document).ready(function() {
	 
	//ACCORDION BUTTON ACTION (ON CLICK DO THE FOLLOWING)
	$('.accordionButton').each(function(i,item){
                $(this).click(function() {
                	
                    $("html,body").animate({"scrollTop":0},200);
                    //REMOVE THE ON CLASS FROM ALL BUTTONS


                    //NO MATTER WHAT WE CLOSE ALL OPEN SLIDES
                    $('.accordionContent').filter(function(){ return $(this).is(':hidden')==false;}).slideUp('slow',function(){

                        $(this).prev().removeClass('on');

                    });

                    //IF THE NEXT SLIDE WASN'T OPEN THEN OPEN IT
                    if($(this).next().is(':hidden') == true) {
                    	
                    	//ADD THE ON CLASS TO THE BUTTON
                        $(this).addClass('on');

                        //OPEN THE SLIDE
                        $(this).next().find("iframe").each(function(){

                            var iframe = $(this).clone();

                            iframe.hide().insertAfter($(this));

                            $(this).remove();

                            iframe.show();
                        })	
                    	
                    	
                    	var page = $("#pageType").val();
                    	if(page == 'news'){
                    		
                    		if(typeof section !== 'undefined'){
                    			var rel = section;	
                    			section = undefined;
                    		}	
                    		else
                    			var rel = $(this).attr('rel');
                    		
                    		var content_id = "content_"+rel; 
                    		var content_html = $("#"+content_id).html();
                    		// on class
                    		$("#content div").removeClass("on");
                    		$(".accordionButton[rel='"+rel+"']").addClass("on");
                    		    
                    		// load news if is empty
                    		if(content_html.length <= 1){                			
                    			$("#"+content_id).load("news/loadItem",{"year":rel},function(){                    				
                    				 $("#"+content_id).slideDown('slow');
                    			});
                    		}
                    		// we have content so let's display it
                    		else{
                    			$("#"+content_id).slideDown('slow');
                    		}
                    	}
                    	else if(page == 'exhibits'){
                    		var rel = $(this).attr('rel');
                    		var content_id = "content_"+rel;
                    		var content_html = $("#"+content_id).html();
                    		    
                    		// load news if is empty
                    		if(content_html.length <= 1){                			
                    			$("#"+content_id).load("exhibits/loadItem",{"rel":rel},function(){                    				
                    				 $("#"+content_id).slideDown('slow');
                    			});
                    		}
                    		// we have content so let's display it
                    		else{
                    			$("#"+content_id).slideDown('slow');
                    		}
                    	}
                    	// display slide as before for other components
                    	else{
                    		$(this).next().slideDown('slow');
                    	}
                    	
                     } 

                    Set_Cookie("accordion_state",i,365);

             });
         
      });
	  
	
	/*** REMOVE IF MOUSEOVER IS NOT REQUIRED ***/
	
	//ADDS THE .OVER CLASS FROM THE STYLESHEET ON MOUSEOVER 
	$('.accordionButton').mouseover(function() {
		$(this).addClass('over');
		
	//ON MOUSEOUT REMOVE THE OVER CLASS
	}).mouseout(function() {
		$(this).removeClass('over');										
	});
	
	/*** END REMOVE IF MOUSEOVER IS NOT REQUIRED ***/
	
	
	/********************************************************************************************************************
	CLOSES ALL S ON PAGE LOAD
	********************************************************************************************************************/	
	$('.accordionContent').hide();
	$('#open').show();

        var accordion_state = Get_Cookie ("accordion_state");
        if(accordion_state!=null && $(".accordionButton").eq(accordion_state).size()==1){
            
            $("#open").removeAttr("id");
            
            $(".accordionButton").eq(accordion_state).attr("id","open");
            
        }
        
	/********************************************************************************************************************
	OPENS THE DIV THAT IS ASSIGNED WITH THE ID open
	********************************************************************************************************************/	
	$("#open").trigger('click');

	
	// OPENME
	$("#content div").removeClass("on");
	$(".accordionContent").css("display","none");
	$(".openMe").addClass("on");
	$(".openMe").next().slideDown('slow');
	
});

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = 60 * 30 * 1000;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
