
function ot_admin() {};

ot_admin.default_image = function(option,color) {
	
	new Ajax.Request('admin-ajax.php',
 	{
	    method:'post',
	    postBody: 'action=ot_default_image&option=' + option + '&color=' + color,
	    onSuccess: function(transport){
	      var response = transport.responseText || "no response text";
	      $(option).src = response;
	    },
	    onFailure: function(){ alert('Something went wrong...') }
 	});

}


ot_admin.editing = false;

ot_admin.get_youtube = function() {
	
	if (ot_admin.editing != false) {
		ot_admin.editing.stop();
	}
	
	$('actual_video_preview').update('<div class="info">Just waiting for you to finish typing..</div>');
	
	ot_admin.editing = new PeriodicalExecuter(function() { 
		new Ajax.Request('admin-ajax.php',
	 	{
		    method:'post',
		    postBody: 'action=ot_youtube_ajax&url=' + $('ot_featured_video_url').getValue(),
		    onLoading: function() {
		    	ot_admin.editing.stop();
		    	$('actual_video_preview').update('<div class="info">Checking YouTube..</div>');
		    },
		    onSuccess: function(transport){
		      var response = transport.responseText;
		     $('actual_video_preview').update(response);
		    },
		    onFailure: function(){ alert('Something went wrong...') }
	 	});
	}, 2);	

}

ot_admin.preview_embed = function() {
	$('actual_video_preview').update($('ot_embed_video').getValue());
}

ot_admin.search_posts = function() {

	if (ot_admin.editing != false) {
		ot_admin.editing.stop();
	}
	
	$('search_results').update('Just waiting for you to finish typing..');
	
	ot_admin.editing = new PeriodicalExecuter(function() { 
		new Ajax.Request('admin-ajax.php',
	 	{
		    method:'post',
		    postBody: 'action=ot_search_posts&search_term=' + $('search_select').getValue(),
		    onLoading: function() {
		    	ot_admin.editing.stop();
		    	$('search_results').update('Searching Posts..');
		    },
		    onSuccess: function(transport){
		      var response = transport.responseText || "no response text";
		      $('search_results').update(response);
		    },
		    onFailure: function(){ alert('Something went wrong...') }
	 	});
 	}, 1);	
 	
}

ot_admin.update_featured_post = function() {

	new Ajax.Request('admin-ajax.php',
	 	{
		    method:'post',
		    postBody: 'action=ot_featured_preview&type=post&id=' + $$('input:checked[type="radio"][name="ot_featured_post"]').pluck('value'),
		    onLoading: function() {
		    	$('admin_featured_post').update('Generating Preview..');
		    },
		    onSuccess: function(transport){
		      var response = transport.responseText || "no response text";
		      $('admin_featured_post').update(response);
		    },
		    onFailure: function(){ alert('Something went wrong...') }
	 	});
	 	
}

ot_admin.update_featured_category = function( id, location ) {

	if(id =='off') {
		 $('admin_featured_category_' + location).update('Deactivated');
		return true;
	}

	new Ajax.Request('admin-ajax.php',
	 	{
		    method:'post',
		    postBody: 'action=ot_featured_preview&type=category&id=' + id,
		    onLoading: function() {
		    	$('admin_featured_category_' + location).update('Generating Preview..');
		    },
		    onSuccess: function(transport){
		      var response = transport.responseText || "no response text";
		      $('admin_featured_category_' + location).update(response);
		    },
		    onFailure: function(){ alert('Something went wrong...') }
	 	});
	 	
}

ot_admin.check_license = function() {
	
	new Ajax.Request('admin-ajax.php',
 	{
	    method:'post',
	    postBody: 'action=ot_license_change&key=' + $('license_input').value,
	    onLoading: function() {
	    	$('license_state').innerHTML = '<strong>Validating, please wait..</strong>';
	    },
	    onSuccess: function(transport){
	    
	    	var response = transport.responseText
	      	
	      	if(response != 0) {
	      
	      	// Go crazy
	      	var results_array = unserialized(response);
	      	$('license_input').disabled = 'disabled';
	      	$('license_button').hide();
	      	$('license_td').setStyle({ border:'2px solid #7cc186', backgroundColor:'#fff' });
	      	$('license_img').src = results_array[5];
	      	$('license_icon').src = results_array[6];
	      	$('license_state').setStyle({ color:'#0C8D1B' });
	      	$('license_state').innerHTML = '<strong>Valid</strong>';
	      	Effect.Appear('party-time');
	      	$('party-time').setStyle({ position: 'absolute', left: '38%', top: '30%' });
	      	
	      }
	      else {
	      	$('license_state').innerHTML = '<strong>INVALID</strong>';
	      }
	    },
	    onFailure: function(){ alert('Something went wrong...') }
 	});
 	
}

ot_admin.toggle_limit_mode = function( value ) {

	if (value == 3) {
		$('limit_mode_3').disabled = '';
	}
	else if (value != 3) {
		$('limit_mode_3').disabled = 'disabled';
	}
	
}

ot_admin.toggle_video_mode = function( value ) {

	if (value == 3) {
	
		$('youtube_label_div').addClassName('blockdisable');
		$('ot_featured_video_url').disabled = 'disabled';
		
		$('embed_label_div').addClassName('blockdisable');
		$('ot_embed_video').disabled = 'disabled';
		$('embed_button').hide();
		
		$('actual_video_preview').update('Featured Video Disabled.');
		
	}
	else if (value == 1) {
	
		$('youtube_label_div').removeClassName('blockdisable');
		$('ot_featured_video_url').disabled = '';
		
		$('embed_label_div').addClassName('blockdisable');
		$('ot_embed_video').disabled = 'disabled';
		$('embed_button').hide();
		
		ot_admin.get_youtube();
		
	}
	else if (value == 2) {
	
		$('embed_label_div').removeClassName('blockdisable');
		$('ot_embed_video').disabled = '';
		$('embed_button').show();
	
		$('youtube_label_div').addClassName('blockdisable');
		$('ot_featured_video_url').disabled = 'disabled';
		
		ot_admin.preview_embed();
		
	}
	
}

// 2. Switch Content Layout
last_header_tab = 'header_tab1';
last_header_bg_tab = 'header_bg_tab1';
last_footer_tab = 'footer_tab1';
last_bg_tab = 'bg_tab1';

colors = new Array();
colors[1] = 'blue';
colors[2] = 'black';
colors[3] = 'green';
colors[4] = 'pink';
colors[5] = 'red';

function show(layerName) { 
	var shuffletop = $(layerName);
	shuffletop.className = 'shuffletop';
} 

function hide(layerName) {
	var shufflebot = $(layerName);
	shufflebot.className = 'shufflebot';
} 

function show_next(tab_name, position) {

	switch(position) {
		case 'header':
			$(last_header_tab).className = 'tab';
			var curr = $(tab_name);
			curr.className='tab_hover';
			hide(last_header_tab+'_data');
			show(tab_name+'_data');
			last_header_tab=tab_name;
			$('header_value').value = 'ot_header_logo_' + colors[tab_name.substr(-1)];
		break;
		
		case 'header_bg':
			$(last_header_bg_tab).className = 'tab';
			var curr = $(tab_name);
			curr.className='tab_hover';
			hide(last_header_bg_tab+'_data');
			show(tab_name+'_data');
			last_header_bg_tab=tab_name;
			$('header_bg_value').value = 'ot_header_bg_' + colors[tab_name.substr(-1)];
		break;
		
		case 'footer':
			$(last_footer_tab).className = 'tab';
			var curr = $(tab_name);
			curr.className='tab_hover';
			hide(last_footer_tab+'_data');
			show(tab_name+'_data');
			last_footer_tab=tab_name;
			$('footer_value').value = 'ot_footer_logo_' + colors[tab_name.substr(-1)];
		break;
		
		case 'bg':
			$(last_bg_tab).className = 'tab';
			var curr = $(tab_name);
			curr.className='tab_hover';
			hide(last_bg_tab+'_data');
			show(tab_name+'_data');
			last_bg_tab=tab_name;
			$('bg_value').value = 'ot_site_background_' + colors[tab_name.substr(-1)];
		break;
	}
	
}
// End 2.
document.observe("dom:loaded", function() {
	if($('header_value') != null) {
		$('header_value').value = 'ot_header_logo_blue';
		$('header_bg_value').value = 'ot_header_bg_blue';
		$('footer_value').value = 'ot_footer_logo_blue';
		$('bg_value').value = 'ot_site_background_blue';
	}
});
