﻿var editorCounter = 0;
var currentItemId = null;

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, '');
}

jQuery.fn.outer = function()
{ 
	return $( $('<div></div>').html(this.clone()) ).html();
}

$(function()
{
	// png
	$.ifixpng('/bazment/deja/image/pixel.gif');
	$('.png').ifixpng();
	
	// dialogs
	$('.dialog').hide();
	
	// accordions (one per page)
	/*$('#accordion').find('p.toggler > a.more').click(function()
	{
		$(this).parent().siblings("a.toggler:first").click();
		$(this).hide('slide', { direction: 'down' });
	});*/
	
	/*$('#accordion').accordion(
	{
		active: false,
		header: 'a.toggler',
		change: function(event, ui)
		{
			ui.oldHeader.parent().children("p.toggler").children("a.more").show("slide", { direction: "up" });
		}
	});*/
	
	/*$('#accordion').find('.event-content').each(function(index)
	{
		if ($(this).text().trim().length == 0)
			$(this).siblings('p.toggler').hide();
	});
	
	try
	{
		if (location.hash)
			$(location.hash + '-toggler').click();
		else
			$('#accordion').find('p.toggler:first > a.more').click();
	}
	catch (e)
	{
	
	}*/
});

function showModal(ctl, id, title, arg)
{		
	if (!arg)
		arg = '';
		
	$(id).dialog('destroy');
	
	$(id).dialog({
			draggable: true,
			resizable: false,
			bgiframe: true,
			width: 550,
			height: 400,
			modal: false,
			title: title,
			buttons: {
				'Avbryt': function() {
					$(this).dialog('close');
				},
				'Spara': function() {
					__doPostBack(ctl, arg);
				}
			}
		});
		
	$(id).dialog('open');
}

function editNews()
{
	var clear = (currentItemId == null);

	if (clear)
	{
		var date = new Date();
		var month = date.getMonth() + 1;
		var day = date.getDate();
		
		$('#ctl00_NewsList_txtNewsListDialogHeading').val('');
		$('#ctl00_NewsList_txtNewsListDialogDate').val(date.getFullYear() + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day) : day));
		
		try
		{
			FCKeditorAPI.GetInstance('ctl00_NewsList_ctl02').SetHTML('');
			FCKeditorAPI.GetInstance('ctl00_NewsList_ctl03').SetHTML('');
			FCKeditorAPI.GetInstance('ctl00_NewsList_ctl04').SetHTML('');
		}
		catch (e)
		{
		
		}
	}
	else
	{
		var news = $('#' + currentItemId);
		
		$('#ctl00_NewsList_txtNewsListDialogHeading').val(news.children('h2.news-heading').text());
		$('#ctl00_NewsList_txtNewsListDialogDate').val(news.children('h3.news-date').text());
		
		try
		{
			FCKeditorAPI.GetInstance('ctl00_NewsList_ctl02').SetHTML(news.children('img.news-image').outer());
			FCKeditorAPI.GetInstance('ctl00_NewsList_ctl03').SetHTML(news.children('div.news-preamble').html());
			FCKeditorAPI.GetInstance('ctl00_NewsList_ctl04').SetHTML($('#' + currentItemId + ' > div.news-content').html());
		}
		catch (e)
		{
		
		}
	}
}

function editLink()
{
	var clear = (currentItemId == null);
	
	if (clear)
	{
		$('#ctl00_LinkList_txtLinkListDialogHeading').val('');
		$('#ctl00_LinkList_txtLinkListDialogUrl').val('');
		
		try { FCKeditorAPI.GetInstance('ctl00_LinkList_ctl02').SetHTML(''); } catch (e) { }
	}
	else
	{
		var link = $('#' + currentItemId);
		
		$('#ctl00_LinkList_txtLinkListDialogHeading').val(link.children('h3.link-heading').text());
		$('#ctl00_LinkList_txtLinkListDialogUrl').val(link.children('a.link-url').text());
		
		try { FCKeditorAPI.GetInstance('ctl00_LinkList_ctl02').SetHTML(link.children('div.link-content').html()); } catch (e) { }
	}
}

function editFaq()
{
	var clear = (currentItemId == null);
	
	if (clear)
	{
		$('#ctl00_FaqList_txtFaqListDialogHeading').val('');
		try { FCKeditorAPI.GetInstance('ctl00_FaqList_ctl02').SetHTML(''); } catch (e) { }
	}
	else
	{
		var faq = $('#' + currentItemId);
		
		$('#ctl00_FaqList_txtFaqListDialogHeading').val(faq.children('h3.faq-heading').text());
		try { FCKeditorAPI.GetInstance('ctl00_FaqList_ctl02').SetHTML(faq.find('div.faq-content > span').html()); } catch (e) { }
	}
}

function editContact()
{
	var clear = (currentItemId == null);
	
	if (clear)
	{
		$('#ctl00_Contact_txtContactListDialogName').val('');
		$('#ctl00_Contact_txtContactListDialogTitle').val('');
		$('#ctl00_Contact_txtContactListDialogEmail').val('');
		$('#ctl00_Contact_txtContactListDialogPhone').val('');
		$('#ctl00_Contact_txtContactListDialogCell').val('');
		
		try { FCKeditorAPI.GetInstance('ctl00_Contact_ctl02').SetHTML(''); } catch (e) { }
	}
	else
	{
		var contact = $('#' + currentItemId + ' > .list-content');
		
		$('#ctl00_Contact_txtContactListDialogName').val(contact.children('h2.contact-name').text());
		$('#ctl00_Contact_txtContactListDialogTitle').val(contact.children('h3.contact-title').text());
		$('#ctl00_Contact_txtContactListDialogEmail').val($('#' + currentItemId + ' > .list-content > p > a.contact-email').text());
		$('#ctl00_Contact_txtContactListDialogPhone').val($('#' + currentItemId + ' > .list-content > p > span.contact-phone').text());
		$('#ctl00_Contact_txtContactListDialogCell').val($('#' + currentItemId + ' > .list-content > p > span.contact-cell').text());
		
		try { FCKeditorAPI.GetInstance('ctl00_Contact_ctl02').SetHTML($('#' + currentItemId + ' > img.contact-image').outer()); } catch (e) { }
	}
}

function editCommissioner()
{
	var clear = (currentItemId == null);
	
	if (clear)
	{
		$('#ctl00_Commissioners_txtCommissionerListDialogName').val('');
		$('#ctl00_Commissioners_txtCommissionerListDialogTitle').val('');
		
		try { FCKeditorAPI.GetInstance('ctl00_Commissioners_ctl05').SetHTML(''); } catch (e) { }
	}
	else
	{
		var commissioner = $('#' + currentItemId);
		
		$('#ctl00_Commissioners_txtCommissionerListDialogName').val(commissioner.children('h2.commissioner-name').text());
		$('#ctl00_Commissioners_txtCommissionerListDialogTitle').val(commissioner.children('h3.commissioner-title').text());
		
		try { FCKeditorAPI.GetInstance('ctl00_Commissioners_ctl05').SetHTML(commissioner.children('img.commissioner-image').outer()); } catch (e) { }
	}
}

function editCalendar()
{
	var clear = (currentItemId == null);
	
	if (clear)
	{
		var date = new Date();
		var month = date.getMonth() + 1;
		var day = date.getDate();
		
		$('#ctl00_Calendar_txtCalendarDialogDate').val(date.getFullYear() + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day) : day));
		$('#ctl00_Calendar_txtCalendarDialogLocation').val('');
		$('#ctl00_Calendar_txtCalendarDialogCity').val('');
		$('#ctl00_Calendar_txtCalendarDialogHeading').val('');
		$('#ctl00_Calendar_chkCalendarDialogExternal').removeAttr('checked');		
		
		try
		{
			FCKeditorAPI.GetInstance('ctl00_Calendar_ctl02').SetHTML('');
			FCKeditorAPI.GetInstance('ctl00_Calendar_ctl03').SetHTML('');
		}
		catch (e)
		{
		
		}
	}
	else
	{
		var event = $('#' + currentItemId);
		
		$('#ctl00_Calendar_txtCalendarDialogDate').val(event.find('input.event-date-raw').val());
		$('#ctl00_Calendar_txtCalendarDialogHeading').val(event.find('h2.event-heading').text());
		$('#ctl00_Calendar_txtCalendarDialogLocation').val(event.find('span.event-location').text());
		$('#ctl00_Calendar_txtCalendarDialogCity').val(event.find('span.event-city').text());
		
		if (event.find('div.tip').length > 0)
			$('#ctl00_Calendar_chkCalendarDialogExternal').attr('checked', 'checked');
		
		try
		{
			FCKeditorAPI.GetInstance('ctl00_Calendar_ctl02').SetHTML(event.find('div.event-preamble').html());
			FCKeditorAPI.GetInstance('ctl00_Calendar_ctl03').SetHTML(event.find('div.event-content').html());
		}
		catch (e)
		{
		
		}			
	}
}

function resetContactForm()
{
	$('#ctl00_Contact_txtName').val('');
	$('#ctl00_Contact_txtMail').val('');
	$('#ctl00_Contact_txtText').val('');
}

function validateContactForm()
{
	var regex = new RegExp('^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$');
	var valid = false;
	
	if ($('#ctl00_Contact_txtName').val().trim().length > 0 && regex.test($('#ctl00_Contact_txtMail').val()) && $('#ctl00_Contact_txtText').val().trim().length > 0)
		valid = true;
		
	if (!valid)
		$('#lblError').show();
	else
		$('#lblError').hide();
		
	return valid;
}


var Deja = {
    init: function() {
        Deja.prepareContainers();
    },
    prepareContainers: function() {
        var i = 0;
        $('.news-content,.event-content').each(function() {
            if (i++ > 0) {
                $(this).hide();
            } else {
                $(this).next().find('a').hide();
            }
            if ($(this).html().length < 5)
                $(this).next().find('a').hide();
        });
        $('#accordion .segment-content .toggler a').click(function() {
            $(this).hide();
            Deja.hideOpenContainer(this);
            return false;
        });
    },
    hideOpenContainer: function(tag) {
        $('.news-content:visible,.event-content:visible').slideToggle("slow", function() {
            if ($(this).html().length > 0)

                $(this).next().find('a').show();
            Deja.showClosedContainer(tag);

        });
    },
    showClosedContainer: function(tag) {
        $('html').animate({ scrollTop: $(tag).parent().parent().offset().top }, 1000);
        $(tag).parent().prev().slideToggle("slow");
    }
};
$(document).ready(function() {
    Deja.init();
});


