/**
*
* $Id: core.js,v 1.7 2008/11/24 03:18:25 tleen Exp $
*
* Application-wide javascript
*
**/

var DATA_COLUMN_SEPERATOR   = '|';
var DATA_ROW_SEPERATOR      = '^^';
var DATA_META_START         = '[';
var DATA_META_END           = ']';
var DATA_META_SEPERATOR     = ':';

/**
* Common ad params
**/

var g_ad_client 	= 'pub-4855361296863608';
var g_ad_type 		= 'text_image';

var g_color_border	= 'F4A460'; // sandybrown 'CD853F'; // peru
var g_color_bg		= 'FFFFFF'; // white
var g_color_link 	= '191970'; // midnightblue 
var g_color_url	    = 'B22222'; // firebrick 
var g_color_text 	= '2F4F4F'; // darkslategrey

var g_ad_channel 	= '';
var g_ui_features   = "rc:6";


/* Existing class extensions */
String.prototype.head = function(seperator){ return this.split(seperator).first(); };
String.prototype.tail = function(seperator){ return this.split(seperator).pop(); };

Element.addMethods({
  	log 			: function(element){ console.log(element); return element; },
	depairClassName : function(element, target){ return $w(element.className).grep('^' + target + '_').first().tail('_') }
});

/**
* Ajax assist
**/

function ajax_parse(raw){
    
    var returner = new Array();      

    data = raw.split(DATA_ROW_SEPERATOR);
    var rows = new Array();       
    for(i = 0; i < data.length; i++){
        
        if(data[i] == '') continue;        

        if(data[i].charAt(0) == DATA_META_START){
            var_data = data[i].substring(1,data[i].length);
            var_data = var_data.split(DATA_META_SEPERATOR);
            returner[var_data[0]] = var_data[1];    
        }else{         
            cells   = data[i].split(DATA_COLUMN_SEPERATOR);
            rows.push(cells);
        }        
    }
    returner['rows'] = rows; 
    return returner;
}

function table_clear(id){
    table_body = $(id).getElementsByTagName('tbody');
    $(id).removeChild(table_body[0]);
}

function table_load(id,rows){
    
    table_clear(id);
    table_body = document.createElement('tbody');
    if(rows){
        rows.each(function(row){
            table_row = document.createElement('tr');
            row.each(function(cell){
                table_cell = document.createElement('td');
                if(typeof cell == 'object') table_cell.appendChild(cell);
                else table_cell.innerHTML = cell;
                table_row.appendChild(table_cell);
            });
            table_body.appendChild(table_row);
        });
    }
    $(id).appendChild(table_body); 
}

function is_false(item){
    return ( (!item) || (item == '0') || (item.size && (item.size() == 0) ));
}

function is_true(item){
    return !is_false(item);
}


/**
* * From the NiftyCorners tutorial @ http://www.html.it/articoli/nifty/index.html
**/

function rounded(selector,size){
    _rounded_create('both',selector,size);
}

function rounded_top(selector,size){
    _rounded_create('top',selector,size);
}

function rounded_bottom(selector,size){
    _rounded_create('bottom',selector,size);
}


function _rounded_create(type,selector,size){


    var class_prefix    = 'r';
    var max             = 4;
    
    if(size && (size == 'small')){ 
        class_prefix = 'rs'; 
        max = 2;
    }

    var type_to_range  = $H({
        'top'       : $R(1,max),
        'bottom'    : $R(1,max).toArray().reverse()
    });

    var types = $A([type]);
    if(type == 'both') types = $A(['bottom','top']);  
    
    $$(selector).each(function(element){

        var element_color = element.getStyle('background-color');
        
        var background_color = 'transparent';
        element.ancestors().each(function(parent){
            if( (background_color == 'transparent') || 
                (background_color == 'inherit')) background_color = parent.getStyle('background-color');
        });

        types.each(function(type){

            var wrapper = $(document.createElement('span'));
            wrapper.addClassName('r' + type);
            wrapper.setStyle({'background-color' : background_color});

            type_to_range[type].each(function(index){   
                var inner = $(document.createElement('span'));
                inner.addClassName(class_prefix + index);
                inner.setStyle({'background-color' : element_color});
                wrapper.appendChild(inner);
            });

            if(type == 'top') element.insertBefore(wrapper,element.firstChild);
            else element.appendChild(wrapper,element.firstChild);
            
        });
    });
}


document.observe('dom:loaded',function(event){

	$$('a.dummy').each(function(element){ element.writeAttribute({'href' : 'javascript:void(0)'}) });
	$$('form.dummy').each(function(form){ form.writeAttribute({'action' : 'javascript:void(0)'}); });


	$$('.toggle').each(function(element){
		element.observe('click',function(event){
			$(string_tail(this.identify(),'_')).toggle();
		}.bindAsEventListener(element));
	});	

	$$('.less').each(function(element){
		var target = $($w(element.className).grep('^target_').first().tail('_'));
		element.observe('click',function(event){
			event.target.update( (this.select('.lessable').invoke('toggle').first().visible() ? 'Less...' : 'More...'));
		}.bindAsEventListener(target));

		if(!target.select('.lessable').invoke('hide').length) element.hide();

	});

	$$('form .autosubmit').each(function(element){
		Event.observe(element,'change',function(event){
			this.up('form').submit();
		}.bindAsEventListener(element));
	});

	if($('adminline')){ $('adminline').observe('click',function(event){ $('adminline').hide() }); }

	if($('body_campaigns')){
		if($('advertisement_id')){
			$('advertisement_id').observe('change',function(event){
				$$('div.ad_preview').invoke('hide');
				var val = $F('advertisement_id');
				if(val) $('ad_preview_' + val).show();
			});
		}
	}

	Ajax.Responders.register({
		onCreate: function(){ $('indicator').show() },
		onComplete : function(){
			if(!Ajax.activeRequestCount) $('indicator').hide();		
	}});

});
