var modified = new Array();
var modifiedMsg = 'Tietoja muutettu <span class="ui-icon ui-icon-alert"></span>';


$(document).ready(function()
{
	$('form').not('.no-trigger').children().change(function()
	{
		id = $(this).parents('form').attr('id');
		section = id.substr(5, id.length);
		ajaxLoad(section, modifiedMsg);
		setModified(section); 
	}); 
	
	$('#etsi #hakuteksti')
		.click(function() {
			$(this).val('');
		})
		.blur(function() {
			if($(this).val() == '')
			{
				$(this).val('Hae tuotteita');
			}
		});
	
	$('a.external').attr('target', '_blank');
	
	$('table.colors tbody tr:not([th]):odd').addClass('odd');
	$('table.colors tbody tr:not([th]):even').addClass('even');
	
	matchHeights();
	makeButtons();
	
});

function makeButtons()
{
	$('.button-login').button({
		icons: {
			primary: 'ui-icon-key'
		}
	});
	$('.button-window').button({
		icons: {
			primary: 'ui-icon-newwin'
		}
	});
	$('.button-gear').button({
		icons: {
			primary: 'ui-icon-gear'
		}
	});
	$('.button-link').button({
		icons: {
			primary: 'ui-icon-link'
		}
	});
	$('.button-new').button({
		icons: {
			primary: 'ui-icon-plus'
		}
	});
	$('.button-folder-open').button({
		icons: {
			primary: 'ui-icon-folder-open'
		}
	});
	$('.button-folder-collapsed').button({
		icons: {
			primary: 'ui-icon-folder-collapsed'
		}
	});
	$('.button-edit').button({
		icons: {
			primary: 'ui-icon-pencil'
		}
	});
	$('.button-arrange').button({
		icons: {
			primary: 'ui-icon-arrowthick-2-n-s'
		}
	});
	$('.button-archive').button({
		icons: {
			primary: 'ui-icon-calendar'
		}
	});
	$('.button-back').button({
		icons: {
			primary: 'ui-icon-arrowthick-1-w'
		}
	});
	$('.button-check').button({
		icons: {
			primary: 'ui-icon-check'
		}
	});
	$('.button-category').button({
		icons: {
			primary: 'ui-icon-note'
		}
	});
	$('.button-cart').button({
		icons: {
			primary: 'ui-icon-cart'
		}
	});
	$('.button-delete').button({
		icons: {
			primary: 'ui-icon-trash'
		}
	});
	$('.button-cancel').button({
		icons: {
			primary: 'ui-icon-cancel'
		}
	});
	$('.button-search').button({
		icons: {
			primary: 'ui-icon-search'
		}
	});
	$('.button-save').button({
		icons: {
			primary: 'ui-icon-disk'
		}
	});
	$('.button-sort').button({
		icons: {
			primary: 'ui-icon-shuffle'
		}
	});
	$('.button-cart').button({
		icons: {
			primary: 'ui-icon-cart'
		}
	});
	$('.button-person').button({
		icons: {
			primary: 'ui-icon-person'
		}
	});
	$('.button-print').button({
		icons: {
			primary: 'ui-icon-print'
		}
	});
	$('.button').button();
	
	// Sivutuksien custom-napit
	$('.pagination-button-next a').button({
		text: false,
		icons: {
			primary: 'ui-icon-triangle-1-e'
		}
	});
	$('.pagination-button-prev a').button({
		text: false,
		icons: {
			primary: 'ui-icon-triangle-1-w'
		}
	});
	$('.pagination-button-first a').button({
		text: false,
		icons: {
			primary: 'ui-icon-seek-start'
		}
	});
	$('.pagination-button-last a').button({
		text: false,
		icons: {
			primary: 'ui-icon-seek-end'
		}
	});
	
	// Napit ilman tekstiä
	$('.button-no-text').each(function(){
		$(this).button("option", "text", false)
	});
}

function setHeights(_target, _height)
{
	var height = 0;
	targets = $(_target).children("div.ui-match-height > div:not(.ui-clear)");
	targets.each(function() {
		$(this).css('height', '');
		theight = $(this).height();
		if(theight > height)
		{
			height = theight;
		}
	});
	targets.each(function() {
		$(this).css('height', height+'px');
		height2 = 0;
		$(this).children("div").each(function() {
			height2 = height2 + $(this).outerHeight(true);
		});
		//alert(height +' '+height2);
		$(this).children("div.ui-footer").each(function() {
			$(this).css('bottom', '');
		});
		if(height > height2)
		{
			movement = height - height2;
			$(this).children("div.ui-footer").each(function() {
				$(this).css('bottom', '-'+movement+'px')
			});	
		}
	}); 
}

function matchHeights(target) {
	if(target != undefined) 
	{
		target = $('#'+target);
		height = target.outerHeight();
		setHeights(target, height);
	}
	else
	{
		$('div.ui-match-height').each(function() {
			target = $(this);
			height = target.outerHeight();
			setHeights(target, height)
		});
	}
}

function processAjaxResponse ( data ) { 
    // 'data' is the json object returned from the server 
	switch (data.type)
	{
		case 'error-multi' :
			$('#dialog').html("");
			jQuery.each(data.errors, function(i, val) {
				$('#dialog').append("<p>"+val+"</p>");
			});
			$('#dialog').dialog({
				modal: true,
				title: "Virheitä!",
				resizable: false,
				buttons: {
					Ok: function() {
						$(this).dialog('destroy');
					}
				},
				close: function() {
					$(this).dialog('destroy');
				}
			});
			return false;
		break;
		case 'error' :
			// 'data' is the json object returned from the server 
			//alert(data.message); 
			$('#dialog').html(data.message);
			$('#dialog').dialog({
				modal: true,
				title: "Virhe!",
				resizable: false,
				buttons: {
					Ok: function() {
						$(this).dialog('destroy');
					}
				},
				close: function() {
					$(this).dialog('destroy');
				}
			});
			return false;
		break;
		case 'error-login':
			$('#dialog').html('Sessiosi on päättynyt.<br />Ole hyvä ja kirjaudu sisään uudelleen<br />ennen kun klikkaat Ok.');
			$('#dialog').dialog(
			{
				modal: true,
				title: 'Sessio päättynyt!',
				resizable: false,
				buttons:
				{
					Ok: function()
					{
						$(this).dialog('destroy');
						window.location.reload();
					}
				},
				close: function()
				{
					$(this).dialog('destroy');
					window.location.reload();
				}
			});
			return false;
		break;
		case 'allok' :
			return true;
		break;
	}
}

function errorHandler ( msg ) { 
    // 'data' is the json object returned from the server 
    //alert(data.message); 
	$('#dialog').html(msg);
	$('#dialog').dialog({
		modal: true,
		resizable: false,
		title: "Virhe!",
		buttons: {
			Ok: function() {
				$(this).dialog('destroy');
			}
		},
		close: function() {
			$(this).dialog('destroy');
		}
	});
}

function ajaxLoad(section, message ) {
	var x = section;
	$('#tab_loading_'+x).html('*');
	$('#loading_'+x).html(message);
	$('#loading_'+x).fadeIn('fast');
}

function ajaxLoadOff(section, message) {
	var x = section;
	$('#tab_loading_'+x).html('');
	$('#loading_'+x).html(message);
	$('#loading_'+x).show().delay(5000).fadeOut('fast', function() {
		loc = jQuery.inArray(x, modified);
		if(loc != -1) {
			ajaxLoad(x, modifiedMsg);
		}
	});
}
function setModified(section) { 
	loc = jQuery.inArray(section, modified);
	if(loc == -1) {
		modified.push(section);
	}
}

function unsetModified(section) { 
	loc = jQuery.inArray(section, modified);
	if(loc != -1) {
		modified.splice(loc, 1);
	}
}

function confirmExit() {
	if(modified.length > 0) {
		msg = "Tietoja on muutettu ja niitä ei ole tallennettu! Oletko varma että haluat poistua sivulta?";
		return msg;
	}
	/* else if (swfu.getStats().files_queued > 0) {
		return "Sinulla on kuvien lataus kesken! Oletko varma että haluat keskeyttää latauksen ja poistua sivulta?";
	} */
}

function getCookie(c_name)
{
	if(document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + '=');
		if(c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if(c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return '';
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ '=' +escape(value)+
	((expiredays==null) ? '' : ';expires='+exdate.toGMTString())+';path=/';
}

/* jQuery UI Datepicker */
/* Finnish initialisation for the jQuery UI date picker plugin. */
/* Written by Harri Kilpiö (harrikilpio@gmail.com). */
jQuery(function($){
    $.datepicker.regional['fi'] = {
		minDate: 0,
		closeText: 'Sulje',
		prevText: '&laquo; Edellinen',
		nextText: 'Seuraava &raquo;',
		currentText: 'T&auml;n&auml;&auml;n',
        monthNames: ['Tammikuu','Helmikuu','Maaliskuu','Huhtikuu','Toukokuu','Kes&auml;kuu',
        'Hein&auml;kuu','Elokuu','Syyskuu','Lokakuu','Marraskuu','Joulukuu'],
        monthNamesShort: ['Tammi','Helmi','Maalis','Huhti','Touko','Kes&auml;',
        'Hein&auml;','Elo','Syys','Loka','Marras','Joulu'],
		dayNamesShort: ['Su','Ma','Ti','Ke','To','Pe','La'],
		dayNames: ['Sunnuntai','Maanantai','Tiistai','Keskiviikko','Torstai','Perjantai','Lauantai'],
		dayNamesMin: ['Su','Ma','Ti','Ke','To','Pe','La'],
		weekHeader: 'Vk',
        dateFormat: 'd.m.yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''
		};
    $.datepicker.setDefaults($.datepicker.regional['fi']);
});

// Return a helper with preserved width of cells
var fixHelper = function(e, ui) {
	ui.children().each(function() {
		$(this).width($(this).width());
	});
	return ui;
};
