﻿var store_old_class = new Array();

function getById(id)
{
    var obj = null;
    if( (obj = document.getElementById(id)) ) return obj;
    return obj;
}

function getInfo(param)
{
    var obj = null;
    if( typeof param == 'object')
    {
        obj = param
    }
    else if( typeof param == 'string')
    {
        obj = getById(param); 
    }
    
    if( obj != null )
    {
        return {width   : parseInt(obj.offsetWidth),
                height  : parseInt(obj.offsetHeight),
                left    : parseInt(obj.offsetLeft),
                top     : parseInt(obj.offsetTop)
                }
    }
}

function highlight(obj)
{
    if(!document.getElementById('designers-right-col'))return false;
    var cont = document.getElementById('designers-right-col');
    var e = cont.getElementsByTagName('h1');
    var i = 0;

    for(i=0 ; i<e.length ; i++)
    {
        store_old_class[i] = e[i].className;
        e[i].className = 'color0';
    }
}

function unhighlight(obj)
{
    if(!document.getElementById('designers-right-col'))return false;
    var cont = document.getElementById('designers-right-col');
    var e = cont.getElementsByTagName('h1');
    var i = 0;

    for(i=0 ; i<e.length ; i++)
    {
        e[i].className = store_old_class[i];
    }
}

function chngClass(obj, cn)
{
    if(obj.parentNode.className != 'active')
    {
        obj.className = cn;
    }
}

function sendSearch(form, initText)
{
    if(checkInput(form.search_text, CHK_LENGTH, ml_lang[1], 2) && form.search_text.value != initText)
    {
        return true;
    }
    return false;
}

function openDigicat(id_catalog,extid,page,area)
{
    if( typeof page == 'undefined' )
        page = 1;
        
    if( typeof area == 'undefined')
    {
        area = 'main';
    }
    
    
    openPopupPage(basedir + 'digicat/index.php?id_catalog='+id_catalog+"&page="+page+"&area="+area,'DIGICAT',screen.availWidth,screen.availHeight,"center:yes;fullscreen:yes;location:no;menubar:no;resizable:no;scrollbars:no;status:no;titlebar:no;toolbar:no");
}

function calendarScroll(day, mode)
{
    var step = (mode == 'down' ? 50 : -50);
    var id_day_col = 'calendar-day-col-'+day;
    var obj = null;
    var cell_count = 0;
    
    if( !(obj = getById(id_day_col)) )return false;
    cell_count = obj.getElementsByTagName('div').length;
    
    switch(mode)
    {
        case 'up':
            obj.scrollTop+=step;
        break;
    
        case 'down':
            obj.scrollTop+=step;
        break;
    }
    
    if(obj.scrollTop != 0)
    {
        getById('img-day-col-up-'+day).style.display = 'inline';
    }
    else
    {
        getById('img-day-col-up-'+day).style.display = 'none';
    }
    
    if(obj.scrollTop == (cell_count-5)*50)
    {
        getById('img-day-col-down-'+day).style.display = 'none'; 
    }
    else
    {
        getById('img-day-col-down-'+day).style.display = 'inline';
    }
}

var akt_desc = 0;
function showDescription(obj, id, color)
{
    var container = getById('calendar-description-container');
    
    container.style.borderColor = color;
    if(akt_desc != id)
    {
        setGrayBg(0, akt_desc);
        setGrayBg(1, id);
        setGrayTypeMenu(1);
        akt_desc = id;
        Ajax.call('getEvent', 'index.php', null, Ajax.OP_SETHTML, {post:{id_event:id, page:'AX:GET_EVENT'}, nodeId:'calendar-description-container'});
    }
    else
    {
        container.style.borderColor = '#BBBBB9';
        Ajax.call('getEvent', 'index.php', null, Ajax.OP_SETHTML, {post:{page:'AX:GET_EVENT'}, nodeId:'calendar-description-container'});
        setGrayBg(0, id);
        setGrayTypeMenu(0);
        akt_desc = null;
    }
}

var old_bg_colors = new Array();
var old_onclick = new Array();
function setGrayBg(mode, id)
{
    if(typeof id == 'undefined')return false;
    if( !getById('calendar-day-content') ) return false;
    var divs = getById('calendar-day-content').getElementsByTagName('div');
    
    if(mode == 1)
    {
        old_bg_colors = new Array();
        old_onclick = new Array();
        for(var i=0 ; i<divs.length ; i++)
        {
            var akt = divs[i];
            if(akt.className.indexOf('cell') == -1)continue;
            if(akt.style.backgroundColor == '')continue;
            if(akt.id == 'event-'+id)continue;
            
            old_bg_colors[i] = akt.style.backgroundColor;
            old_onclick[i] = akt.onclick;
            akt.style.backgroundColor = '#dededc';
            //akt.onclick = null;
        }
    }
    else
    {
        for(var i=0 ; i<divs.length ; i++)
        {
            var akt = divs[i];
            if(akt.className.indexOf('cell') == -1)continue;
            if(akt.style.backgroundColor == '')continue;
            if(akt.id == 'event-'+id)continue;
            
            akt.style.backgroundColor = old_bg_colors[i];
            //akt.onclick = old_onclick[i];
        }
    }
}

var old_menu_class = new Array();
function setGrayTypeMenu(mode)
{
    var container = getById('calendar-type-menu');
    var a = container.getElementsByTagName('a');
    
    if(mode == 1)
    {
        for(var i=0 ; i<a.length ; i++)
        {
            if(a[i].className != '')old_menu_class[i] = a[i].className;
            a[i].className = '';
            a[i].style.color = '#AAAAAA';
            a[i].style.textDecoration = 'none';
            a[i].style.cursor = 'default';
            a[i].onclick = function(){return false;}
        }
    }
    else
    {
        for(var i=0 ; i<a.length ; i++)
        {
            a[i].className = old_menu_class[i];
            a[i].style.color = '';
            a[i].style.textDecoration = '';
            a[i].style.cursor = '';
            a[i].onclick = null;
        }
    }
}

function showCalendarUploadPic(url)
{
    var obj = getById('show-upload-pic');
    var img = obj.getElementsByTagName('img').item(0);
    if(!isElement(img)) img = document.createElement('img');
    
    
    if(obj.style.display == 'none')
    {
        obj.getElementsByTagName('td').item(0).appendChild(img);;
        img.src = url;
        obj.style.display = 'block';
    }
    else
    {
        img.parentNode.removeChild(img);
        obj.style.display = 'none';
    }
}

function openMap(addr)
{
    openPopupPage(basedir + "map.php?addr="+addr,"POP_MAP",640,480, "location:no;menubar:no;resizable:no;scrollbars:no;status:yes;titlebar:yes;toolbar:no;center:yes", false);
}

function calendarScrollMonthView(id, type)
{
    var obj = null;
    var step = 12;
    var container = 'monthv-day-'+id;
    var scroll_up = getById('monthv-scroll-up-'+id);
    var scroll_down = getById('monthv-scroll-down-'+id);
    var count = 0;
        
    if( !(obj = getById(container)) )return alert('Error: Not found this id: '+id);
    count = obj.getElementsByTagName('a').length-1;
    
    switch(type)
    {
        case 'up':
            obj.scrollTop-=step;
        break;
    
        case 'down':
            obj.scrollTop+=step;
        break;
    }
    
    if(obj.scrollTop == 0)
    {
        if(scroll_up != null)
        {
            scroll_up.style.display = 'none';
            obj.style.height = '24px';
            obj.style.marginTop = '5px';
        }
    }
    else
    {
        if(scroll_up != null)
        {
            scroll_up.style.display = 'block';
            obj.style.height = '12px';
            obj.style.marginTop = '0px';
        }
    }
    
    if(obj.scrollTop == (count-1)*step)
    {
        if(scroll_down != null)
        {
            scroll_down.style.display = 'none';
            obj.style.height = '24px';
        }
    }
    else if(obj.scrollTop != 0)
    {
        if(scroll_down != null)
        {
            scroll_down.style.display = 'block';
            obj.style.height = '12px';
        }
    }
}

function showAdvertTitle(state)
{
    if(state == 1)
    {
        getById('calendar-advert-function-buttons').style.display = 'none';
        getById('calendar-advert-title').style.display = 'inline';
    }
    else
    {
        getById('calendar-advert-function-buttons').style.display = 'inline';
        getById('calendar-advert-title').style.display = 'none';
    }
}

var loadedImages = {};
var EventCalendarAdvertOnMouseOute = false;

function preloadAdvertImage(name, url)
{
    if(typeof loadedImages[name] == 'undefined' && url.length > 0)
    {
        loadedImages[name] = new Image();
        loadedImages[name].src = url;
        loadedImages[name].onload = function()
        {
            if(url.length == 0)getById('_EVENT_CALENDAR_ADVERT').src = this.src;
        }
    }
    else
    {
        if(url.length == 0)getById('_EVENT_CALENDAR_ADVERT').src = loadedImages[name].src;
    }    
}

function pickDate(formName,inputName)
{
    var url = "calendar.php?formName="+formName+"&inputName="+inputName;

    var form = document.forms[formName];

    if( form )
    {
        var year  = form.elements[inputName + '_year'];
        var month = form.elements[inputName + '_month'];
        var day   = form.elements[inputName + '_day'];
    }

	openPopupPage(url,"DATEPICKER",182,204,"center:yes");
}

function setPickedDate(formName, name, value)
{
    var form = document.forms[formName];
    var date = value.split('/');
        
    form[name +'_year'].value = date[0];
    form[name +'_month'].value = date[1];
    form[name +'_day'].value = date[2];
    
    if(formName == 'frmNewEvt')
    {
        setDate(name, date);
    }
    
    if(formName == 'frmNewAdvert' || formName == 'frmEditAdvert')
    {
        setAdvertDate(formName, name);
    }
}

function writeClock()
{
    
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="36" height="36" id="clock" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="allowFullScreen" value="false" />');
	document.write('<param name="movie" value="./flash/clock.swf" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="wmode" value="transparent" />');
	document.write('<param name="bgcolor" value="#cccccc" />');
	document.write('<embed src="./flash/clock.swf" quality="high" wmode="transparent" bgcolor="#cccccc" width="36" height="36" name="clock" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}


var moveTo =
{
    center: function()
    {
        var i_client 	= { width   	: parseInt(document.documentElement.clientWidth),
                            height  	: parseInt(document.documentElement.clientHeight),
                            scroll_left	: parseInt(document.documentElement.scrollLeft),
                            scroll_top	: parseInt(document.documentElement.scrollTop)};
        var o_container = getById('container');
        var i_container = getInfo(o_container);
        
        var left = i_client.width/2 - i_container.width/2 ;
        var top = i_client.height/2 - i_container.height/2 ;
        
        o_container.style.position = 'absolute';
        o_container.style.left = (left > 0 ? left : 0) + 'px';
        o_container.style.top = (top > 0 ? top : 0) + 'px';
    }
}

var onloadF =
{
    event_id:       '',
    event_color:    '',
    
    showEvent: function()
    {
        showDescription(getById('event-'+this.event_id), this.event_id, '#'+this.event_color);
    }
}


