	
/* PUBLIC FUNCTIONS 
 * ----------------------------------------------------------------------------------------------------------*/

function get_autocomplete_data(target) {
	var data = new Array();
	$("#"+target+" option").each(function(i){
		var opt = new String();
		if ($(this).text() != "---------"){
			opt = $(this).text();
			data.push(opt);
		}
	});
	return data;
}

var timeouts = Array();
function openLoadingDialog() {
	$('#dialog_loading .message').hide();
	/* This is a non optimal alternative to The terrible hack */
	//var dialog = $('#dialog_loading').closest('.ui-dialog');
	//window.scrollTo(dialog.offset().left, dialog.offset().top - $(window).height() / 2 + dialog.height() / 2);
	$('#dialog_loading .message').each(function(i, el){
		$($(el).attr("class").split(" ")).each(function(i, c) {
			if (c.indexOf("time") === 0) {
				var time = c.split("-")[1];
				timeouts[time] = setTimeout('$("#dialog_loading .time-'+time+'").slideDown();', time * 1000);
			}
		});
	});
}
function closeLoadingDialog() {
	for (i = 0; i < timeouts.length; i++) {
		clearTimeout(timeouts[i]);
	}
}

$(document).ready(function(){
	
	$("#toolbar-user li").hover(
		function(){
			$(this).addClass("active");
		},
		function(){
			$(this).removeClass("active");
		}
	);
	$(".to-hover").hover(
		function(){
			$(this).addClass("mouse-over")
		},
		function(){
			$(this).removeClass("mouse-over")
		}
	);
	$(".to-link").click(function(){
		var rel = $(this).attr("rel");
		if(rel != "0")
		{
			window.location.href = rel;
		}
		else return false;
	});
	$(".match_url").each(function(index, obj){
		//var obj = $(this)
		getTinyURL($(obj).val(), function(tinyurl){
			// Do something with tinyurl:
			var href_attr = ""
			var href_attr = $(obj).nextAll(".twitter-share-button").attr("href");
			$(obj).nextAll(".twitter-share-button").attr("href", href_attr+tinyurl);
		});
	});
	
	$("#dialog_loading").dialog({
		modal: true,
		autoOpen: false,
		closeOnEscape: true,
		resizable: false,
		draggable: false,
		dialogClass: "loading-dialog",
		width: "200",
		minHeight: "70",
		buttons: {"": function(){}}, /* The terrible hack prevents IE from scrolling to bottom */
		open: openLoadingDialog,
		close: closeLoadingDialog
	});

	$(".send").click(function(){
		$("#dialog_loading").dialog('open');
	});

	$('body').addClass(jQuery.uaMatch(navigator.userAgent).browser);
	
	$("#feed-filter li a").each(function(obj){
		if(!$(this).parent("li").hasClass("first")){
			$(this).simpletip({
				content: $(this).attr("title"),
				fixed: true,
				position: [-20,-20], 
				baseClass: "feed-filter-tooltip"
			});
		}
	});
	$(".add-on > input").click(function(){
		$(this).parent(".add-on").toggleClass("active");
	})
});

function enableShareResult() {
	$(".type_result").unbind('mouseenter mouseleave');
	$(".type_result").hover(
		function(){
			$(this).addClass("fb-share-on")
		},
		function(){
			$(this).removeClass("fb-share-on")
		}
	);
}

function get_tweets(){
	$(document).ready(function(){
		$.ajax({
		   type: "GET",
		   url: "http://twitter.com/statuses/user_timeline.json?",
		   data: "screen_name=konkuri&count=5",
		   dataType : "jsonp",
		   success: function(data, msg){
		     $("#twitter_update_list").append('<li><a href="http://twitter.com/konkuri/status/'+data[0]["id_str"]+'" title="'+data[0]["text"]+'" target="_blank">'+data[0]["text"]+'</a></li>');
		   }
		});

	})
}

function create_konami_dialog(){
	$("#dialog_konami").dialog({
		width:660,
		height:600,
		autoOpen:false,
		modal: true,
		overlay: {
			backgroundColor: '#1F4678',
			opacity: 0
		}
	});
}

function add_konami_listener(){	
	var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";  
	
	$(document).keydown(function(e) {
		kkeys.push( e.keyCode );
		if ( kkeys.toString().indexOf( konami ) >= 0 ){
			$(document).unbind('keydown',arguments.callee);
			$("#dialog_konami").dialog('open');
			add_konami_listener()
		}
	});
	
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        return [curleft,curtop];
    }
}

function time_select(target)
{
    $(target).click(function(event){
        event.stopPropagation();
        var target_position = $(this).position();
        $(this).select();
        target_input = target;
        $(".support-time").css("left",target_position.left);
        $(".support-time").css("top",target_position.top+25);
        $(".support-time").show();
    })
}

$('html').ajaxSend(function(event, xhr, settings) {
	function getCookie(name) {
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
	if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
		// Only send the token to relative URLs i.e. locally.
		xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
	}
});


