// Intercepting document write
//document.write = function(txt) { window.outputBuffer = txt; };

//console.log(window.outputBuffer);
	
//(function(){

//var script = document.createElement("script");
//script.type = "text/javascript";
//script.src = "http://maps.google.com/maps/api/js?sensor=true&region=AU";
//document.body.appendChild(script);

	window.onload = function(){



					
	}
	


window.addEvent('domready', function(e){


//			if(((document.body).getElements('input[name=latitude]') ).length == 0 && ((document.body).getElements('input[name=longtitude]') ).length == 0 )

//			var myTips = new Tips('.thisisatooltip');


			var gSF = document.getElementById('global-search'); // Global Search form
			var gSI = document.getElementById('global-search-field');			
			var gSO = document.getElementById('type');
			var gDDV = document.getElementById('global-search-drop-down-value');
			
		  	gDDV.value = (gSO.options[gSO.options.selectedIndex]).value;
 			gDDV.value = (gSO.options[gSO.options.selectedIndex]).innerHTML;
			
			
			//Change search field value depending on drop down onload
			if(gSI.value =='Enter keywords'){
				if(gDDV.value == 'Clubs' || gDDV.value == 'Coaches' || gDDV.value == 'Partners'){
					gSI.value = 'Enter postcode or suburb';
				}
				else if(gDDV.value == 'Players'){
					gSI.value = 'Enter player name';
				}
				else{
					gSI.value = 'Enter keywords';
				}
			}
			

			
			
			// Global Search - Updating underlaying input value from Select element upon user's selection
			gSO.onchange=function(){
			
				//Change form action for the form to point to the main site if searching from a sub site
				if(gSF.hasClass('sub-site-search')){
					if(this.selectedIndex > 0){
						gSF.action = '/';
					}
					else{
						gSF.action = 'http://'+window.location.hostname+window.location.pathname;
					}
				}
				
				
				
				
				
				
		 	   	gDDV.value = (this.options[this.selectedIndex]).value;
//				if(gDDV =='News' || gDDV =='Photos' || gDDV =='Video' || gDDV =='Players' || gDDV =='tennis.com.au' || gDDV =='Tournaments' || gDDV =='Clubs' || gDDV =='Coaches' ){
				if(gDDV.value =='Clubs' || gDDV.value =='Coaches' ){
					gSI.value = 'Enter postcode or suburb';
					// Get Geolocation of user if browser supports
					/*
					if (navigator.geolocation){
					
						navigator.geolocation.getCurrentPosition(
							// IF PERMITTED BY THE USER AND SUCCESSFULLY OBTAINED USER'S LOCATION
							function(position){
								//position.coords.latitude+','+position.coords.longitude
								
								if(((document.body).getElements('input[name=latitude]') ).length == 0 && ((document.body).getElements('input[name=longitude]') ).length == 0 ){
								
									var usersLat = new Element('input', {
										name: 'latitude',
									    type: 'hidden',
									    value: position.coords.latitude
									}).injectBefore(gDDV,'after');
									
									var usersLng = new Element('input', {
										name: 'longitude',
									    type: 'hidden',
									    value: position.coords.longitude
									}).injectBefore(gDDV,'after');
								}

							},
							function(error){
								console.log('FAILED');
								gSI.value = 'Enter postcode';
							},
							// Parameters
							{
								timeout: 30000,
								maximumAge: (1000 * 60 * 15),
								enableHighAccuracy: true
							}
						);

						
					}
					else{
						gSI.value = 'Enter postcode';
					}
					*/
					

					
				}
				else if(gDDV.value == 'Players'){
					gSI.value = 'Enter player name';
				}
				else{
					gSI.value = 'Enter keywords';
				}		
			}
			
            gSI.onfocus=function(){
                if(this.value == 'Enter keywords' || this.value == 'Enter postcode or suburb' || this.value == 'Enter player name'){
                    this.value ='';
                }
            }
            gSI.onblur=function(){
                if(this.value == ''){
                    if(gDDV.value == 'Clubs' || gDDV.value == 'Coaches' || gDDV.value == 'Partners' ){
                        this.value = 'Enter postcode or suburb';
                    }
                    else if(gDDV.value == 'Players'){
                        this.value = 'Enter player name';
                    }       
                    else{
                        this.value ='Enter keywords';
                    }
                }
			}
			
			//Enabled drop down options on sub sites if javascript is enabled onload
			if(document.id('global-search').hasClass('sub-site-search')){
				var disabled_options = document.id('type').getElements('option[disabled=disabled]');
				disabled_options.each(function(e){
					e.removeProperty('disabled');
				});			
			}
			
// GLOBAL VARIABLES & OBJECTS


	if(document.id('home')){
	// Instantiate class if user is on the homepage

		/* NEWS CAROUSEL */
		/* REF: http://mootools.net/forge/p/tab */
		(function(){
			var news_selected_index;
			var news_tab = new Tab.Extra({
					container: 'homepage-news-carousel',
					selector: 'li', 
					tabs: 'ul.homepage-featured-news-buttons li a',
					activeClass: 'selected',
					animation: 'Move',
					interval: 5, 
					params: {
						circular: true,
						useOpacity: false,
						opacity: 0.7
					},
					fx: {
						link: 'ignore',
						duration: 300
					},
					onCreate: function(newPanel, index){
						// When tabs had been created....
						// Grab all image src and then bind
//						var myImages = Asset.images(['/images/myImage.png', '/images/myImage2.gif'], {
//						    properties: {
//						        'class': 'myImage',
//						        title: 'myImage'
//						    },
//						    onComplete: function(){
//						        alert('All images loaded!');
//						    }
//						});
						//console.log(index);
					},
					onChange: function(a,b,c,d){
						news_selected_index = c;
					}
				});
				
			$$('ul.homepage-featured-news-buttons li a').each(function (el) {
				el.addEvent('mouseenter', function () { 
					news_tab.stop(); 
				});
				
				el.addEvent('mouseleave', function () { 
					news_tab.start.delay(2 * 150); 
				});
			});	
		})();
		/* END - NEWS CAROUSEL */
				
		/* NEWS TICKER */
		(function(){
			var news_ticker = new Tab.Extra({
					container: 'homepage-news-ticker',
					selector: 'li', 
					animation: 'Move',
					interval: 2.5, 
					params: {
						circular: true,
						mode: 'verticle'
					},
					fx: {
						link: 'ignore',
						duration: 1000
					}
				});
				
			$$('ul#homepage-news-ticker li').each(function (el) {
				el.addEvent('mouseenter', function () { 
					news_ticker.stop(); 
				});
				
				el.addEvent('mouseleave', function () { 
					news_ticker.start(); 
				});
			});
				
			document.id('more-news-next').addEvents({
				'click': function (e) {
					e.preventDefault();
					news_ticker.next(); 
				},
				'mouseenter': function (e) {
					news_ticker.stop(); 
				},
				'mouseleave': function (e) {
					news_ticker.start();
				}
			});
			
			document.id('more-news-previous').addEvents({
				'click': function (e) {
					e.preventDefault();
					news_ticker.previous(); 
				},
				'mouseenter': function (e) {
					news_ticker.stop(); 
				},
				'mouseleave': function (e) {
					news_ticker.start();
				}
			});
						
		})();
		/* END - NEWS TICKER */
	}
	
	// GENERAL CONTENTS PANEL 
	/*
		USAGE:
		<div class="ticker">
			<ul class="ticker-contents-container">
				<li>ITEM 1</li>
				<li>ITEM 2</li>
				<li>ITEM 3</li>
			</ul>
			<!-- [OPTIONAL]: MUST BE EQUAL TO NUMBER OF CONTENT ITEMS -->
			<ul class="ticker-tabs">
				<li><a href="#">TAB 1</a></li>
				<li><a href="#">TAB 2</a></li>
				<li><a href="#">TAB 3</a></li>
			</ul>
			<!-- /[OPTIONAL] -->
			<a class="ticker-btn-previous">PREVIOUS</a>
			<a class="ticker-btn-next">NEXT</a>
		</div>
	*/ 
	$$('.ticker').each(function(el){
		// Search & collect contents container
		var ticker_contents = el.getElements('.ticker-contents-container');
		// Search & collect
		var ticker_tabs = el.getElements('.ticker-tabs');
		// Search & collect buttons
		var next_btn = el.getElements('.ticker-btn-next');
		var previous_btn = el.getElements('.ticker-btn-previous');

		
		// Check if required elements exists and apply Fx functions
		if(ticker_contents.length == 1 && next_btn.length > 0 && previous_btn.length > 0){
			var ticker = new Tab.Extra({
					container: ticker_contents[0],
					selector: 'li', 
					animation: 'Move',
					tabs: ((ticker_tabs.length != 0 ) ? '.ticker-tabs li a' : '' ),
					interval: 5, 
					params: {
						circular: true,
						mode: 'horizontal'
					},
					fx: {
						link: 'ignore',
						duration: 600
					}
				}).stop();
		
			next_btn.each(function(n_btn){
				n_btn.addEvents({
					'click': function (e) {
						e.preventDefault();
						ticker.next(); 
					},
					'mouseenter': function (e) {
						//ticker.stop(); 
					},
					'mouseleave': function (e) {
						//ticker.start();
					}
				});
			});

			previous_btn.each(function(p_btn){
				p_btn.addEvents({
					'click': function (e) {
						e.preventDefault();
						ticker.previous(); 
					},
					'mouseenter': function (e) {
						//ticker.stop(); 
					},
					'mouseleave': function (e) {
						//ticker.start();
					}
				});
			});
		}
	});




	
	
	
	//This code will send a data object via a GET request
	// If found variable 'ajax' on page initiate create object
	if( typeof( window[ 'site_ajax' ] ) != "undefined"  ){
		var player_json_request = new Request.JSON({
			url: site_ajax.ajaxurl, 
			noCache: true, // Force browser not to cache results...
			onSuccess: function(posts,text){
			console.log(posts);
/*				posts.each(function(post,index){
					console.log(post);
	//				console.log(index+": "+post.ID);
	//				console.log(index+": "+post.post_title);
	//				console.log(index+": "+post.post_excerpt);
	//				console.log(index+": "+post.post_title);
				});
*/				
				// Update current page title and other elements an initiate GA tracking for ajax calls...
				document.title =  'AJAX success - ' + document.title;
	
			}
		});
		
	 //UNCOMMENT AND CHECK BROWSER CONSOLE TO SEE RESPONSES
/*
		player_json_request.get({
			// PHP Function name to bind so that it will process it.
			'action': 'ajax-get-posts',
			// Refer to Wordpress 'query_posts' function
			// REF: http://codex.wordpress.org/Function_Reference/query_posts
			
			'posts_per_page': '10', 
			'post_type': 'player',
			'orderby': 'rand',
			'order': 'ASC',
			'meta_key': 'ta_player_details_gender',
			'meta_value' : 'male',
			// Extra custom properties... 
			'excerpt_length': 120,
			'get_thumbnail': true
	
		});	
*/			

		
		var tournament_json_request = new Request.JSON({
			url: site_ajax.ajaxurl, 
			noCache: true, // Force browser not to cache results...
//			onRequest: function(){
//			 	console.log();
//			},
			onSuccess: function(posts,text){
				console.log(posts);
//				posts.each(function(post,index){
//					console.log(post);
//				});
				
				// Update current page title and other elements an initiate GA tracking for ajax calls...
				//document.title =  'AJAX success - ' + document.title;
	
			}
		});
		
	 //UNCOMMENT AND CHECK BROWSER CONSOLE TO SEE RESPONSES
		
		tournament_json_request.get({
			// PHP Function name to bind so that it will process it.
			'action': 'ajax-get-tournaments',
			'json': true
//			'type': 'Disability',
//			'data': 'ID, post_title, post_modified, post_excerpt, type, start_date, end_date, venue, state_country, type, surface, url, online_entry_url'
	
		});		
		
		
	}
	

	
	
});



window.addEvent('load', function(e){

//console.log(window.outputBuffer);


		//Get document URL
		var doc_url = document.url;
		
		//Get Document Title
		var doc_title = document.title;


	// DOUBLE CLICK IMPLEMENTATION (NOT WORKING YET)	
	var ad_300x250 = $$('.ad_300x250');
	var ad_300x250_placeholders = $$('.ad_300x250_placeholders');
	
	if((ad_300x250.length>0 && ad_300x250_placeholders.length>0 && ad_300x250.length == ad_300x250_placeholders.length)){

		ad_300x250_placeholders.each(function(el,i){
			ad_300x250[i].innerHTML = el.innerHTML;
			el.empty();


			// Document write issues affecting this way of inject the ADs.
			// Solution: http://subtlegradient.com/articles/2009/06/22/replace_document_write_using_mootools.html
			
		});
		//alert(ad_300x250.length+'\n'+ad_300x250_placeholders.length);
	}
	
	
	
	
	
	var sharethis = $$('.sharethis');
	if(sharethis.length>0 ){
		
	}

	
});








//})();

