jQuery(document).ready(function(){
   	
	var post = false;

	jQuery('#searchInputBar').focus(function(){ jQuery(this).val(''); });	
	//jQuery('#searchInputBar').blur(function(){ jQuery(this).val('Search'); jQuery('#searchResults').html(' '); });

	jQuery('#searchInputBar').keyup(function(event){
      	if(jQuery(this).val().length > 2 && post == false){
			jQuery.ajax({
			  	type: "POST",
			  	url: base + "search/ajax/" + jQuery('#searchInputBar').val(),
				dataType: "html",
				beforeSend: function(){
     				post = true;					
   				},
				success: function(html){
					post = false;
					jQuery('#searchResults').html(html);
   				}
			})
		}else{
			jQuery('#searchResults').html(' ');
		}
	});    
});

/* Ajax Taggings */
var tagging;

jQuery(document).ready(function(){   	
    jQuery('#tagInput').focus(function(){ 
		tagging = true; 
		//jQuery('#pagin').html(" " + tagging);  
	});
	jQuery('#tagInput').blur(function(){ 
		tagging = false; 
		//jQuery('#pagin').html(" " +  tagging); 
	});
	
	jQuery('#tagInput').keyup(function(event){
		//jQuery('#pagin').html(" " +  event.keyCode);		
		if(event.keyCode == 13){ 
			jQuery.ajax({
			  	type: "POST",
			  	url: base + 'async/add/',
				data: "content_id="+jQuery("#content_id").val() + "&tags="+jQuery("#tagInput").val() + "&type="+jQuery("#content_type").val(),
				dataType: "html",
				beforeSend: function(){
     				post = true;					
   				},
				success: function(html){					
					jQuery('#tagList').html(html);
					jQuery('#tagInput').val("");
					refresh();
   				}
			})
		}
	});
	// tag deleting
	refresh();
});

function refresh()
{
	jQuery('.tag_click').click(function(){
		//alert(jQuery(this).attr('name'));
		jQuery.ajax({
		  	type: "POST",
		  	url: base + 'async/tagdelete/' + jQuery(this).attr('name'),
			data: "content_id="+jQuery("#content_id").val() + "&type="+jQuery("#content_type").val(),
			dataType: "html",
			beforeSend: function(){
 				post = true;					
			},
			success: function(html){					
				jQuery('#tagList').html(html);
				jQuery('#tagInput').val("");
				refresh();
			}
		})
	})	
}

/* File Adding */
jQuery(document).ready(function(){   	
	
	filedelete();
	jQuery('#fileSearch').keyup(function(event){
		//jQuery('#pagin').html(" " +  event.keyCode);		
		jQuery.ajax({
		  	type: "POST",
		  	url: base + 'async/fileSearch/',
			data: "filename="+jQuery("#fileSearch").val(),
			dataType: "html",
			beforeSend: function(){
     				post = true;					
   				},
			success: function(html){					
				jQuery('#fileOutput').html(html);
				fileadd();
   			}
		})		
	});	
});

function fileadd()
{	
	jQuery('.file_add').click(function(){
		//alert("content_id="+jQuery("#file_content_id").val() + "&file_id="+jQuery(this).attr('id') + "&type="+jQuery("#file_content_type").val());
		jQuery.ajax({
		  	type: "POST",
		  	url: base + 'async/fileadd/',
			data: "content_id="+jQuery("#file_content_id").val() + "&file_id="+jQuery(this).attr('id') + "&type="+jQuery("#file_content_type").val(),
			dataType: "html",
			beforeSend: function(){
		     				post = true;					
		   				},
			success: function(html){									
				updateFileList();
		   	}
		})
	})
}

function filedelete()
{	
	jQuery('.file_delete').click(function(){
		jQuery.ajax({
		  	type: "POST",
		  	url: base + 'async/filedelete/' + jQuery(this).attr('id'),
			data: "content_id="+jQuery("#file_content_id").val() + "&file_id="+jQuery(this).attr('id') + "&type="+jQuery("#file_content_type").val(),
			dataType: "html",
			beforeSend: function(){					
		   				},
			success: function(html){									
				updateFileList();
		   	}
		})
	})
}

function updateFileList()
{
	jQuery.ajax({
	  	type: "POST",
	  	url: base + 'async/filelist/' + jQuery("#file_content_id").val() + '/resources/',
		dataType: "html",
		success: function(html){								
			window.location.reload();
	   	}
	})
}

/* Flash Message */
jQuery(document).ready(function(){   	
    jQuery('#flash_message_box').fadeOut(2000);
});

// Events Change
jQuery(document).ready(function(){  	 		
	update_event('.cell_calendar_tradeshow');
	update_event('.cell_calendar_class');
	
	jQuery("#eventsPagination a").click(function(){			
		event_rotate(jQuery(this).attr('id'));	
	});	
			
	jQuery("#newsPagination a").click(function(){		
		news_rotate(jQuery(this).html());	
	});	
	
	
	jQuery("#testimonialPaginate a").click(function(){		
		testimonial_rotate(jQuery(this).html());	
	});	
	
});

function update_event(target)
{
	jQuery(target).click(function(){		
		jQuery('#event_json_date').html(events_array[jQuery(this).attr('ev_id')].date);
		jQuery('#event_json_title').html(events_array[jQuery(this).attr('ev_id')].name);
		jQuery('#event_json_description').html(events_array[jQuery(this).attr('ev_id')].description);
	});
}

function event_rotate(target)
{	
	if(target == 'event_next'){ current_index  = current_index +1; }
	if(target == 'event_previous'){ current_index = current_index -1 ; }
	if(current_index == -1){ current_index = (event_rotator.length -1); }
	if(current_index == (event_rotator.length)){  current_index = 0; }
	if(target == 'event_last'){ current_index = (event_rotator.length -1); }

	//jQuery('#pagin').html(" " + current_index + "  " + target + " " );		
	
	jQuery('#event_json_date').html(events_array[event_rotator[current_index]].date);
	jQuery('#event_json_title').html(events_array[event_rotator[current_index]].name);
	jQuery('#event_json_title').attr("href", "/event/show/" + events_array[event_rotator[current_index]].id);	//SGS-JMM 11/04/2009
	jQuery('#event_json_description').html(events_array[event_rotator[current_index]].description);
}

/* News Rotation Business */
function news_rotate(target)
{
	if(target == 'Next'){ news_index  = news_index +1; }
	if(target == 'Previous'){ news_index = news_index -1 ; }
	if(news_index == -1){ news_index = (news_rotator.length -1); }
	if(news_index == (news_rotator.length)){  news_index = 0; }
	if(target == 'Last'){ news_index = (news_rotator.length -1); }
		
	//jQuery('#newgin').html(" " + news_index + "  " + target + " " );		
	
	jQuery('#news_json_date').html(news_json[news_rotator[news_index]].published);
	jQuery('#news_json_headline').html(news_json[news_rotator[news_index]].headline);
	jQuery('#news_json_copy').html(news_json[news_rotator[news_index]].copy);
	jQuery('#news_json_permalink').attr('href', news_json[news_rotator[news_index]].permalink);
}

function testimonial_rotate(target)
{
	if(target == 'Next'){ t_index  = t_index +1; }
	if(target == 'Previous'){ t_index = t_index -1 ; }
	if(t_index == -1){ t_index = (t_size); }
	if(t_index == (t_size)){  t_index = 0; }
	if(target == 'Last'){ t_index = (t_size); }

	jQuery('#testimonials').html(testimonials[t_index].body);
	jQuery('#user_name').html(testimonials[t_index].user_name);
	jQuery('#company').html(testimonials[t_index].company);
}


// Sequence Update

jQuery(document).ready(function(){
   sequence('sort_update2', 'sequenceList2');
   sequence('sort_update', 'sequenceList');

	setInterval(testesAuto, 8000);


});


function testesAuto()
{
	testimonial_rotate('Next');
}

function sequence(click, target_list)
{				
		var string = "";
		var count = 0;
		jQuery('#' + click).click(function(event){
			jQuery('#' + target_list + ' li').each(function(){			
				string = string + jQuery(this).attr('id') + "=" + count + '&';
				count++;
			})
					
		  jQuery.ajax({
		  	type: "POST",
		  	url: base + 'async/sequence/',
			data: string,
			dataType: "html",
			success: function(html){					
				// console.log(html);
				count = 0;
				jQuery('.alert_message').html('thanks, the sequence has been updated');
			}
		})
	});
}