// JavaScript Document

var linkable_modules = false;

$.extend({
    keys:    function(obj){
        var a = [];
        $.each(obj, function(k){ a.push(k) });
        return a;
    }
})

function deleteFromArray(element,arr)
{
	var newArray = jQuery.grep(arr, function (a) {
		return a != element; }
	);
	return newArray;
}

function intersectArrays(arr1,arr2)
{
	var newArray = jQuery.grep(arr1, function (a) { return jQuery.inArray(a,arr2) != -1; });
	return newArray;
}

function equals(compareFrom, compareTo) {
  if (!compareTo || compareFrom.length != compareTo.length) {
    return false;
  }
  for (var i = 0; i < compareFrom.length; ++i) {
    if (compareFrom[i] !== compareTo[i]) {
      return false;
    }
  }
  return true;
}

function XOR(a,b)
{
  return ( a || b ) && !( a && b );
}

function debuglog(msg)
{
	if(typeof console != "undefined" && typeof console.log == "function")
	{
		console.log(msg);
	}
}

function createToggleEntry(elements,hideNext,callback)
{
	$(elements).addClass('ui-accordion-header ui-helper-reset ui-state-default ui-corner-all').prepend('<span class="ui-icon ui-icon-triangle-1-e"></span>');
	if(hideNext)
	{
		$(elements).next().hide();
	}
	if(callback)
	{
		$(elements).click(callback);
	}
	
	$(elements).click(function(){
		$(this).find('span').toggleClass('ui-icon-triangle-1-e').toggleClass('ui-icon-triangle-1-s');
		$(this).next().toggle();
	}).bind('mouseover', function() {
		$(this).addClass('ui-state-hover');
	}).bind('mouseout', function() {
		$(this).removeClass('ui-state-hover');
	});
}

function createEditor(elements)
{
	if(!linkable_modules)
	{
		linkable_modules = [
			['pages','Seitenverwaltung'],
            ['forms','Formulare'],
            ['users','Benutzerverwaltung'],
            ['news','News']
		]
	}
	return $(elements).ckeditor({
		contentsCss : [
			pathToDir + '/templates/' + templatedir + '/css/editor.css',
			'http://fonts.googleapis.com/css?family=Josefin+Sans:100,100italic,300,300italic,400,400italic,600,600italic,700,700italic'
		],
		toolbar : [
		           ['Maximize','Source','-','Undo','Redo'],
		           ['Cut','Copy','Paste','PasteText','PasteFromWord',],
		           ['Find','Replace','-','SelectAll','RemoveFormat'],
		           ['Styles','Format'],
		           '/',
		           ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
		           ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote','CreateDiv'],
		           ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
		           ['Link','Unlink','Anchor'],
		           ['Image','Table','SpecialChar']
		       ]
	});
}

function saveStateToSession(){
	 if( typeof window.sessionStorage != "undefined")
		 window.sessionStorage.setItem('state', JSON.stringify(jQuery.deparam.fragment()));
}

function getStateFromSession() {
	if( typeof window.localStorage != "undefined" && typeof $.bbq != "undefined")
	{
		var retrievedLanguage = window.sessionStorage.getItem('language');
		if(retrievedLanguage != current_lang)
		{
			window.sessionStorage.setItem('language',current_lang);
			var retrievedState = window.sessionStorage.getItem('state');
			$.bbq.pushState(JSON.parse(retrievedState));
			sessionStorage.removeItem('state');
			debuglog(retrievedState);
		}
		
	}
}

function recolor(elements) {
	$(elements).each(function(index){
		$(this).removeClass('row_odd row_even').addClass(index % 2 ? 'row_even' : 'row_odd');
	})
}

function ajaxError(xhr, ajaxOptions, thrownError)
{
	showDialog('AJAX-ERROR','Status : ' + xhr.status + '<br/>thrownError : ' + thrownError,'alert');
};

function cloneObj(oldObj) {
  var newObj = (oldObj instanceof Array) ? [] : {};
  for (i in oldObj) {
    if (i == 'clone') continue;
    if (oldObj[i] && typeof oldObj[i] == "object") {
      newObj[i] = cloneObj(oldObj[i]);
    } else if(oldObj[i] && typeof oldObj[i] != "function"){
		newObj[i] = oldObj[i];
	}
  } return newObj;
};

function showDebugOutput(){
	showDialog('DebugOutput','<pre>' + $('#debugOutput').html() + '</pre>',false,{
		modal: true,
		resizeable:true,
		height: $(window).height() - 90,
		width: $(window).width() - 90
	});
}

function showDialog(title,text,icon,options){
	$('#dialog').attr('title',title);
	$('#dialog-text').html(text);
	$('#dialog-icon').hide()
	if(icon)
		$('#dialog-icon').show().attr('class','ui-icon ui-icon-' + icon);	
	var options = $.extend({
		modal: true,
		resizeable:false
	},options);
	$('#dialog').dialog(options);
}


function checkDelete(modul,languageVar,callback,data){
	var buttons = new Object();
	buttons[language_data['core']['no']] = function(){
		$('#dialog').dialog('destroy');
	};
	buttons[language_data['core']['yes']] = function(){
		callback(data)
		$('#dialog').dialog('destroy');
	};
	var options = {
			modal: true,
			resizeable:false,
			buttons : buttons
	};
	showDialog(language_data[modul][languageVar + 'Title'],language_data[modul][languageVar + 'Msg'],'question',options);
	event.stopPropagation();
}

/* css tricks */	
function css(el, prop) {
    return parseInt($.css(el[0], prop)) || 0;
};
function width(el) {
    return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
};
function height(el) {
    return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
};
