function cycle_homepage() {
	if($("div.middle_content_picture1").length) {
		var j = '';
		for(var i=0;i<5;i++){
			if (i>0) { var j = i};
			$('div.middle_content_picture'+j).cycle({
			fx: "fade",
			speed: 1000,
			timeout: 10000,
			pause: 1
			});
		}
	}
	var angel_count = $('span.helper').text();
	var timeout = 36000 / angel_count 
	$('div.middle_subcontent_left').cycle({
	fx: "fade",
	speed: 1000,
	timeout: timeout,
	pause: 1
	});
	$("div.scrollable").scrollable({
		clickable: true,
		loop: true,
		size: 1,
		speed: 200,
		items: '#thumbs'
	});	
}

//Checking version web browser
var Browser = {
	Version: function() {
	var version = 999; // we assume a sane browser
	if (navigator.appVersion.indexOf("MSIE") != -1)
	// bah, IE again, lets downgrade version number
	version = parseFloat(navigator.appVersion.split("MSIE")[1]);
	return version;
	}
}
function checkValue(input)
{
if (input.value!=""){
$("img.loading").remove();
$("#"+input.id).after("<img src='/site_media/images/ajax-loader.gif' class='loading' />");
$.ajax({
    url: "/accounts/validation/",
    type: 'GET',
    dataType: 'html',
	data: input.name+"="+input.value,
    success: function(text){
		if (text=="existing") {
		var error_text = "This "+input.name+" is already taken. Please choose another."
		var lang = $("div.language").text();
		if (lang == 'pl') {
			if (input.name=='username') error_text = "Ta nazwa użytkownika jest już zajęta. Wybierz inną.";
			if (input.name=='email') error_text = "Ten adres email jest już używany. Użyj innego adresu email."
		} 
		$("#"+input.id).nextAll("ul.errorlist").remove();
		$("#"+input.id).after("<ul class='errorlist'><li>"+error_text+"</li></ul>");
		}
		else{
		$("#"+input.id).nextAll("ul.errorlist").remove();
		}
    }
});
$("img.loading").ajaxSuccess(function(request, settings){
  $(this).remove();
});
}
}
function check_country()
{  
	if ($("#id_country").val()!="US"){
		$("#id_state").attr("disabled","disabled");
	}
	else{
		$("#id_state").removeAttr("disabled");
	}
}
function showAddAnotherPopup(triggeringLink, width, height, link) {
    var name = triggeringLink.id;
    href = triggeringLink.href;
	if (link) href = link;
    if (href.indexOf('?') == -1) {
        href += '?_popup=1';
    } else {
        href  += '&_popup=1';
    }
    var win = window.open(href, name, 'height='+height+',width='+width+',resizable=yes,scrollbars=yes');
    win.focus();
    return false;
}
function get_mailing_page() {
$.post("/mailing/preview/",
  {message: $("#id_message").val(), username: $("#id_author").val(), receiver: $("#id_receiver").val()},
  function(data){
	$("#response_text").text(data);
  }
);
}

function preview_mailing_page() {
    var mypopup=window.open("","preview_window",'width=950,height=700,resizable=yes,scrollbars=yes');
    mypopup.document.write($("#response_text").text());
    mypopup.document.close();
    mypopup.focus();
}
function dismissAddAnotherPopup(win, newId, newRepr, data, name) {
    var elem = document.getElementById(name)
    if (elem) {
        if (elem.nodeName == 'SELECT') {
            var o = new Option(newRepr, newId);
            elem.options[elem.options.length] = o;
            o.selected = true;
        } else if (elem.nodeName == 'INPUT') {
            elem.value = newRepr;
        }
    } else {win.close();}
	if (name=="id_addressbook"){
	}
	win.close();
}
function add_remove_field(field, action) {
	var number = parseInt(field.substring(4));
	var next_field = 'link' + (number+1);
	var id_next_field = 'id_' + next_field;
	var id_next_add = 'add_' + next_field;
	var id_next_remove = 'remove_' + next_field;
	var id_before_remove = 'remove_' + field;
	var plus=" ";
	if (number < 4) {
	if (action=='add' && $("#id_"+field).val()) {
	if (number < 3){
		plus = " <a id='"+id_next_add+"'><img src='/site_media/images/plus.gif' border='0'/></a>";
	}
	$("#register_tb_3_5").append(
	"<tr class="+next_field+"><td>Dream photo</td>"+
	"<td style='width:286px'><input type='file' id='"+id_next_field+"'name='"+next_field+"'/></td>"+
	"<td>"+plus+
	" <a id='"+id_next_remove+"'><img src='/site_media/images/minus.gif' border='0'/></a>"+
	"</td></tr>");
	$("#add_"+field).remove();
	$("#"+id_next_add).click( function() { add_remove_field(next_field, 'add') } );
	}
	$("#"+id_next_remove).click( function() { 
	$("tr."+next_field).remove();
	$("#"+id_before_remove).before(
	"<a id='"+id_next_add+"'><img src='/site_media/images/plus.gif' border='0'/></a> "
	);
	$("#"+id_next_add).click( function() { add_remove_field(next_field, 'add') } );
	} 
	);
		$("#"+id_next_field).filestyle({ 
          image: "/site_media/images/browse.gif",
          imageheight : 25,
          imagewidth : 98,
          width : 180
      });
	}
	}
function slug_field()
{
    document.getElementById("id_slug").onchange = function() { this._changed = true; };
    document.getElementById("id_first_name").onkeyup = function() {
        var e = document.getElementById("id_slug");
        if (!e._changed) { e.value = URLify(document.getElementById("id_first_name").value + ' ' + document.getElementById("id_family_name").value, 255); }
    }
    document.getElementById("id_first_name").onblur = function() {
        var e = document.getElementById("id_slug");
        if (!e._changed) { e.value = URLify(document.getElementById("id_first_name").value + ' ' + document.getElementById("id_family_name").value, 255); }
    }
    document.getElementById("id_family_name").onkeyup = function() {
        var e = document.getElementById("id_slug");
        if (!e._changed) { e.value = URLify(document.getElementById("id_first_name").value + ' ' + document.getElementById("id_family_name").value, 255); }
    }
    document.getElementById("id_family_name").onblur = function() {
        var e = document.getElementById("id_slug");
        if (!e._changed) { e.value = URLify(document.getElementById("id_first_name").value + ' ' + document.getElementById("id_family_name").value, 255); }
    }
}
function help_messages() {
		$('ul.message_list').cycle({
			cleartypeNoBg: true,
			fx: "fade",
			speed: 300,
			timeout: 5000,
			pause: 1
		});
		$("input[name!='packet'], select, textarea").bind('mouseover focus', function() {
		$("p.input_txt").remove();
		var help_txt = $("#help_"+this.name).text();
		if (help_txt) {
			$("span.error").hide();
			$("ul.message_list").hide();
			var input_txt = "<p class='input_txt'>"+$("#help_"+this.name).text()+"</p>";
			$("div.contener_right").append(input_txt);
		}
		});
		$("input, select, textarea").mouseout(function() {
			$("ul.message_list").show();
			$("span.error").show();
			$("p.input_txt").remove();
			});
}
function reservation(id_field, y, year, month, day, last_day, next_month) {
	var column = $('#'+id_field);
	var input = $('#'+id_field + ' '+'input');
	if (column.attr('class')=='selected') {
		if ($("input[type='hidden'][value!='']").length==2) {
		
			$("input[type='hidden'][value!='']").each(function() {
				$(this).val('');
			})
			$('td:not(.reserved)').each(function() {
				$(this).removeClass('selected');
			})
		}
		else {
		input.val("");
		column.removeClass('selected');
		}
	}
	else {
	var all_inp = $("input[type='hidden'][value!='']");
	if ((all_inp).length==2 || ((all_inp).length==1 && (all_inp).attr('name').split('_')[3]!=input.attr('name').split('_')[3])) {
		$("input[type='hidden'][value!='']").each(function() {
			$(this).val('');
		})
		$('td:not(.reserved)').each(function() {
			$(this).removeClass('selected');
		})
	}
	var inputString = year+','+month+','+day+','+y
	column.addClass('selected');
	input.val(inputString);
	var selected = $("[type='hidden'][value!='']");
	var tab_id = new Array(3);
	if (selected.length > 1) {
		for (var i = 0; i <= selected.length-1; i++) {
			var tab = $(selected[i]).attr('name').split('_');
			tab_id[i] = tab[1];	
		    tab_id[i+2] = tab[2];
		}
		start_id = parseInt(tab_id[0]);
		end_id = parseInt(tab_id[1]);
		start_month = tab_id[2]
		end_month = tab_id[3]
		delta = end_id - start_id
		for (var i = 1; i <= (end_id-start_id)-1; i++) {
		if ($("#id_"+(start_id+i)+"_"+start_month+"_"+y).attr('class')=='reserved' || $("#id_"+(start_id+i)+"_"+end_month+"_"+y).attr('class')=='reserved'){
		$("input[type='hidden'][value!='']").each(function() {
			$(this).val('');
		})
		$('td').each(function() {
			$(this).removeClass('selected');
		})
		break;
		}
		if(next_month){
		 if ((start_id+i)>last_day) month_ = month;
		 else {
			if (month==1) month_ = 12;
			else month_ = month - 1;
		}
		}
		else {
		if ((start_id+i)>last_day) {
			if (month==12) month_ = 1;
			else month_ = month + 1;
		}
		else month_ = month;
		}
		$("#id_"+(start_id+i)+"_"+month_+"_"+y).addClass('selected');
		}
	}
	}
}

function set_maxlength(len) {
	$("textarea").attr("maxlength", len);
	$("textarea[maxlength]").keyup(function(){
	var max = parseInt($(this).attr("maxlength"));
	if($(this).val().length > max){
	$(this).val($(this).val().substr(0, $(this).attr("maxlength")));
	}
	$(this).parent().find(".charsRemaining").html("You have " + (max - $(this).val().length) + " characters remaining");
	});
}
function check_flash_player() {
	if(navigator.appName=="Microsoft Internet Explorer") {
	var requiredMajorVersion = 8;
	var requiredMinorVersion = 0;
	var requiredRevision = 0;
	var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	if (!(hasReqestedVersion)) {
		$('div.contener_center').prepend("<p class='info_text'>This content requires the Adobe Flash Player. <a href='http://www.adobe.com/go/getflash/'>Get Flash</a></p>");
		return false;
	}
	return true;
	}
	return true;
}

function clear_field(){
	var lang = $("div.language").text();
	if (lang == 'pl') var text = "Kliknij tutaj, aby napisać rozbudowaną historię swojego anioła.";
	else var text = "Click here to write a detailed description of your Angels life.";
	$("#id_description").click( function() { 
		if ($("#id_description").val()==text){
			$("#id_description").val("");
		}
	});
	$("#id_description").blur( function() { 
		if ($("#id_description").val()=="" ){$("#id_description").val(text);}
	} );
}

function display_text(){
		$("#Simple, #Standard, #Extended, #Individual").mouseover(function() {
		$("ul.message_list").hide();
		var lang = $("div.language").text();
		if (lang == 'pl') {
			if ($(this).attr('id')!="Simple"){
				var text = "Zobacz na podglądzie <br />wybraną grafikę.";
			}
			else {
				var text = "Pakiet podstawowy <br />oferuje 1 grafikę: <br />\"Błękitne niebo\"";
			}
		}
		else {
			if ($(this).attr('id')!="Simple"){
				var text = "Click to preview a sample <br />of this package.";
			}
			else {
				var text = "Click to preview a sample <br />of this package.";
			}
		}
		$("div.contener_right").append("<p class='input_txt'>" + text + "</p>");
		});
		$("#Simple, #Standard, #Extended, #Individual").mouseout(function() {
		$("p.input_txt").remove();
		$("ul.message_list").show();
		});
}

function display_text1(){
		var lang = $("div.language").text();
		if (lang == 'pl') {
			var input_txt_later = "<p class='input_txt'>Możesz aktywować swoją stronę poźniej z panelu administracynego.</p>";
			var input_txt_activate = "<p class='input_txt'>Twoja strona będzie widoczna dla użytkowników i odwiedzających.</p>";

		}
		else {
			var input_txt_later = "<p class='input_txt'>You may further edit and activate your memorial later from your administration panel.</p>";
			var input_txt_activate = "<p class='input_txt'>Your memorial will be visible to all users and visitors right away.</p>";
		
		}
		$("#later").mouseover(function(){
		$("ul.message_list").hide();
		$("span.error").hide();
		$("div.contener_right").append(input_txt_later);
		})
		$("#activate").mouseover(function(){
		$("ul.message_list").hide();
		$("span.error").hide();
		$("div.contener_right").append(input_txt_activate);
		})	
}

/* load with page */
$(document).ready(function() 
    { 
	if (($('#id_birthday').length > 0) || ($('#id_angel_day').length > 0) || ($('#id_event_day').length > 0)) {
		var country = $("div.country").text();
		$.datepick.setDefaults($.datepick.regional[country]);
	}
    if ($('#id_birthday').length > 0) $('#id_birthday').datepick();
	if ($('#id_angel_day').length > 0) $('#id_angel_day').datepick();
	if ($('#id_event_date').length > 0) $('#id_event_date').datepick();
	$("ul.errorlist").prevAll("input, textarea").css("border", "1px solid red")
    } 
); 
