var defaultVal = '';
var pos=0;

if (typeof dmsone == "undefined") var dmsone = new Object();

dmsone = {
    
    initJHelper: function(){
        $(".tipp").jHelperTip({
    		trigger: "hover", 
    		source: "attribute", 
    		attrName: "name", 
    		opacity: 0.8, 
    		autoClose:true
    	});    
    },
    
    sendFormAndJump:function(form_id){
	   
        dmsone.bgprocessLock();       
		dmsone.removeAllError();
		$('#' + form_id).ajaxSubmit({
			success: function(response){
				var res = response.split('##@@##');
				if(res[0] != 'error'){	
                    document.location.href = res[1];
				}else{
					dmsone.alertAndHighlight(res[1]);
				}
			}
		});

	
	},
    
    sendFormAndMessage:function(form_id){
	   
        dmsone.bgprocessLock();       
		dmsone.removeAllError();
		$('#' + form_id).ajaxSubmit({
			success: function(response){
				var res = response.split('##@@##');
				if(res[0] != 'error'){	
                    //dmsone.emptyFields('clearable');
                    dmsone.message(res[1]);
                    
				}else{
					dmsone.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(){
	   
        dmsone.bgprocessLock();       
		dmsone.removeAllError();
		$('#searchForm').ajaxSubmit({
			success: function(response){
				var res = response.split('##@@##');
				if(res[0] != 'error'){	
                    document.location.href= '/Search/' + res[1];
				}else{
					dmsone.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');
				}
			}
		});
	},
    
    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');
				}
			}
		});
        

	}
	
}
