/**
 * This is for Cross-site Origin Resource Sharing (CORS) requests.
 * 
 * Additionally the script will fail-over to a proxy if you have one set up.
 * 
 * @param string   url      the url to retrieve
 * @param mixed    data     data to send along with the get request [optional]
 * @param function callback function to call on successful result [optional]
 * @param string   type     the type of data to be returned [optional]
 */
function getCORS(url, data, callback, type) {
    try {
        // Try using jQuery to get data
        jQuery.get(url, data, callback, type);
    } catch(e) {
        // jQuery get() failed, try IE8 CORS, or use the proxy
        if (jQuery.browser.msie && window.XDomainRequest) {
            // Use Microsoft XDR
            var xdr = new XDomainRequest();
            xdr.open("get", url);
            xdr.onload = function() {
                callback(this.responseText, 'success');
            };
            xdr.send();
        } else {
            try {
                // Ancient browser, use our proxy
                var mycallback = function() {
                    var textstatus = 'error';
                    var data = 'error';
                    if ((this.readyState == 4)
                        && (this.status == '200')) {
                        textstatus = 'success';
                        data = this.responseText;
                    }
                    callback(data, textstatus);
                };
                // proxy_xmlhttp is a separate script you'll have to set up
                request = new proxy_xmlhttp();
                request.open('GET', url, true);
                request.onreadystatechange = mycallback;
                request.send();
            } catch(e) {
                // Could not fetch using the proxy
            }
        }
    }
}

/**
 * This method is for Cross-site Origin Resource Sharing (CORS) POSTs
 *
 * @param string   url      the url to post to
 * @param mixed    data     additional data to send [optional]
 * @param function callback a function to call on success [optional]
 * @param string   type     the type of data to be returned [optional]
 */
function postCORS(url, data, callback, type)
{
    try {
        // Try using jQuery to POST
        jQuery.post(url, data, callback, type);
    } catch(e) {
        // jQuery POST failed
        var params = '';
        for (key in data) {
            params = params+'&'+key+'='+data[key];
        }
        // Try XDR, or use the proxy
        if (jQuery.browser.msie && window.XDomainRequest) {
            // Use XDR
            var xdr = new XDomainRequest();
            xdr.open("post", url);
            xdr.send(params);
            xdr.onload = function() {
                callback(xdr.responseText, 'success');
            };
        } else {
            try {
                // Use the proxy to post the data.
                request = new proxy_xmlhttp();
                request.open('POST', url, true);
                request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                request.send(params);
            } catch(e) {
                // could not post using the proxy
            }
        }
    }
}

// load page into divid
function ajaxpage(url, containerid){
   getCORS(url,null,function(data){document.getElementById(containerid).innerHTML=data;});
}

// load screen
function loadUrl(url) {
   ajaxpage('/path64/' + url + '/screen',url + '-applet');	
}  

function closeObj(url64){	
   ajaxpage('',url + '-applet');	
}

var vars = {};

$(document).ready(function() {
    $('a.toencode').click(function(event) {
      event.preventDefault();
      var link = $(event.target).attr('href').replace(/[?&]+([^=&]+)=([^&]*)/gi,function(m,key,value) {
         vars[key] = value;
       });
      var NewLink = encode64(vars['search']);
      window.location.href = '/search64/' + NewLink + '/html';
    });
});



$(function() {
	$(".buttonset").buttonset();
	$('#flat').menu({ 
		content: $('#flat').next().html(),
		showSpeed: 400 
	});
	$('#flatpls').menu({ 
		content: $('#flatpls').next().html(),
		showSpeed: 400 
	});	


	 $('input[type="text"]').addClass("idleField");  

     $('input[type="text"]').focus(function() {  
         $(this).removeClass("idleField").addClass("focusField");  
         if (this.value == this.defaultValue){  
             this.value = '';  
         }  
         if(this.value != this.defaultValue){  
             this.select();  
         }  
     });  
     $('input[type="text"]').blur(function() {  
         $(this).removeClass("focusField").addClass("idleField");  

/* if ($.trim(this.value == '')){  
             this.value = (this.defaultValue ? this.defaultValue : '');  
         }  */

});  


});

$(document).ready(function () {
   
   /* metadata */
   $('#info').dialog({
	   autoOpen: false,
	     show: 'blind',
	     height: 500,
	     width: 700,
	     resizable: true,
	     title: 'Metadata list'
	});
   
   $('.metadata').click(function() {
	   $('#info').load(this.href, function() {
		   /*$('table').width() + $('.ui-dialog-content').attr
			 $('#info').width();	*/	
		}).dialog('open');
	   
	   return false;
	});
   
   
   /* qrcode */
   $('#qrcode').dialog({
      autoOpen: false,
	show: 'blind',
	height: 500,
	width: 600,
	resizable: true,
	title: 'QRCode Image'
   });
   
   
   $('.qrcode').click(function() {
      $('#info').append($("<img>").attr("src",this.href)).dialog('open');
      return false;
   });
   
   
   
   /* dialog embed */
   $('#codepaste').width(450);
   $('#codepaste').height(150);
   
   /* dialog di embed oggetto */ 
   
   $('#embeddami').dialog({
      autoOpen: false,
	show: 'blind',
	height: 240,
	width: 500,
	resizable: false,
	title: 'Embed media',
	close: function() {
	   $('.options').removeAttr('checked');
	}
   });
   
   $('.embed').click(function() {
      $('.options').filter('[value=compact]').attr('checked', true);// imposto il radio checked di default
      
      $.get(this.href + '/compact', function(data) {
	 $('#codepaste').val(data);
      });	
      $('#embeddami').dialog('open').data('url', this.href); // apro il dialog e salvo l'url base
      
      
      return false;	
   });
   
   $('.options').click(function() {
      $.get($('#embeddami').data('url') + '/' + this.value, function(data) {
	 $('#codepaste').val(data);
      });		
   });

   
   /* dialog di embed playlist */
   $('#embedda_playlist').dialog({
      autoOpen: false,
	show: 'blind',
	height: 240,
	width: 500,
	resizable: false,
	title: 'Embed playlist',
   });
   
   
   $('.embed_playlist').click(function() {
      
      $.get(this.href + '/osmp', function(data) {
	 $('#codepaste').val(data);
      });	
      $('#embedda_playlist').dialog('open').data('url', this.href); // apro il dialog e salvo l'url base
      return false;	
   });
      
});

