var myDialog ; 			// Dialog Reference
var mergeDialog ; 		// Merge reference
var dialogexist = 0; 	// Dialog set check
var mdialogexist = 0;	// Merge set check

// Sync an attached account
function app_update(myUrl, img){
	var orig = img.src;
   img.src = siteRoot +'/img/ajax-loader.gif';
   
   dojo.xhrGet({
	    url: myUrl,
	    handleAs: "text",
	    load: function(data){
	   		img.src = orig;
	   		//Check for updates
			checkSync();
 		}
	});
}
//Check current sync status
function checkSync(){
	   dojo.xhrGet({
		    url: siteRoot+"/sync/check/",
		    handleAs:"text",
		    preventCache: true,
		    load: function(data){
		        if(data == 1){
		        	if(mdialogexist == 0){
			        	mergeDialog = new dijit.Dialog({
							title: "New data found",
							style: "width: 600px",
							href: siteRoot+"/sync/load/",
							preventCache : true,
						});
		        	} mdialogexist = 1;
		        	
		        	mergeDialog.show();
		        }
		    }
		});
}
//Select options in merge dialogs
function mergeSelect(itm){
	 var itm_id = dojo.attr(itm, 'id');
	 var range = dojo.attr(itm, 'range')
	 var other = itm_id.substring(0, itm_id.length-1);
	 
	 dojo.byId(other+0).value = itm.innerHTML;
  
	 for(i=1; i <= range; i++ ){
		 dojo.removeClass(other+i, "mergeSelected");
	 }  
	 
	  dojo.addClass(itm.id, "mergeSelected");
}
//Display a dialog
function showDialog(name, url, zone){
	if(!zone){ var zone = '' };
	if(dialogexist == 0){
		myDialog = new dijit.Dialog({
			title: "Bla",
			style: "width: 400px; text-align: left;"
		});
		dialogexist = 1;
	}
	myDialog.attr('title', name);
	myDialog.attr('href', url);
	myDialog.attr('zone', zone);
	myDialog.show();
	return false;
}   
var dbg;

//Save a Dialogs form sumbissions
function dialogSave(cform){

	if(!cform.validate()){return false;}

	dojo.xhrPost({
	    form: cform.attr('id'),
	    load: function(data, ioArgs){
	    	if(data == ''){
	    		myDialog.hide();
	    		refreshContent();
			}else{
				myDialog.attr('content', data);
			}
	    	checkFlash();
	    },
	    error: function(err, ioArgs){
	        // again, ioArgs is useful, but not in simple cases
	        alert(err); // display the error
	        
	    }
	 });
	return false;
}
// See if any new Flash's have been set
function checkFlash(){
	
	dojo.xhrGet({
	    url: siteRoot+"/users/getFlash",
	    handleAs:"text",
	    preventCache: true,
	    load: function(data){
	        if(data !== ''){
	        	dojo.byId('flashZilla').innerHTML = data;
	        }
	    }
	});
}
// Update elements via ajax
function refreshContent(){
	var z = myDialog.attr('zone');
	if(z=='') return false; // full page refresh is probs best idea
	if(z==null) return false; 
	var node = dojo.byId('z_'+z);
	dojo.fadeOut({ node: node}).play();
	 dojo.xhrGet({
		    url: siteRoot+"/ajax_zone/"+z,
		    handleAs: "text",
		    load: function(data){
				dojo.byId(node).innerHTML = data;
				dojo.fadeIn({ node: node}).play();
	 		}
	});
}
//Generate and display tooltips
function toolTipr(item){

	var text = '<div style="width:180px;">' + item.getAttribute("alt") + '</div>';
	var x = new dijit.Tooltip({label: text, connectId: item, position: 'above'});
	item.onmouseover = '';
	x.open(item) ;
}
