﻿/* window 'load' attachment */
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

/* get splited string */
function getSplited(obj, splitby) {
    var objarray = new Array();
    objarray = obj.split(splitby);
    return objarray;
}

/* grab Elements from the DOM by className */
function getElementsByClass(searchClass, node, tag) {
    var classElements = new Array();
    if (node == null)
        node = document;
    if (tag == null)
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className)) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

//-------------------------------------------Hide/Show Images on mouse over -----------------------------
function imageshowhide() {
    if (!document.getElementsByTagName) {
        return false
    }
    var images = getElementsByClass("magic");
   
    /* loop through all images and attach a events */
    for (i = 0; i < images.length; i++) {
        /*retrive new path for image*/
        var objimage = new Array();
        objimage = getSplited(images[i].src, '/');
        var srcpath = objimage[objimage.length - 1];
        var imagepath = '';
        imagepath = 'PopatStore_Images/pressactualthumb/' + srcpath;
        
        // create container
        var div = document.createElement('div');
        div.id = 'pressImage:' + srcpath;

        
        // create container image
        var newImage = document.createElement('img');
        newImage.id = 'img:' + imagepath;
        newImage.src = imagepath;
        newImage.setAttribute('title', images[i].title);

        // apply css and style
        if (document.setAttribute) {
            div.setAttribute('style', 'display:none');
            div.setAttribute('class', 'divpreview');
        }
        else{
            div.style.display = 'none';
            div.className = 'divpreview';
        }
        div.appendChild(newImage);
        document.body.appendChild(div);
    }
}
addLoadEvent(imageshowhide);

/* show image preview */
function showtrial(obj, event) {
    var el = document.getElementById(obj);
    followmouse(obj, event); 
    el.style.display = '';
}

/* hide image preview */
function hidetrial(obj) {
    var el = document.getElementById(obj);
    el.style.display = 'none';
}

function truebody() {
    return (!window.opera && document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body
}

function followmouse(obj, e) {
    
    var docwidth = document.all ? truebody().scrollLeft + truebody().clientWidth : pageXOffset + window.innerWidth - 15
    var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)
    
    var divwidth = document.all ? document.getElementById(obj).clientWidth : document.getElementById(obj).offsetWidth;
    var divheight = document.all ? document.getElementById(obj).clientHeight : document.getElementById(obj).offsetHeight;
    
    var xCord = 0;
    var yCord = 0;
    if (document.captureEvents) {
        xCord = e.pageX;
        yCord = e.pageY;
    } else if (window.event.clientX) {
        xCord = window.event.clientX + document.documentElement.scrollLeft;
        yCord = window.event.clientY + document.documentElement.scrollTop;
    }
    
    if (docwidth - xCord < divwidth) { xCord = xCord - divwidth - 10; }
    if (docheight - yCord < divheight) { yCord = yCord - divheight - 15; }

    var left = Math.ceil(xCord);
    var top = Math.ceil(yCord);

    //document.getElementById("hdntest").value = 'xycord ' + xCord + ' - ' + yCord + ' -  window ' + docwidth + ' - ' + docheight + ' - div ' + divwidth + '  ' + divheight + ' - lefttop ' + left + '  ' + top + ' --- ' + (yCord - divheight - 10 + '->' + (docwidth - yCord));
    
    if (isNaN(top)) {
        top = 0;
    }

    if (isNaN(left)) {
        left = 0;
    }

    document.getElementById(obj).style.left = left + 5 + "px";
    document.getElementById(obj).style.top = top + 5 + "px";
}

//-------------------------------------------End Hide/Show Images on mouse over -------------------------



//--open video
function openvideo(id) {
    var win = window.open('lifestyleimage.aspx?msg=video&id=' + id, null, 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=500,height=417,top=10,left=10', 'true');
    win.focus();
    return false
}
