$ = jQuery.noConflict();


parseQuery = function(query) {
   var Params = new Object() ;
   if (!query) return Params; 
   if (query.search("&")>0){
       var Pairs = query.split(/[;&]/);
       str_arr = "";
       for ( var i = 0; i < Pairs.length; i++ ) {
          var KeyVal = Pairs[i].split('=');
          if ( ! KeyVal || KeyVal.length != 2 ) continue;
          var key = decodeURIComponent( KeyVal[0] );
          var val = decodeURIComponent( KeyVal[1] );
          val = val.replace(/\+/g, ' ');
          if (key=='class') key='class_name'; 
          eval( "Params."+key+" = '"+val+"'");
         // Params[key] = val;
       }
   }else if(query.search("/")>0){
       var Pairs = query.split("/");
       for ( var i = 0; i < Pairs.length; i++ ) {
          var key = decodeURIComponent( Pairs[i] );
          var val = decodeURIComponent( Pairs[i+1] );
          if (key=='class') key='class_name'; 
          eval("Params."+key+" = '"+val+"'");
         // Params[key] = val;
          i++;
       }
   }
   return Params;
}  

parseUrl = function(query) {
   var p = new Array ();
   if (!query) return p; 
   p = query.split("?");
   if (p.length==1 && query.search(".php/")> 0){
        p = query.split(".php/");
        p[0]=p[0]+".php"
   }
    return p;
}

//---- Ibox   
   var imgLoader = '<table height="100%" align="center" ><tr><td><img id="loader" src="/images/admin/loading.gif" alt="Loading..." /></td></tr></table>';
 	$(function() {
    	$.fx.speeds._default = 1000;

	   init_ibox();
        
      $('<div id="dialog" title="" style="overflow:auto" ></div>').appendTo("body");
		$('#dialog').dialog({
			autoOpen: false,
            dragStart : 
                function(){ 
                $("iframe").each(function() {
                  $('<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($(this));
                  
                  })
            },
            dragStop: function(){
                    $("div.ui-resizable-iframeFix").each(function() { this.parentNode.removeChild(this); });
            },                        
            
            resizeStart : 
                function(){ 
                $("iframe").each(function() {
                  $('<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($(this));
                  
                  })
            },
            resizeStop: function(){
                    $("div.ui-resizable-iframeFix").each(function() { this.parentNode.removeChild(this); });
            }                        
            
        });            
	});

init_ibox = function(){
   $("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 ($(el).attr('target')!=''){
        url = $(el).attr('target');
    }else{
        url = $(el).attr('href');
    }
     title  = $(el).attr('title');
     params = parseQuery($(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 = $("#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 =$(content).html(); 
            }
        }

        if (params['type']=='3'){
            content = '<iframe src="'+content+'" height="100%" width="100%" style="border: 0;" frameborder="0"></iframe>'; 
        }
        if (params['type']=='4'){
           $('#dialog').load(content);
           content = imgLoader;
        }
        
        for(var i in params){
            $(myIbox).dialog("option",i,params[i]);
        }
     
        if (content) {
            $(myIbox).html(content);
        }
        
        $(myIbox).dialog("open");
          
        if (params['type']=='1'){
             img_tmp = new Image();
  		       $(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;
		                }
	                }
                             
                    $("#dialog")
				            .dialog("option","width", 30+parseInt(ix))
				            .dialog("option","height", 60+ parseInt(iy))
                        .dialog( "option", "position", "center" );
			            $("#loader").attr('src',img_src);
                   })
                   .attr("src",img_src);
        }  
        
        return true;    
    }
    
    
hideIbox = function hideIbox() {
        $("#dialog").dialog( "close" );
    }
    
//---- Ibox   


