
   $j = jQuery.noConflict();

   
   var imgLoader = '<table height="100%" align="center" ><tr><td><img id="loader" src="/images/admin/loading.gif" alt="Loading..." /></td></tr></table>';
 	$j(function() {
	   init_ibox();
        
      $j('<div id="dialog" title="" style="overflow:auto" ></div>').appendTo("body");
		$j('#dialog').dialog({
			autoOpen: false,
            dragStart : 
                function(){ 
                $j("iframe").each(function() {
                  $j('<div class="ui-resizable-iframeFix" style="background: #fff;"></div>')
                  .css({
                        width: this.offsetWidth+"px", height: this.offsetHeight+"px",
                        left:  this.offsetLeft+"px", top: this.offsetTop+"px",
                        position: "absolute", opacity: '0.0001', zIndex: 1000
                      })
                  .insertAfter($j(this));
                  
                  })
            },
            dragStop: function(){
                    $j("div.ui-resizable-iframeFix").each(function() { this.parentNode.removeChild(this); });
            },                        
            
            resizeStart : 
                function(){ 
                $j("iframe").each(function() {
                  $j('<div class="ui-resizable-iframeFix" style="background: #fff;"></div>')
                  .css({
                        width: this.offsetWidth+"px", height: this.offsetHeight+"px",
                        left:  this.offsetLeft+"px", top: this.offsetTop+"px",
                        position: "absolute", opacity: '0.0001', zIndex: 1000
                      })
                  .insertAfter($j(this));
                  
                  })
            },
            resizeStop: function(){
                    $j("div.ui-resizable-iframeFix").each(function() { this.parentNode.removeChild(this); });
            }                        
            
        });            
	});

init_ibox = function(){
   $j("a[rel^='ibox']").unbind('click').bind('click',function(){
     showIbox(this);
     return false;
   });  
}
   
   
   
showIbox = function (el) {   
    var params = new Array();
    var ibox_type = 0;
    var url,title ;
       
    if ($j(el).attr('target')!=''){
        url = $j(el).attr('target');
    }else{
        url = $j(el).attr('href');
    }
     title  = $j(el).attr('title');
     params = parseQuery($j(el).attr('rel').substr(5,999)); 
     if (params['title']){
        title = params['title'];
     }
     if (params['type']){
         params['type'] = parseInt(params['type']);   
     }else{    
	    var urlString = /\.jpg|\.JPG|\.JPEG|\.jpeg|\.png|\.gif|\.GIF|\.html|\.htm|\.php|\.cfm|\.asp|\.aspx|\.jsp|\.jst|\.rb|\.rhtml|\.txt/g;

	    var urlType = url.match(urlString);
	    if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.JPG' || urlType == '.JPEG' || urlType == '.png' || urlType == '.gif' || urlType == '.GIF'){
		    ibox_type = 1;
	    } else if(urlType=='.htm'||urlType=='.html'||urlType=='.php'||
			     urlType=='.asp'||urlType=='.aspx'||urlType=='.jsp'||
			     urlType=='.jst'||urlType=='.rb'||urlType=='.txt'||urlType=='.rhtml'||
			     urlType=='.cfm') {
		    ibox_type = 3;
	    }     
        params['type'] = parseInt(ibox_type);
     }

    showIboxContent(url,title,params);
}   
   
showIboxContent = function (content,title,params) {
        var myIbox = $j("#dialog");
        if (myIbox.dialog( "isOpen" ))
            myIbox.dialog( "close" );
            
        params['type'] = (params['type']) ? params['type'] : 3;
        params['title'] = (params['title']) ? params['title'] : title;
        params['modal'] = (params['modal']) ? params['modal'] : false;
        params['width'] =  (params['width']) ? (parseInt(params['width'] )+ ((params['width'].search('%')>-1)  ? '%' : 0 )):150 ;
        params['height'] = (params['height'])? (parseInt(params['height'])+ ((params['height'].search('%')>-1) ? 500 : 0 )):170 ;

        if (params['type']=='1'){
            img_src = content;
            params['modal'] = (params['modal']) ? params['modal'] : true; 
            content = imgLoader;
        }

        if (params['type']=='2'){
            if( content.substr(0,1)=="#"){
                content =$j(content).html(); 
            }
        }

        if (params['type']=='3'){
            content = '<iframe src="'+content+'" height="100%" width="100%" style="border: 0;" frameborder="0"></iframe>'; 
        }
        if (params['type']=='4'){
           $j('#dialog').load(content);
           content = imgLoader;
        }
        
        for(var i in params){
            $j(myIbox).dialog("option",i,params[i]);
        }
     
        if (content) {
            $j(myIbox).html(content);
        }
        
        $j(myIbox).dialog("open");
          
        if (params['type']=='1'){
             img_tmp = new Image();
  		       $j(img_tmp).load(function(){
             
	                var ix = img_tmp.width;
                   var iy = img_tmp.height

  	                var docElem = document.documentElement
	                var x = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth - 150;
	                var y = self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight- 150;
	                if(ix > x) { 
		                iy = iy * (x/ix); 
		                ix = x; 
		                if(iy > y) { 
			                ix = ix * (y/iy); 
			                iy = y; 
		                }
	                } 
	                else if(iy > y) { 
		                ix = ix * (y/iy); 
		                iy = y; 
		                if(ix > x) { 
			                iy = iy * (x/ix); 
			                ix = x;
		                }
	                }
                             
                    $j("#dialog")
				            .dialog("option","width", 30+parseInt(ix))
				            .dialog("option","height", 60+ parseInt(iy))
                        .dialog( "option", "position", "center" );
			            $j("#loader").attr('src',img_src);
                   })
                   .attr("src",img_src);
        }  
        
        return true;    
    }
    
    
hideIbox = function hideIbox() {
        $j("#dialog").dialog( "close" );
    }


