(function($){
 $.fn.colorFire = function(settings) {	
	 var defaults = {	
			 			scrolling : false, 
			 			event: '',
			 			iframe: false,
			 			width: '',
			 			height: '',
			 			modal: false,
			 			bindOn: '',
			 			onOpen: false,
			 			onComplete: false,
			 			onClosed: false,
			 			classnameSuffix: ''
			 		};
	 
	 if( settings ) {
		 $.extend(defaults, settings);
	 }
	 
    $.fn.colorFire.setProps = function(e){
    	
    	var ep = new Object;
    	ep.h = e.attr('href');
    	ep.p = e.attr('rel'); 
    	
    	if(ep.p.length > 0){
	    	if(ep.p.indexOf('=') !== false){
	    		if(ep.p.indexOf('&') !== false){
	    			ep.splitAnds = ep.p.split("&");
	    			$(ep.splitAnds).each(function(i){
	    				var pointer = ep.splitAnds[i];
	    				if(pointer.indexOf('=') != false){
	    					ep.splitEq = pointer.split('=');
	    					$.fn.colorbox.settings[ep.splitEq[0]] = $.fn.colorFire.typeConvert(ep.splitEq[1]);	    					
	    				}
	    			});
	    		}
	    	}	else	{
	    		eq.splitEq = ep.p.split('=');	    			    					
				$.fn.colorbox.settings[ep.splitEq[0]] = $.fn.colorFire.typeConvert(ep.splitEq[1]);
	    	}
    	}
    	
    	$.fn.colorbox.settings['href'] = ep.h;
    	$.fn.colorbox.settings['onOpen'] = defaults.onOpen;    
    	$.fn.colorbox.settings['onComplete'] = function(){
    		defaults.onComplete;
    	};
    	$.fn.colorbox.settings['onClosed'] = defaults.onClosed; 
    	
    	return this;
    };
    
    $.fn.colorFire.setEvents = function(){
    	$('.' + defaults.bindOn + 'ColorBox' + defaults.classnameSuffix).each(function(){    		
    		$(this).bind(defaults.bindOn, function(){
    			$.fn.colorFire.launchOne($(this));
    			return false;
    		});
    	});
    };
    
    $.fn.colorFire.launchOne = function(item){
    	$.fn.colorFire.setProps(item);
    	$.fn.colorbox({href: $(item).attr('href')});
    	return false;
    };
    
    $.fn.colorFire.typeConvert = function(string){
    	switch (string.toLowerCase ()) {
		    case "true":			    	 
		    	 string = true;
		    	 break;
		    case "false":			    	 
		    	 string = false;
		    	 break;			    
		}

    	if(! isNaN(parseInt(string)) ){
    		string = parseInt(string);
    	}
    	
    	return string;
    };
    
    return this.each(function() {
    	
    	if(defaults.bindOn != ''){     		
    		$.fn.colorFire.setEvents();  
    		return false;
    	}	else	{
    		var e = $(this);
    		$.fn.colorFire.setProps(e);
    		$.fn.colorbox({href: e.attr('href')}); 
    		return false;
    	}
    });
 };
})(jQuery);

$(document).ready(function() {	
	$('.ajaxColorBox').colorFire();	
	$('.clickColorBox').colorFire({bindOn: 'click'});
	$('.resizeColorBox').colorFire({onComplete: function(){
		$.fn.colorbox.resize();
	}});
	$('.closeColorBoxRedirect').colorFire({bindOn: 'click', onComplete: function(){
		$.fn.colorbox.close();
	}});
	$('.clickColorBoxReload').colorFire({bindOn: 'click', onClosed:function(){ window.location.reload();}, classnameSuffix: 'Reload'});
});


function resizeBox( widthExt, heightExt, toUrl ) {
	parent.$.fn.colorbox({href:toUrl, iframe:true, width:widthExt, height:heightExt, scrolling:true });	
}
function resizeColorBoxElement( idElem, width, height ) {
	IDElem = '#'+idElem;
	if ( typeof($(IDElem).attr('rel') ) !=  'undefined' ){
		newRelAttr = "iframe=true&width="+width+"&height="+height+"&scrolling=true";
		$(IDElem).attr('rel', newRelAttr);
	}
}
