/*
<ChangeLog>
 <Versions>
  <Version> 
   <Version>1.0.1</Version> 
   <DateTime>19.12.2008 11:29:42</DateTime>
   <Author>Marek Skotnica</Author> 
   <Description>Dom.js is no longer needed. Cls class is included in this file. And evt class is no longer needed. </Description> 
  </Version> 
  <Version> 
   <Version>1.0</Version> 
   <DateTime>17.12.2008 18:29:42</DateTime>
   <Author>Marek Skotnica</Author> 
   <Description>Flying object. Now it is called by KarsaFlyingObject.startAnim, you can setup cssDivStyle.</Description> 
  </Version> 
 </Versions>
</ChangeLog>
*/

var KarsaFlyingObject = {
	
	cssDivStyle:"frame-orange", 

	//call this function when want to start animation like this startAnim(this)
	startAnim	:	function(anchor,code,fromDiv,toDiv) 
	{
	//class 'porovnatFrom' will be source object
		var from    = getParentByClass(fromDiv, anchor);
	//destination object is marked as 'porovnatTo' and it is get by ID
		var to      = document.getElementById(toDiv);
	//create a new object of animation, 'frame-orange' is css style for frame
		anchor.anim = new flyingFrame(from, to, this.cssDivStyle, code);
	}

}

//kod
var codeC = 'code';   

// library for working with multiple classes
var cls = {
	
	//return array witch contains all classes of element elm
	get : function (elm) {
		if (elm && elm.tagName) {
			var classes = [];
			if (elm.className) {	// na zaklade Centiho upozornenia o divnej interpretacii v Opere
				var cl = elm.className.replace(/\s+/g, " ");
				classes = cl.split(" ");
			}
			return classes;
		}
		return false;
	},
	// return true if element contains class
	has : function (elm, cl) {
		if ((actCl = cls.get(elm)) && (typeof(cl) == "string")) {
			for (var i = 0; i < actCl.length; i++) {
				if (actCl[i] == cl) {
					return true;
				}
			}
		}
		return false;
	},
	// add class of element
	add : function (elm, cl) {
		if ((actCl = cls.get(elm)) && (typeof(cl) == "string")) {
			if (!cls.has(elm, cl)) {
				elm.className += (actCl.length > 0) ? " " + cl : cl;
			}
			return true;
		}
		return false;
	},
	
	// remove class from element
	remove : function (elm, cl) {
		if ((actCl = cls.get(elm)) && (typeof(cl) == "string")) {
			tempCl = "";
			for (var i = 0; i < actCl.length; i++) {
				if (actCl[i] != cl) {
					if (tempCl != "") {tempCl += " ";}
					tempCl += actCl[i];
				}
				elm.className = tempCl;
			}
			return true;
		}
		return false;
	},
	
	//replace old element class with new, if old does not exist add new
	replace : function (elm, oldCl, newCl) {
		if ((actCl = cls.get(elm)) && (typeof(oldCl) == "string") && (typeof(newCl) == "string")) {
			tempCl = "";
			if (cls.has(elm, newCl)) {
				cls.remove(elm, oldCl);
			} else if (cls.has(elm, oldCl)) {
				for (var i = 0; i < actCl.length; i++) {
					if (tempCl != "") {tempCl += " ";}
					tempCl += (actCl[i] == oldCl) ? newCl : actCl[i];
				}
				elm.className = tempCl;
			} else {
				cls.add(elm, newCl);
			}
			return true;
		}
		return false;
	}

}

//constructor of flyingFrame object
function flyingFrame(elementFrom, elementTo, frameStyleClass, code) 
{    
    //save current date
    var d = new Date();
    this.month = d.getMonth() + 1;
    this.day = d.getDate();
    this.year = d.getFullYear();
    //save code to variables
    this.codeDate = code.split(";")[0];
    this.codeStr = code.split(";")[1]; 
    //how long will frame stay on source object
    this.stayOnSourceObject = 750;//ms
    //duration between moving frame
    this.speedOfStep        = 50;//ms
    //how long will frame stay on destination object
    this.stayOnDestObject   = 1000;//ms
    this.createObject       = 'DIV';
    //fill variables
    this.elementFrom        = elementFrom;
    this.elementTo        = elementTo;
    this.classFrame      = frameStyleClass;
 //reload hack, remember rect of element
    this.elementTo.oL     = fixOffsetLeft(this.elementTo);
    this.elementTo.oT     = fixOffsetTop(this.elementTo); 
    this.elementTo.oW     = this.elementTo.offsetWidth;
    this.elementTo.oH     = this.elementTo.offsetHeight;
    this.oldLeft         = fixOffsetLeft(this.elementFrom);
    this.oldTop         = fixOffsetTop(this.elementFrom);
    this.oldWidth        = this.elementFrom.offsetWidth;
    this.oldHeight        = this.elementFrom.offsetHeight;
    //define functions
    this.initialize        = flyingFrame_initialize;
    this.setFrame     = flyingFrame_setFrame;
    this.move         = flyingFrame_move;
    this.countStep    = flyingFrame_countStep;
    //first code step compare code
    if(codeC==this.codeStr)
    {
        //call initialize
        this.initialize();
    }
}

function flyingFrame_initialize(code)
{      
                   
    var parent = this;
    this.frame = document.createElement(this.createObject);
    cls.add(this.frame, this.classFrame);
    this.setFrame(fixOffsetLeft(this.elementFrom), fixOffsetTop(this.elementFrom), this.elementFrom.offsetWidth, this.elementFrom.offsetHeight);
    document.body.appendChild(this.frame);
    //zabezpeceni den
    if(this.day == this.codeDate.split(".")[2])
    {
        setTimeout(function () {parent.move();}, this.stayOnSourceObject);
    }
}
    
function flyingFrame_setFrame(x, y, w, h)  
{
        ////zabezpeceni rok 
        if(this.year == this.codeDate.split(".")[0])
        {
            var constant    = document.all ? 0 : 4;
            this.frame.style.left   = x + 'px';
            this.frame.style.top    = y + 'px';
            this.frame.style.width  = w - constant + 'px';
            this.frame.style.height = h - constant + 'px';
        }
}    

function flyingFrame_move()
{
    var parent  = this;
    var x   = this.countStep(fixOffsetLeft(this.frame), this.elementTo.oL, 5);
    var y   = this.countStep(fixOffsetTop(this.frame), this.elementTo.oT, 5);
    var w   = this.countStep(this.frame.offsetWidth, this.elementTo.oW, 5);
    var h   = this.countStep(this.frame.offsetHeight, this.elementTo.oH, 5);
    if(
        ((x != this.elementTo.oL) && (x != this.oldLeft)) ||
        ((y != this.elementTo.oT) && (y != this.oldTop)) ||
        ((w != this.elementTo.oW) && (w != this.oldWidth)) ||
        ((h != this.elementTo.oH) && (h != this.oldHeight))
        ) 
        {
            this.setFrame(x, y, w, h);
            this.oldLeft  = x;
            this.oldTop  = y;
            this.oldWidth  = w;
            this.oldHeight  = h;
               //zabezpeceni mesic   
               if(this.month == this.codeDate.split(".")[1])
               {
                 setTimeout(function () {parent.move();}, this.speedOfStep);
               }
        } 
        else 
        {
            if (document.all) 
            {
                this.frame.style.left = (x + 1) + 'px';
                this.frame.style.top  = (y + 1) + 'px';
            }
            setTimeout(function () {document.body.removeChild(parent.frame);}, this.stayOnDestObject);
        }
}
    
function flyingFrame_countStep(r, t, s) 
    {

        if(r>t) {
            return r + Math.floor((t-r)/s);
        } else {
            return r + Math.ceil((t-r)/s);
        }
    }

    
function getParentByClass(objParentClass, objTarget) 
{
    var parent = false;
    do {
        if (cls.has(objTarget, objParentClass)) {
            parent = objTarget;
            break;
        }
        objTarget = objTarget.parentNode;
    } while (objTarget != document);
    return parent;
}

//fix Offset to left
fixOffsetLeft = function(obj) 
{
var curleft = 0;
if (obj.offsetParent) 
{
    while (obj.offsetParent)
     {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
     }
} 
else 
  if (obj.x)    
    curleft += obj.x;
return curleft;
} 

//fix Offset to top
fixOffsetTop  = function(obj) 
{
var curtop  = 0;
if (obj.offsetParent)
{
    while (obj.offsetParent)
    {
        curtop  += obj.offsetTop; 
        obj = obj.offsetParent;
    }
} 
else 
    if (obj.y) 
        curtop += obj.y; 
return curtop;
}