function arrrow_bounce(arrrow,pos,movement,param){
	if(param == undefined)
		param = "top";
	var css = {};
	css[param] = pos+movement;
	arrrow.animate(css,1000,function(){
		css[param] = pos;
		arrrow.animate(css,1000,function(){arrrow_bounce(arrrow,pos,movement,param)});
	})
}
function setup_arrrow(point_to,direction,offset){
	var arrrow=$('<div class="hide_for_animation" id="arrrow_container" style="position:absolute;z-index:999"><img src="/images/arrrow.png" id="arrrow" style="position:relative"></div>').prependTo('body');
	if(direction == undefined)
		direction = "up";
	if(offset == undefined)
		offset = 5;
	if(direction == "up" || direction == "down")
		param = 'top';
	$(window).resize(function(){position_arrrow(arrrow,point_to,offset,direction)});
	setTimeout(function(){position_arrrow(arrrow,point_to,offset,direction)},1000)
	var pos = position_arrrow(arrrow,point_to,offset,direction);
	arrrow_bounce(arrrow.find('img'),offset,30,param);
}

function position_arrrow(arrrow,point_to,offset,direction){
	var x,y,pos;
	if(direction == "up" || direction == "down"){
		x = $('#'+point_to).offset().left + ($('#'+point_to).width()/2) - (arrrow.width()/2);
		if(direction == "up")
			y = $('#'+point_to).offset().top + $('#'+point_to).height() + offset;
		pos = y;
	}
	arrrow.css({left:x,top:y});
	return pos;
}




function submit_request_form(form){
	$.ajax({
		url: $('#'+form).attr("action"),
		type: "POST",
		context: document.body,
		dataType: "text",
		data: get_form_data(form),
		success: function(){
        $("#"+form+"_success").show();
        $("#"+form).hide();
      },
      error: function(){
        $("#"+form+"_error").show();
      }
      });

	return false;
}

function get_form_data(form_id){

	var a = [];
	
	a.push({name: "action", value: "post"});

	$('#'+form_id).find(':input').each(function(){a.push({name: $(this).attr("name"), value: $(this).val()})}) 

	return a;

}

function edit_page(){

	$(".display_page").hide();
	$(".edit_page").show();

}

function scrollTo(object,offset,speed,callback){
	if(speed == undefined)
		speed = 1000;
	if(offset == undefined)
		offset = 0;
	$('html,body').animate({scrollTop:$(object).offset().top - offset},speed);
	setTimeout(callback, speed);
}


function show_hide(container){
	if($('#'+container).is(':hidden'))
		$('#'+container).show("slow");
	else
		$('#'+container).hide("slow");
}





function leave_comment(){
	$.ajax({
		url:'comment.php',
		data:{comment:$('#video_comment').val(), id:$('#video_id').val(),'action':"post_comment"},
		type:'POST',
		dataType:'text',
		success: function(html){
			if(html != "fail"){
				$("#results").append(html);
				$("#all_comments").prepend("<div class='comment'>"+html+"<br>"+$('#video_comment').html()+"</div>");
				$('#video_comment').html("");
			} else {
				alert("fail");
			}
		}
	});
}

function recordOutboundLink(link, category, action) {
  try {
    var myTracker=_gat._getTrackerByName();
    _gaq.push(['myTracker._trackEvent', ' + category + ', ' + action + ']);
    setTimeout('document.location = "' + link.href + '"', 100)
  }catch(err){}
}

function addCommas(nStr)
{
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}


/* BLOG FUNCTIONS */

function submit_comment(blog_id){
	var email = $('#blog_comment_email').val();
	var name = $('#blog_comment_name').val();
	var web = $('#blog_comment_web').val();
	var comment = $('#blog_new_comment').val();
	$.ajax({
		url: "/blog.php",
		data: {"action":"post_comment_check","name":name,"email":email,"web":web,"comment":comment,"blog_id":blog_id},
		type: "POST",
		dataType: "html",
		success: function(data){
			$.ajax({
				url: "blog.php",
				data: {"action":"post_comment",
						"name":name,
						"email":email,
						"comment":comment,
						"web":web,
						"blog_id":blog_id,
						"check_val":data},
				type: "POST",
				dataType: "html",
				success: function(data){
					var new_comment = $('#blog_comment_blank').clone().attr('id','').hide();
					new_comment.find('.blog_comment_body').html(comment);
					new_comment.find('.blog_comment_name').html("By "+name);
					date = new Date();
					new_comment.find('.blog_comment_date').html("just posted");
					$('#blog_comment_container').prepend(new_comment);
					$('#new_comment_box').fadeOut(function(){
						$('#blog_comment_container .blog_comment').fadeIn("slow");
					});
				}
			})
		}
	});
}

function edit_blog(){
	$('#blog_edit').show();
	$('#blog_body').hide();
}

function delete_blog_comment(comment_id){
	$.ajax({
		url:"/blog.php",
		data:{"action":"remove_blog_comment","blog_comment_id":comment_id},
		type:"POST",
		dataType:"html",
		success:function(data){
			if(data == "ok")
				$('#blog_comment_'+comment_id).fadeOut();
		}
	});
}

function approve_blog_comment(comment_id){
	$.ajax({
		url:"blog.php",
		data:{"action":"approve_blog_comment","blog_comment_id":comment_id},
		type:"POST",
		dataType:"html",
		success:function(data){
			if(data == "ok")
				$('#blog_comment_'+comment_id+" .approve_comment").fadeOut();
		}
	});
}

function add_link(link){
	new_link = $('#'+link+'_list').val();
	new_link_name = $('#'+link+'_list option:selected').text();
	remove_link(link,$('#'+link+'_list').val())
	$('#'+link+'_input_array').val($('#'+link+'_input_array').val()+new_link+",");
	$('#'+link+'_list_container').append($("<div class='"+link+"_box' id='"+link+"_box_"+new_link+"'>"+new_link_name+" <a href='#' onclick='javascript:remove_link(\""+link+"\","+new_link+");return false;'>X</a></div>"));
}

function remove_link(link,link_id){
	$('#'+link+'_box_'+link_id).remove();
	var new_links = $('#'+link+'_input_array').val();
	var regex = new RegExp("[\\^,]"+link_id+",", "i");
	new_links = new_links.replace(regex,",");
	regex = new RegExp("^"+link_id+",", "i");
	new_links = new_links.replace(regex,"");
	$('#'+link+'_input_array').val(new_links);
}

function cms_edit_content(container){
	$('#content_block_'+container).hide();
	$('#edit_content_block_'+container).show();
}

function cms_save_content(container){
	$.ajax({
		url:'/cms.php',
		data:{action:'update_content','container':container,content:CKEDITOR.instances["cke_"+container].getData()},
		type:"POST",
		dataType:"html",
		success:function(){
			$('#content_body_'+container).html(CKEDITOR.instances["cke_"+container].getData());
			$('#content_block_'+container).show();
			$('#edit_content_block_'+container).hide();
		}
	});
}




