var $ = jQuery.noConflict();

/**
 * Runs functions on document ready.
 */
$(document).ready(function(){
	slidingMenus_news(); // Call first so that first all of the news page tabs are hidden
	slidingMenus();
	gapi.plusone.go('google-plusone'); // Adds the google plus button
});

/**
 * Reloads the ajax content if the history.state is not null
 */
window.onpopstate = function(e){
	if(e.state !== null){
		run_load_updated_page(e.state.post_id, e.state.security, false)
	}
}

/** 
 * Show or hide the menus for staff and services every time a menu header is clicked.
 */
function slidingMenus(){	
	/** Hide all of the menus if the visible class is not present*/
	$('.JS_sliding_menu ul:not(.visible)').hide();
	
	/** Show the expanded class arrow on the tab that is active */
	$('.JS_sliding_menu ul.visible').siblings('h2').addClass('expanded_tab');
	
	/** Run on user click to show new active tab */
	$('.JS_sliding_menu h2').click(function(e){
		e.preventDefault();
		
		/** Remove all instances of the sliding_menu_bcg_active class */
		$(this).parents('.JS_sliding_menu').find('h2').removeClass('expanded_tab');
		
		/** Hides all of the ULs under this instance of .JS_sliding_menu */
		$(this).parents('.JS_sliding_menu').find('ul').slideUp(600);
		
		/**
		 * Checks if the UL is visible when clicked.
		 * Removes the active arrow if it is (as we are hiding it), or adds it if it is not (as we are now showing that one).
		 */
		if($(this).siblings('ul').is(':visible')){
			$(this).removeClass('expanded_tab');
		}else{
			$(this).addClass('expanded_tab');
		}
		
		/** Hides all of the ULs under .JS_sliding_menu */
		$(this).siblings('ul:not(:visible)').slideDown(600);
	});
}

/**
 * Hides all of the ULs under .JS_news_page_tab.
 * These need a seperate funcion as all tabs on the news page are independent of each other, unlike the staff and services tabs.
 */
function slidingMenus_news(){
	$('.JS_news_page_tab h2').click(function(e){
		e.preventDefault();
		$('.JS_news_page_tab').find('h2').removeClass('expanded_tab');
		$('.JS_news_page_tab').find('ul').slideUp(600);
	});	
}

/** 
 * Opens a window when a user clicks the 'Make an enquiry' button.
 */
$(function(){
    $('a.big-contact-link').click(function(e){
		e.preventDefault();
        window.open(this.href,'popup_contact','width=450, height=575, scroll=yes');
    });
});

/** 
 * Opens a window when a user clicks the 'Request a brochure' link.
 */
$(function(){
    $('a.request-a-brochure').click(function(e){
		e.preventDefault();
        window.open(this.href,'popup_brochure','width=450, height=465, scroll=yes');
    });
});

/** 
 * Opens a window when a user clicks the 'Email' link.
 */
$(function(){
    $('a.email-link').click(function(e){
		e.preventDefault();
        window.open(this.href,'popup_email_link','width=450, height=450, scroll=yes');
    });
});

/** 
 * Opens a window when a user clicks the 'Link' link.
 */
$(function(){
    $('a.show-link').click(function(e){
		e.preventDefault();
        window.open(this.href,'popup_link_window','width=450, height=175, scroll=yes');
    });
});

/**
 * Code for the Facebook 'Like' button (HTML code only - iFrame does not use this code
 */
(function(d, s, id){
	var js, fjs = d.getElementsByTagName(s)[0];
	if (d.getElementById(id)){
		return;
	}
	js = d.createElement(s); js.id = id;
	js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
	fjs.parentNode.insertBefore(js, fjs);
}
(document, 'script', 'facebook-jssdk'));

/**
 * Gets a query var from the URL
 *
 * @param required string query_var The query vat to Get
 * @return query[1] The selected query var (if it exists)
 */	
function $_GET(query_var, text, var_split, data_split){
	if(text === undefined){
		text = window.location.href;
	}
	query_string_location = text.search(/\?/);
	query_string = text.substring(query_string_location + 1);
	
	if(var_split === undefined){
		var_split = "&"
	}
	
	if(data_split === undefined){
		data_split = "="
	}
	
	query_strings = query_string.split(var_split);
	for (i=0; i<query_strings.length; i++){
		query = query_strings[i].split(data_split);
		if (query[0] === query_var){
			return query[1];
		}
	}
}
