/*---addCategory()-------------------------------------------------------------------------------------------
Purpose: For video/add page, does an ajax call that inserts the new category into the db category with associate b_id.
---------------------------------------------------------------------------------------------------------
Args: 
b_id: string: name of form element with this value
name: string: name of form element with this value
---------------------------------------------------------------------------------------------------------
Returns: Clears the thinking state on page
---------------------------------------------------------------------------------------------------------
Example: We'll be calling this dynamically so getting the value of two input fields on the page for args 
when a button is clicked.  

var b_id = 'b_id';
var name = 'category';
addCategory(b_id,name);	

-------------------------------------------------------------------------------------------------------*/
function addCategory(b_id,name){
	
	var b_id = $('#'+b_id).val();
	var name = $('#'+name).val();
	
	if(name != ""){
		$('#category').remove();
		$(cancel_btn).remove();
		$(add_btn).remove();
		var working= "<p id='working'>Working...</p>";
		$(working).insertAfter('#select_toggle');
		$.ajax({
			type: "POST",
			url: "/video/addCategory",
			data: "name="+name+"&b_id="+b_id,
			success: function(msg){
				$('#working').remove();
				$(select_category).html(msg);
				$(select_category).attr('selectedIndex', 0).insertAfter('#select_toggle');	
				$('#select_toggle').text(category_text);
				selected();				
			}
		});
	}
}

$(document).ready(function() {

	if($('#make_another').length > 0) {
		$('#make_another').bind('click', function(click) {
			click.preventDefault();
			$('#follow_up').attr('value','1');
			$('#comp_form').submit();
		});
	}
	
	if($('#user_type').length > 0) {

		$('#user_type').bind('change', function(i) {
			if($(this).attr('value') === "business") {
				$('.business').slideDown();
				$('.resellerx').slideDown();
				$('.vendor').slideDown();
			} else if($(this).attr('value') === "reseller") {
				$('.business').slideUp();
				$('.resellerx').slideDown();
				$('.vendor').slideDown();
			} else if($(this).attr('value') === "vendor") {
				$('.business').slideUp();
				$('.resellerx').slideUp();
				$('.vendor').slideDown();			
			} else {
				$('.business').slideUp();
				$('.resellerx').slideUp();
				$('.vendor').slideUp();				
			}
				
		});
	}




	if($('#video_date').length>0) {
		$("#video_date").datepicker({
			'buttonImageOnly':true,
			showAnim: 'blind'
		});
	}
	if($('#countdown_end_date').length>0) {
		$("#countdown_end_date").datepicker({
			'buttonImageOnly':true,
			showAnim: 'blind'
		});
	}
	$('#video_upload').bind('click', function(click) {
		//click.preventDefault();
		$("#progress-bar").progressbar({value: 0}).fadeIn();
		var t=setTimeout("timedCount()",1000);
	});
	
	selected();
	billing_address();
	forgotten_password();
	/*$('input').formReplace({
		alt_text: {'#website': 'http://'},
		ignore: ['#stream_ftp_host','#stream_ftp_username']
	});*/
	company_type();
	close_message();
	new_comment();
	
	
	/* ColorBox */
	if($('.add_new_user').length>0) {
		$('.add_new_user').colorbox({
			href:"/user/add",
			close:'Close',
			onComplete:function(){
				$('#contact_type').bind('change', function(change){					
					var e = $(this);
					var next = $(this).next('.other');
					
					if($(e).val() == 'Other') {
						$(next).slideDown();
					} else {
						if($(next).css('display') == 'block'){
							$(next).slideUp();
						}
					}
				});
			}
		});
	}
	
	if($('.edit_user').length>0) {
		$.each($('.edit_user'), function() {
			var num = $(this).attr('name');
			$(this).colorbox({
				href:"/user/edit/"+num,
				close:'Close',
				onComplete:function(){
					$('#contact_type').bind('change', function(change){
						var e = $(this);
						var next = $(this).next('.other');
						
						if($(e).val() == 'Other') {
							$(next).slideDown();
						} else {
							if($(next).css('display') == 'block'){
								$(next).slideUp();
							}
						}
					});
				}
			});
		});
	}	
	
	
	if($('.add_new_user_comp').length>0) {
		str = $('.add_new_user_comp').attr('name');
		str.toString();
		if(str != ""){
			str = str.split("-");	
		}
		var my_url;
		switch(str.length){
			case 1:
				my_url = "/user/add/company/0/0/"+str[0];
				break;
			case 2:
				my_url = "/user/add/company/0/"+str[0]+"/"+str[1];
				break;
			case 3:
				my_url = "/user/add/company/"+str[0]+"/"+str[1]+"/"+str[2];
				break;
				
		}
		$('.add_new_user_comp').colorbox({
			href:my_url,
			close:'Close',
			onComplete:function(){
				$('#contact_type').bind('change', function(change){					
					var e = $(this);
					var next = $(this).next('.other');
					
					if($(e).val() == 'Other') {
						$(next).slideDown();
					} else {
						if($(next).css('display') == 'block'){
							$(next).slideUp();
						}
					}
				});
			}
		});
	}	
	/*if($('.email').length>0) {
		$('.email').colorbox({
			href:"email.html",
			close:'Cancel',
			onComplete:function() {
				$("#email_to").fcbkcomplete({
					json_url: "email_test_json.txt",
					cache: true,
					//filter_case: true,
					filter_hide: true,
					firstselected: true,
					filter_selected: true,
					//newel: true
				});
			}
		});
	}*/
	if($('.preview').length>0) {
		$.each($('.preview'), function() {
			params = $(this).attr('name');
			params.toString();
			if(params != ""){
				params = params.split("|");	
			}
			$(this).colorbox({
				html:'<div class="widget"><embed width="640" height="480" flashvars="file='+params[0]+'&streamer=rtmp://thebroadcastgroup.flashsvr.com/'+params[1]+'/_definst_/&skin=http://www.tbgstreaming.com/player/skins/modieus.swf&autostart=true" wmode="opaque" allowscriptaccess="always" allowfullscreen="true" quality="high" name="single" src="http://www.tbgstreaming.com/player/player-licensed.swf" type="application/x-shockwave-flash"/></div>'
			});
		});
	}

/*	if($('#video_tags').length > 0) {
		$('#video_tags').fcbkcomplete({
			//json_url: "email_test_json.txt",
			cache: true,
			filter_hide: true,
			firstselected: true,
			filter_selected: true,
			newel: true
		});	
	}*/
	
	add_ticket_user();
	
	/* If there is a secondary list defined left this will make list 20px to the right of the text */
	$('ul.secondary.left').each(function(i, item){
		var prev = $(item).prev('h3').find('span').css('display', 'inline-block').width();
		$(item).css('left', (prev + 24) + 'px');
	});

	loading_demo();
	
	/* Testing Autocomplete */	
	// Fade out the suggestions box when not active
	$("#search").blur(function(){
		$('#suggestions').fadeOut();
	}).bind('keyup', function(up) {
		lookup($(this).val());
		/* Runs PHP Function that search DB...let me know if this will work or be too heavy on the site. */
	});
	
	$('#account_status').bind('change', function(change){ 
		if($(this).val() == 'Suspended') {
			
			var curr = $(this).val();
			
			$.fn.colorbox({
				href:'/message/suspended', 
				open:true,
				onLoad: function() {
					$('#cboxClose').bind('click', function() {
						$('#account_status').val(curr);
					});
					$('#cboxOverlay').bind('click', function() {
						$('#account_status').val(curr);
					});
				}
			});
		}
	});
	
	$('#new_password').bind('click', function() {
		$('.reset_password').slideToggle();
	});
	
	toggle_checkbox('paypal_donations');
	toggle_checkbox('change_password');
	toggle_checkbox('edit_billing');
	toggle_checkbox('countdown_timer');
	
	$('#login_btn').bind('click', function(event) {
		event.preventDefault();
		if($(this).text() == 'Log In') {
			$(this).text('View Today\'s Event');			
			$('#todays_event').fadeOut(function() {
				$('.login_bar').animate({ width: "330px" }, 700 );
				$('#container').fadeIn();
			});
			
		} else { 
			$(this).text('Log In');
			$('#container').fadeOut(function() {
				$('.login_bar').animate({ width: "620px" }, 700 );
				$('#todays_event').fadeIn();
			});			
		}
	});
	
});
	 
function toggle_checkbox(elem) {
	
	if($('#'+elem).length>0) {
		$('#'+elem).attr('checked','');
		
		$('#'+elem).bind('click', function() {
			$('.'+elem).slideToggle();
		});
	}
	
}

function lookup(inputString) {
	if(inputString.length == 0) {
		$('#suggestions').fadeOut(); // Hide the suggestions box
	} else {
		$.post("autocomplete_search.php", {queryString: ""+inputString+""}, function(data) { // Do an AJAX call
			$('#suggestions').fadeIn(); // Show the suggestions box
			$('#suggestions').html(data); // Fill the suggestions box
		});
	}
}

function loading_demo() {

	/* 	This is the loading function examples you can call this function 'loading()' anywhere inside a widget and on any event ... or so I think
		and it will run locally to the widget itself.  Instructions are in the plugin.
	*/
	$('.disp_loading').click(function(click) {
		click.preventDefault();
		$(this).loading();
	});
	$('.disp_update').click(function(click) {
		click.preventDefault();
		$(this).loading({label: "Still Loading!", status: "continue"});
	});
	$('.disp_complete').click(function(click) {
		click.preventDefault();
		$(this).loading({status:"complete"});
	});
	$('#ftp_host').bind('focus', function() {
		$(this).loading();
	});
	$('#ftp_username').bind('focus', function() {
		$(this).loading({status:'continue', label:'Almost Finished'});
	});
	$('#ftp_password').bind('focus', function() {
		$(this).loading({status:'complete'});
	});

}

function timedCount() {

	curr_percent = $("#progress-bar").progressbar("option", "value");
	
	if(curr_percent < 100) {
		
		curr_percent = (curr_percent + 1);
		t=setTimeout("timedCount()",500);
		$('#progress-bar').progressbar('option', 'value', curr_percent);
		
	} else {		
		curr_percent = 0;
		t=setTimeout("timedCount()",500);
		$('#progress-bar').progressbar('option', 'value', curr_percent);

	
	}

}

function selected() {

	select_category = $('#category');
	
	category_text = $('#select_toggle').text();
	
	new_category = $('<input>')
							.attr('type', 'text')
							.attr('id', 'category')
							.attr('name', 'category');
							
	cancel_btn = $('<a>')
							.attr('id', 'cancel_new')
							.attr('href', '#')
							.text('Cancel')
							.bind('click', function(click) {
								
								click.preventDefault();
							
								$('#category').remove();
								$(this).remove();
								$(add_btn).remove();
								$(select_category).attr('selectedIndex', 0).insertAfter('#select_toggle');
								$('#select_toggle').text(category_text);
								selected();
							
							});
							
	add_btn = $('<a>')
						.attr('id', 'add_new')
						.attr('href', '#')
						.text('Add')
						.bind('click', function(click) {
							
							click.preventDefault();
							//ACM: call to addCategory() here and pass args...
							var b_id = 'b_id';
							var name = 'category';
							addCategory(b_id,name);						
						});

	$(select_category).change(function() { 
		
		var curr_select = $($(this) +'option:selected').text();
		
		if(curr_select == 'Create New Category') {
			
			$(this).remove();
			$('#select_toggle').text('New '+ category_text);
			$(new_category).insertAfter('#select_toggle');
			$(add_btn).insertAfter('#category');
			$(cancel_btn).insertAfter('#add_new');
			
		}
	});

}

function billing_address() {
	if($('#billing_address_form').attr('checked') == true) {
		$('.billing_address').slideUp();
	} else {
		$('.billing_address').slideDown();
	}
	
	$('#billing_address_form').bind('click', function(click) {
				
		if($(this).attr('checked') == true) {
		
			$('.billing_address').slideUp();
			
		} else {
		
			$('.billing_address').slideDown();
		
		}
		
	});

}

function forgotten_password() {
	
	$('.forgotten_password').bind('click', function(click) {
		
		if($('#login').text() == 'Login') {
		
			click.preventDefault();
				
			$('#login_form').slideUp('fast', function() {
				$('#forgotten_form').slideDown();
			});
			
			$('#login').text('Generate Password');
			$(this).text('Login');
			$('#login_title').text('Forgotten Password');
		
		} else {
			
			click.preventDefault();
		
			$('#forgotten_form').slideUp('fast', function() {
				$('#login_form').slideDown();
			});
		
			$('#login').text('Login');
			$(this).text('Forgotten Password');
			$('#login_title').text('Login');
		
		}
	
	});

}

function company_type() {
	var type = $('#company_type').attr('value');
	if($('#company_type').attr('value') === "business") {
		$('.reseller_info').slideDown();
	} else {
		$('.reseller_info').slideUp();
	}	
	
	$('#company_type').bind('change', function(i) {
		if($(this).attr('value') === "business") {
			$('.reseller_info').slideDown();
		} else {
			$('.reseller_info').slideUp();
		}
		
	});
}

function close_message() {

	$('.close').bind('click', function(click) {
	
		click.preventDefault();
	
		$(this).parents('.parent').slideUp();
		
	});
	
}

function onSelectChange(){
	var id = $(this).attr('id');
	var selected = $("#" + id + " option:selected");    
	var output = "";
	var abbr = selected.attr('value');

	if(selected.val() != 0) {
		selected.text(abbr);
	}
	//$("#output").html(output);
}

function new_comment() {

	$('#new_comment').bind('click', function(click) {
		
		click.preventDefault();
		
		if($(this).text() == 'New Comment' || $(this).text() == 'Edit Comment') {
			$(this).text('Cancel');
			$('#comment_form').slideDown();
		} else {
			if($('#comment_form').text() == '') {
				$(this).text('New Comment');
			} else {
				$(this).text('Edit Comment');
			}
			$('#comment_form').slideUp(function() { $(this).attr('style',''); });
		}
		
	});

}

function add_ticket_user() {

	var container = $('.add_user');
	var field = $('#add_user');
	var trigger = $('.add_user_btn');
	
	/*$(field).autocomplete(cities, {
		multiple: true,
		mustMatch: true,
		autoFill: true
	});*/

	$(trigger).bind('click', function(click) {
		
		click.preventDefault();
		
		($(trigger).text() == 'Add New') ? $(trigger).text('Cancel') : $(trigger).text('Add New');
		
		$(container).slideToggle();
		
	});

}