var LC_LANG='@LC_LANG@';
var LC_Index=[LC_LANG=='ru'?0:1];

var Mx = {
    Version: '1.0.0_beta',
    require: function(library) {
        document.write('<script type="text/javascript" src="' + library + '"></script>');
    },
    PrototypeVersionRequired: '1.5.1',
    initialize: function() {
        function convertVersionString(version) {
            var parts = version.split('.');
            return parseInt(parts[0]) * 100000 + parseInt(parts[1]) * 1000 + parseInt(parts[2]);
        }
        if (
            (typeof Prototype == 'undefined') ||
            (typeof Element == 'undefined') ||
            (typeof Element.Methods == 'undefined') ||
            (convertVersionString(Prototype.Version) < convertVersionString(Mx.PrototypeVersionRequired)))
            throw ('Micex JavaScript library requires Prorotype JavaScript framework >= ' + Mx.PrototypeVersionRequired);
    },
    
    loaderBigBackground: 'transparent url(/images/ajax-loader-bigindicator-trans.gif) no-repeat center',
    
    precision: 2
};

Mx.initialize();

Mx.Shield=Class.create();
Mx.Shield.prototype = {

    initialize: function(holder, options) {
        this.shield = $(holder);
        this.options=$H({});
        this.setOptions(options);
    },
    
    setOptions: function(options) {
        this.options=$H({
            parent:           undefined,
            background:       Mx.loaderBigBackground,
            backgroundColor: 'transparent',
            opacity:          0.5,
            duration:         0.5
        }).merge(this.options);
        this.options.merge(options);
    },
    
    _resize: function(){
        $(this.shield).setStyle({
            height: $($(this.options.parent)).getHeight() + 'px', 
            width: $($(this.options.parent)).getWidth() + 'px', 
            'z-index': 10, 
            cursor: 'progress', 
            'background-color': this.options.backgroundColor,
            opacity: this.options.opacity
        });
        
    },
    
    show: function(){
    
        if(!this.shield) return;
    
        if(!this.options.parent) return;
                
        Position.absolutize(this.shield);
        Position.clone($($(this.options.parent)), $(this.shield));
    
        this._resize();
        
        if(this.options.background)
            $(this.shield).setStyle({background: this.options.background});
        
        if(this._ie_fix_frame){
            this._fixIE();
        }
        
        $(this.shield).show();
    },
    
    _fixIE: function(){
        Position.clone($($(this.options.parent)), this._ie_fix_frame, {setTop: !$(this.shield).getStyle('height')});
        this._ie_fix_frame.setStyle({'z-index': 1, background: '#00cf00'});
        $(this.shield).setStyle({'z-index': 2}); 
        this._ie_fix_frame.show();
    },
    
            
    hide: function (){
        if(this.shield){
            Effect.Fade($(this.shield), {duration: this.options.duration, afterFinish: function(){
                    this._afterFinsh();
                }.bind(this)})
            if(this._ie_fix_frame){
                this._ie_fix_frame.hide();
            }
        }
    },
    
    _afterFinsh: function(){
        $(this.shield).setStyle({height: '0px', width: '0px', cursor: 'default'});
        if(this._ie_fix_frame){
            $(this._ie_fix_frame).setStyle({display: 'none', height: '0px', width: '0px', cursor: 'default', 'z-index': -1000});
        }
    } 
};


/**
* Prototype micex extentions
*/
Position.pageSize=function(){
        //
        // from lightbox2 by Lokesh Dhakar - http://www.huddletogether.com
        //
        var xScroll, yScroll;

        if (window.innerHeight && window.scrollMaxY) {
                xScroll = window.innerWidth + window.scrollMaxX;
                yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ 
                xScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
        } else {
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
        }

        var windowWidth, windowHeight;

        if (self.innerHeight) { 
                if(document.documentElement.clientWidth){
                        windowWidth = document.documentElement.clientWidth;
                } else {
                        windowWidth = self.innerWidth;
                }
                windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { 
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
        }

        if(yScroll < windowHeight){
                pageHeight = windowHeight;
        } else {
                pageHeight = yScroll;
        }

        if(xScroll < windowWidth){
                pageWidth = xScroll;
        } else {
                pageWidth = windowWidth;
        }

        arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
        return arrayPageSize;
}

Position.overlay=function(element){
    var dimension = Position.pageSize();
    $(element).setStyle({top:'0px', left:'0px', width: dimension[0]+'px', height: dimension[1]+'px'});
}

Mx.ProgressBar = Class.create();
Mx.ProgressBar.prototype={
	
	initialize: function (holder, params) {
		this.holder = $(holder);
		this.params = $H({
			maxValue: 100,
			minValue: 0,
			value:  50,
			cssStyle: {"background": "url(/images/pgbar.jpg) repeat-x", display: 'block', height: "9px", overflow: 'hidden'}	
			}).merge(params);	
			
		this.build();
	},
	
	build: function(){
                if(!this.holder) return;
		this.block = $(Builder.node("div"));
		this.block.setStyle(this.params.cssStyle);
		if (this.params.value < 0.001)
		    this.block.setStyle({width: "0%"});
		else
		    this.block.setStyle({width: this.params.value+"%"});
		this.block.update("&nbsp;");
		this.holder.appendChild(this.block);
	}
	
};

Date.prototype.toCSString = function() {
  	
  	var year = this.getYear();
  	var month = this.getMonth()+1;
  	var date = this.getDate();
  	var hours = this.getHours();
  	var minutes = this.getMinutes();
  	var seconds = this.getSeconds();
  	
  	if (year<1900) year+=1900;
  	
  	return year+"."+month+"."+date+" "+hours+":"+minutes+":"+seconds;
};

Date.prototype.toDBString = function() {
  	
  	var year = this.getYear();
  	var month = this.getMonth()+1;
  	var date = this.getDate();
  	var hours = this.getHours();
  	var minutes = this.getMinutes();
  	var seconds = this.getSeconds();
  	
  	if (year<1900) year+=1900;
  	
  	return year+"-"+month+"-"+date+" "+hours+":"+minutes+":"+seconds;
};
  
Date.prototype.setMaxTime = function() {
	this.setHours(23);
  	this.setMinutes(59);
  	this.setSeconds(59);
};
  
Date.prototype.setMinTime = function() {
	this.setHours(0);
  	this.setMinutes(0);
  	this.setSeconds(0);
  	this.setMilliseconds(0);
};
  
Date.prototype.addTimeSpanString=function(value) {
    
    value=new String(value).strip();
    var oldValue = new Date(this);
    
    if (value=='all'){
    	var newDate = this;
        newDate.setTime(this.getTime()-3600000*24*10000);
        return newDate;
    }
    
	var re = /^-?[0-9]+[dMyhm]$/;
  	if (!re.test(value)) {  		
  		return this;
  	}
  	else {
  		var nvalue = parseInt(value.replace(/[dMyhm]/,""));
  		var postfix = value.replace(/-?[0-9]+/,"");
  		var addDays=0;
  		var addMonth=0;
  		var addYears=0;
  		var addHours=0;
  		var addMinutes=0;
  		var newDate = this;
  		var currentYear = newDate.getYear();
  		if (currentYear < 1900) currentYear += 1900;
  		switch (postfix) {
  			case "d": {addDays=nvalue; break;}
  			case "M": {addMonth=nvalue; break;}
  			case "y": {addYears=nvalue; break;}
  			case "h": {addHours=nvalue; break;}
  			case "m": {addMinutes=nvalue; break;}
  		}
  		if (addDays!=0)
  			newDate.setDate(newDate.getDate()+addDays);
  		if (addMonth!=0) {  			
  			newDate.setMonth(newDate.getMonth()+addMonth);
  		}
  		if (addYears!=0)
  			newDate.setYear(currentYear+addYears);
  		if (addHours!=0)
  			newDate.setHours(newDate.getHours()+addHours);
  		if (addMinutes!=0)
  			newDate.setMinutes(newDate.getMinutes()+addMinutes);

		return newDate;
  	}
};

Date.timeSpans=[
        {id: '0',    title: ' ', short_title: ' '},
        {id: '-1h',  title: (LC_LANG=='ru'?'Час':'Hour'), short_title: (LC_LANG=='ru'?'Час':'hour')},
        {id: '-1d',  title: (LC_LANG=='ru'?'День':'Intraday'), short_title: (LC_LANG=='ru'?'день':'Intraday')},
        {id: '-7d',  title: (LC_LANG=='ru'?'Неделя':'1 Week'), short_title: (LC_LANG=='ru'?'нед':'1W')},
        {id: '-1M',  title: (LC_LANG=='ru'?'Месяц':'Month'), short_title: (LC_LANG=='ru'?'мес':'1M')},
        {id: '-3M',  title: (LC_LANG=='ru'?'3 Месяца':'3 months'), short_title: (LC_LANG=='ru'?'3 мес':'3 mons')},
        {id: '-6M',  title: (LC_LANG=='ru'?'6 Месяцев':'6 months'), short_title: (LC_LANG=='ru'?'6 мес':'6 mons')},
        {id: '-12M', title: (LC_LANG=='ru'?'Год':'Year'), short_title: (LC_LANG=='ru'?'год':'1Y')},
        {id: '-1y',  title: (LC_LANG=='ru'?'Год':'Year'), short_title: (LC_LANG=='ru'?'год':'year')},
        {id: 'all',  title: (LC_LANG=='ru'?'Весь период':'All period'), short_title: (LC_LANG=='ru'?'весь период':'All')}
    ];
    
Date.intervals= [
		{id: '1', title: (LC_LANG=='ru')?'1 минута':'1 minute'},
		{id: '5', title: (LC_LANG=='ru')?'5 минут':'5 minutes'},
		{id: '10', title: (LC_LANG=='ru')?'10 минут':'10 minutes'},
		{id: '30', title: (LC_LANG=='ru')?'30 минут':'30 minutes'},
		{id: '60', title: (LC_LANG=='ru')?'60 минут':'60 minutes'},
		{id: '24', title: (LC_LANG=='ru')?'сутки':'day'}			
];

Object.extend(Date.prototype, {
    daysTillNow: function(){
        var ctime=new Date();
        return new Number((ctime.getTime()-this.getTime())/3600000/24).toFixed(0);
    }
});


