var intervalId = 0;
$(document).ready(function () 
{
	$('body').browserDetection();
	
	$('#reference').change(function ()
	{
		if ($(this).val() == '0') $('#other_reference_container').show();
		else $('#other_reference_container').hide();
	});
	
	$('a.popup').click(function ()
	{
		var href_array = $(this).attr('href').split('/');
		popup.open(href_array[(href_array.length-1)]);
		return false;
	});
	
	$('.teaser').click(function ()
	{
		window.location = $(this).find('.link a').attr('href');
		return false;
	});
	
	$('#teasers .loginteaser .form .field div').click(function ()
	{
		$text = $(this);
		$input = $(this).parent().find('input');
		$text.hide();
		$input.val('');
		$input.focus();
	});
	$('#teasers .loginteaser .form .field input[type=text]').focus(function ()
	{
		$input = $(this);
		if ($input.val() == 'E-mail') $input.val('');
	});
	$('#teasers .loginteaser .form .field input[type=password]').focus(function ()
	{
		$text = $(this).parent().find('div');
		$input = $(this);
		$text.hide();
	});
	$('#teasers .loginteaser .form .field input[type=text]').blur(function ()
	{
		if ($input.val() == '') $input.val('E-mail');
	});
	$('#teasers .loginteaser .form .field input[type=password]').blur(function ()
	{
		$text = $(this).parent().find('div');
		$input = $(this);
		if ($input.val() == '') $text.show();
	});
	
	Preloader.add(global_url + 'image/btn.arrow.hover.gif');
	Preloader.add(global_url + 'image/btn.arrow.purple.hover.gif');
	Preloader.add(global_url + 'image/btn.arrow.grey.hover.gif');
	Preloader.add(global_url + 'image/btn.arrow.hover.gif');
	Preloader.add(global_url + 'image/btn.back.arrow.hover.gif');
	Preloader.add(global_url + 'image/btn.add.hover.gif');
	Preloader.add(global_url + 'image/btn.feedback.hover.gif');
	Preloader.load();
});

var popup = {
	initialized: false,
	init: function(close)
	{
		if (close==null) close = true;
		if(this.initialized) return;
		if (close == true)
		{
			$('#popup-background').click(popup.close);
			$('#popup .close').click(function ()
			{
				popup.close();
				return false;
			});
		}
		else
		{
			$('#popup .close').hide();
		}
		this.initialized = true;
	},
	open: function(id, close, width)
	{
		if (close==null) close = true;
		if (width==null) width = '710px';
		$('#popup').css('width',width).css('left', (($(document).width()/2)-($('#popup').width()/2)) + 'px').css('top', ($(window).scrollTop() + 50) + 'px').show();
		$('#popup-background').css('height', $(document).height() + 40).show();
		popup.init(close);
		if (id) popup.getHTML(id);
	},
	close: function()
	{
		$('#popup').hide();
		$('#popup-background').hide();
		$(this).html('');
	},
	getHTML: function(id)
	{
		$.get(global_url + 'ajax/popup/' + id, null, function(response) {
			popup.html(response.html);
			$('#popup-background').css('height', $(document).height() + 40);
			init_cufon_button();
		}, 'json');
	},
	html: function(html)
	{
		$('#popup .content .container').html(html);
	},
	error: function (error)
	{
		popup.html(error);
		popup.open(null,null,'400px');
		$('#popup').css('top', ($(window).scrollTop() + ($(window).height()/2)-$('#popup').height()) + 'px').show();
	}
}

var Preloader = {
  callbacks: [],
  images: [],
  loadedImages: [],
  imagesLoaded: 0,

  add: function(image){
    if (typeof image == 'string') this.images.push(image);
    if (typeof image == 'array' || typeof image == 'object'){
      for (var i=0; i< image.length; i++){
        this.images.push(image[i]);
      }
    }
  },
  onFinish: function(func){
    if (typeof func == 'function') this.callbacks.push(func);
    if (typeof func == 'array' || typeof func == 'object'){
      for (var i=0; i< func.length; i++){
        this.callbacks.push(func[i]);
      }
    }
  },
  load: function(){
    for(var i=0; i<this.images.length; i++){
      this.loadedImages[i] = new Image();
      this.loadedImages[i].onload = function(){ Preloader.checkFinished.apply(Preloader) }
      this.loadedImages[i].src = this.images[i];
    }
  },

  checkFinished: function(){
    this.imagesLoaded++;
    if (this.imagesLoaded == this.images.length) this.fireFinish();
  },
  fireFinish: function(){
    for (var i=0; i<this.callbacks.length; i++){
      this.callbacks[i]();
    }
    this.images = [];
    this.loadedImages = [];
    this.imagesLoaded = 0;
    this.callbacks = [];
  }
}

function create_survey ()
{
	window.location = global_url + 'survey/create';
}
function delete_survey(name, url)
{
	if (confirm("Er du sikker på du vil slette undersøgelsen: " + name + "?"))
	{
		window.location = url;
	}
}
function submit_login()
{
	$('#login_form').submit();
}
