﻿
var tunesDiv = document.getElementById('TunesContainer');
var tunesPane = document.getElementById('TunesPane');
var tunesTab = document.getElementById('TunesTab');
var isIE = navigator.userAgent.indexOf('MSIE')!=-1;
var isIE7 = navigator.userAgent.indexOf('MSIE 7')!=-1;
var isFF2 = navigator.userAgent.indexOf('Firefox/2')!=-1;
//alert(navigator.userAgent);
var pWidth = pageWidth();
var isOpened = false;
var openedPos, closedPos;
var imgCloseTabOn = new Image();
imgCloseTabOn.src = '/Portals/_default/Skins/Boathouse/CloseWidjetHover.png';
var imgOpenTabOn = new Image();
imgOpenTabOn.src = '/Portals/_default/Skins/Boathouse/OpenWidjetHover.png';
function initTunesBox()
{
    var x,y;
    if(isIE)
    {
        x =  getOffsetLeft(tunesDiv.parentElement) + tunesDiv.parentElement.clientWidth;
        y = -6;
    }
    else
    {
        x =  getOffsetLeft(tunesDiv.parentNode) + tunesDiv.parentNode.clientWidth;
        y = -6;
    }
    
    tunesDiv.style.left = x + 'px';
    tunesDiv.style.top = tunesDiv.offsetTop + y + 'px';
    tunesDiv.style.visibility = 'visible';
    
    closedPos = tunesPane.offsetLeft;
    openedPos = closedPos + 200;
    
    initIcons('newIcons');
    initIcons('salesIcons');
    if (readCookie('SideBarOpened') == '1')
        slide();
}

function slide()
{
    var curPos;
    if(isOpened)
    {
        isOpened = false;
        
        tunesTab.style.backgroundImage = 'url(/Portals/_default/Skins/Boathouse/images/OpenWidjet.gif)';
        curPos = openedPos;
        while(curPos > closedPos && !isOpened)
        {
            tunesPane.style.left = curPos + 'px'; 
            curPos--;

        }
        createCookie("SideBarOpened", '0', 10, '/');
    }
    else
    {
        isOpened = true;
        tunesTab.style.backgroundImage = 'url(/Portals/_default/Skins/Boathouse/images/CloseWidjet.gif)';
        curPos = closedPos;
        while(curPos < openedPos && isOpened)
        {
            tunesPane.style.left = curPos + 'px'; 
            curPos++;
        }
        createCookie("SideBarOpened", '1', 10, '/');
    }
}

function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null;}
function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset: document.documentElement.scrollLeft? document.documentElement.scrollLeft: document.body.scrollLeft? document.body.scrollLeft:0;}
function posTop() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset: document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop? document.body.scrollTop:0;}

function adjustPos()
{
    if(isIE)
        x =  getOffsetLeft(tunesDiv.parentElement) + tunesDiv.parentElement.clientWidth;
    else
        x =  getOffsetLeft(tunesDiv.parentNode) + tunesDiv.parentNode.clientWidth;
    
    tunesDiv.style.left = x + 'px';
    
    initIcons('newIcons');
    initIcons('salesIcons');
    
}

function getOffsetLeft(obj)
{
    var parentel = obj;
    var x = 0;
    while( (parentel!=null && parentel.tagName!="BODY") ) {
				    if( parentel.tagName == "html:body" ) break;
				    x += parentel.offsetLeft;
				    parentel = parentel.offsetParent;
			    } 
	return x;		    
}
function getOffsetTop(obj)
{
    var parentel = obj;
    var y = 0;
    while( (parentel!=null && parentel.tagName!="BODY") ) {
				    if( parentel.tagName == "html:body" ) break;
				    y += parentel.offsetTop;
				    parentel = parentel.offsetParent;
			    } 
	return y;		    
}


function tabOn()
{
    if(isOpened)
        tunesTab.style.backgroundImage = 'url('+imgCloseTabOn.src+')';
    else    
        tunesTab.style.backgroundImage = 'url('+imgOpenTabOn.src+')';
}
function tabOff()
{
    if(isOpened)
        tunesTab.style.backgroundImage = 'url(/Portals/_default/Skins/Boathouse/CloseWidjet.png)';
    else    
        tunesTab.style.backgroundImage = 'url(/Portals/_default/Skins/Boathouse/OpenWidjet.png)';
}

function initIcons(iconType)
{
    var icons = document.getElementsByName(iconType);
   // alert(iconType+':'+icons.length);
    var x,y, i, par;
    for(i=0;i<icons.length;i++)
    {
        //alert(icons[i].style.display);
        par = icons[i].parentElement?icons[i].parentElement:icons[i].parentNode;
        
        icons[i].style.display = '';
        x = getOffsetLeft(par) + par.clientWidth - icons[i].offsetWidth;
        y = getOffsetTop(par) + par.clientHeight - icons[i].offsetHeight;
        icons[i].style.left = x + 'px';
        icons[i].style.top = y + 'px';
        icons[i].style.display = '';
    }
}


window.onresize = adjustPos;
onload = initTunesBox;
