//add to product to bookMark
function checkBookMark(){
var bookMarkElement = document.getElementById("bookMarkLink");
var bookMarkStatus = document.getElementById("bookMarkStatus");
if ( bookMarkElement.innerHTML == 'Do oblíbených' ){
 bookMarkElement.innerHTML = "Odebrat z oblíbených";
}else{
 bookMarkElement.innerHTML = "Do oblíbených";
}
var bookMarkStatusValue = bookMarkStatus.value;
KarsaAjaxRefreshDirect('bookMarkAjax', 'controlname=BookMarkProduct.ascx;bookMarkStatus='+bookMarkStatusValue+';productID='+document.getElementById("productID").value);
//set field for ajax
if ( bookMarkStatusValue == 'addProduct' ){
 bookMarkStatus.value = 'removeProduct';
}else{
 bookMarkStatus.value = 'addProduct';
}

}

function addToCompare(pid){
if(KarsaAjaxGetObjectById('f_compare'+pid).checked){
    var ScriptUncheck = KarsaAjaxCall('controlname=GetFromSessionAjax.ascx;sessionUncheck=get');
    eval(ScriptUncheck);
    KarsaAjaxRefreshDirect('adcomp', 'controlname=ComparisonProductList.ascx;productID='+pid+';compStatus=addProduct');	
}
else{
	KarsaAjaxRefreshDirect('adcomp', 'controlname=ComparisonProductList.ascx;productID='+pid+';compStatus=removeProduct');
}
}

function addComparasion(){
KarsaAjaxRefreshDirect('adcomp', 'controlname=ComparisonProductList.ascx;productID='+document.getElementById("productID").value+';compStatus=addProduct');
}


function removeComparasion(productID){
KarsaAjaxRefreshDirect('adcomp', 'controlname=ComparisonProductList.ascx;productID='+productID+';compStatus=removeProduct');
}




//kod
var codeC = 'code';   

//call this function when want to start animation like this startAnim(this)
function startAnim(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, 'frame-orange', code);
}


//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;
}
