var defaultVal = '';
var pos=0;

if (typeof skeleton == "undefined") var skeleton = new Object();

skeleton = {
    
	inArray: function(elem, array){
		for(var i=0;i<array.length;i++){
			if(array[i]==elem){return i;}
		}
		
		return -1;
	},

	initJHelper: function(){
	    $(".tipp").jHelperTip({
			trigger: "hover", 
			source: "attribute", 
			attrName: "name", 
			opacity: 0.8, 
			autoClose:true
		});    
	},
	
	sendFormAndJump:function(form_id){
	   
	    skeleton.bgprocessLock();       
		skeleton.removeAllError();
		$('#' + form_id).ajaxSubmit({
			success: function(response){
				var res = response.split('##@@##');
				if(res[0] != 'error'){	
	                document.location.href = res[1];
				}else{
					skeleton.alertAndHighlight(res[1]);
				}
			}
		});
	
	
	},
	
	sendFormAndMessage:function(form_id){
	   
	    skeleton.bgprocessLock();       
		skeleton.removeAllError();
		$('#' + form_id).ajaxSubmit({
			success: function(response){
				var res = response.split('##@@##');
				if(res[0] != 'error'){	
	                skeleton.emptyFields('clearable');
	                skeleton.message(res[1]);
	                
				}else{
					skeleton.removeAllError();
					skeleton.alertAndHighlight(res[1]);
				}
			}
		});
	
	
	},
	
	sendFormAndAjax:function(form_id){
		
		skeleton.bgprocessLock();       
		skeleton.removeAllError();
		$('#' + form_id).ajaxSubmit({
			success: function(response){
				var res = response.split('##@@##');
				if(res[0] == 'ok'){	
					skeleton.emptyFields('clearable');
	                skeleton.message(res[1]);
					
				}else if(res[0] == 'okreload'){	
					skeleton.emptyFields('clearable');
	                skeleton.messageAndReload(res[1]);
					
				}else if(res[0] == 'okjump'){	
					var rez = res[1];
					var newres = rez.split('##XXX##');
					
					skeleton.emptyFields('clearable');
	                skeleton.messageAndJump(newres[0], newres[1]);
					
				}else if(res[0] == 'jump'){	
					var targ = res[1].split('#@#');
					$(targ[0]).load(targ[1]);
					
				}else{
					skeleton.removeAllError();
					skeleton.alertAndHighlight(res[1]);
				}
			}
		});
		
		
	},
	
	initGaleries: function(relation, imageTitle){
		$("a[rel=" + relation + "]").fancybox({			
			'titlePosition' 	: 'inside',
			'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
	            if ( title.length > 0 ){
	                vistitle = ' (' + title + ')';
	            }else{
	                vistitle = '';
	            }
				return '<span id="fancybox-title-inside">' + imageTitle + ' ' +  (currentIndex + 1) + ' / ' + currentArray.length + vistitle + '</span>';
			}
		});
	},
	
	initFancy: function(){
		$(".fancy").each(function(){
			var height=$(this).attr('height');
			if(height == undefined){height = '80%';}
			else{ height = parseInt(height); }
			
			var width=$(this).attr('width');
			if(width == undefined){width = 600;}
			else{ width = parseInt(width); }
			
			$(this).fancybox({
				'width'			: width,
				'height'		: height,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'autoScale'		: true,
				'type'			: 'iframe'
			});
		})		
		
	},
	
	search:function(){
	   
	    skeleton.bgprocessLock();       
		skeleton.removeAllError();
		$('#searchForm').ajaxSubmit({
			success: function(response){
				var res = response.split('##@@##');
				if(res[0] != 'error'){	
	                document.location.href= '/Search/' + res[1];
				}else{
					skeleton.alertAndHighlight(res[1]);
				}
			}
		});
	
	
	},
	
	emptyFields:function(clearable){
	    $('.' + clearable).val('');
	},
	
	bgprocessLock: function(){
	    
	    load_w = $('body').width();
	    //load_h = $('wrapper').height() + 500;
	    //loader_styles = 'filter:alpha(opacity=50); opacity: 0.5; -moz-opacity:0.5; background-color: white; position: fixed; top: 0px; width: '+load_w+'px; height: '+load_h+'px;';
	    loader_styles = 'width: '+load_w+'px; height: 3000px; position: fixed; top: 0px';
	    
	    $('body').append('<div class="ui-widget-overlay" id="loading" style="'+loader_styles+'"></div>');
	    document.body.style.cursor = "wait";
	
	    $("body")
		.ajaxComplete(function(){
			$('#loading').remove();
	        document.body.style.cursor = "default";
		});
	    
	    
	},
	
	removeAllError: function(){
		$('.errorMessage').removeClass('error');
	},
	
	message: function(title){
		var items = title.split('@@##@@');
		var html = '';
		for(var i=0;i<items.length;i++){
			html += '<p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span><strong>' + items[i] + '</strong></p>';
		}
		$("#dialog").html(html);
		$("#dialog").dialog({
			modal: true,
			buttons: {
				ok: function() {
					$(this).dialog('close');
				}
			}
		});
	},
	
	messageAndReload: function(title){
		var items = title.split('@@##@@');
		var html = '';
		for(var i=0;i<items.length;i++){
			html += '<p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span><strong>' + items[i] + '</strong></p>';
		}
		$("#dialog").html(html);
		$("#dialog").dialog({
			modal: true,
			buttons: {
				ok: function() {
					$(this).dialog('close');
					document.location.reload();
				}
			}
		});
	},
	
	messageAndJump: function(title, target){
		var items = title.split('@@##@@');
		var html = '';
		for(var i=0;i<items.length;i++){
			html += '<p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span><strong>' + items[i] + '</strong></p>';
		}
		$("#dialog").html(html);
		$("#dialog").dialog({
			modal: true,
			buttons: {
				ok: function() {
					//$(this).dialog('close');
					document.location.href = target;
				}
			}
		});
	},
	
	alertAndHighlight: function(title){
		var items = title.split('@@##@@');
		var html = '';
		for(var i=0;i<items.length;i++){
			var item = items[i].split('#@#');			
			$('#' + item[0]).addClass('error');
			html += '<div class="ui-widget"><div style="padding: 0pt 0.7em;" class="ui-state-error ui-corner-all"> <p><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-alert"></span><strong>' + item[1] + '</strong></p></div></div>';
		}
		$("#dialog").html(html);
		$("#dialog").dialog({
			modal: true,
			buttons: {
				ok: function() {
					$(this).dialog('close');
				}
			}
		});
	    
	
	}
}
