var baseName = 'part_';
var imgBaseName = 'lazyimage_';

/**
    Changes the current div displayed in content area.
    @param index - Integer index of div to display. All other
    divs are hidden
*/
function displayPart(index)
{
    
    if((typeof PART_COUNT == 'undefined') || !PART_COUNT)
        PART_COUNT = 6;

    for(var i = 0; i < PART_COUNT; i++)
    {
        var elem = document.getElementById(baseName + i);
        if(elem)
        {
            if(i == index)
                elem.style.display = 'block';
            else
                elem.style.display = 'none';
        }
    }

    drawBorder();

    if(self.partDisplayed)
    {
        partDisplayed(index);
    }
    

}

function setImgSrc(index, src)
{
    var imgElem = document.getElementById(imgBaseName + index);
 
    if(imgElem && imgElem.src != src)
    {
        /*
            Could optimize this a bit by running method body only if 
            imgElem.src.lastIndexOf(src) == -1
        */
        imgElem.src = src;
        imgElem.onload = countHeights;
    }
}

function drawBorderFrontPage()
{
    var borderDiv = document.getElementById("borderdiv");
    var bodyElem = document.getElementsByTagName("body").item(0);
    var contentElem = document.getElementById("content");
    var corner = document.getElementById("right_bottom_corner");
    var indexContent = document.getElementById("index_content");    
    
    // Get width
    borderDiv.style.width = (contentElem.offsetWidth) + 'px';

    // Get left
    borderDiv.style.left = (contentElem.offsetLeft - 1) + 'px';

    // Get top
    borderDiv.style.top = contentElem.offsetTop + 'px';

    // Calculate height
    var contentBottom = indexContent.offsetTop + indexContent.offsetHeight;
    
    // Draw ornament
    corner.style.top = contentBottom - corner.offsetHeight + 100 + 'px';
    corner.style.left = 803 + 'px';
    borderDiv.style.height = corner.offsetTop + corner.offsetHeight + 'px';
    
    makeBottomCornerVisible();
}

function drawBorder()
{
    var borderDiv = document.getElementById("borderdiv");
    var bodyElem = document.getElementsByTagName("body").item(0);
    var contentElem = document.getElementById("content");
    var corner = document.getElementById("right_bottom_corner");
    var contentCenterElem = document.getElementById("content_center_col");    
    var contentRightCol = document.getElementById("content_right_col");
    var contentLeftCol = document.getElementById("content_left_col");
    
    if(!contentLeftCol)
        contentLeftCol = document.getElementById("content_left_col_product");
        
    if(!contentCenterElem)
        contentCenterElem = document.getElementById("content_center_col_product");    
    
    
    // Get width
    borderDiv.style.width = (contentElem.offsetWidth) + 'px';

    // Get left
    borderDiv.style.left = (contentElem.offsetLeft - 1) + 'px';

    // Get top
    borderDiv.style.top = contentElem.offsetTop + 'px';

    var contentBottom = 0;
    
    if((contentCenterElem.offsetTop + contentCenterElem.offsetHeight) > (contentLeftCol.offsetTop + contentLeftCol.offsetHeight))
        contentBottom = contentCenterElem.offsetTop + contentCenterElem.offsetHeight;
    else
        contentBottom = contentLeftCol.offsetTop + contentLeftCol.offsetHeight;
    
    // Draw ornament
    corner.style.top = contentRightCol.offsetHeight + contentRightCol.offsetTop + 100 + 'px';
    corner.style.left = contentRightCol.offsetLeft + contentRightCol.offsetWidth - corner.offsetWidth + 4 + 'px';
    
    if(contentBottom > (corner.offsetTop + corner.offsetHeight))
    {   
        borderDiv.style.height = contentBottom + 'px'; 
    }
    else
    {
        borderDiv.style.height = corner.offsetTop + corner.offsetHeight +  'px';
    }
    makeBottomCornerVisible();
}

function makeBottomCornerVisible()
{
   var corner = document.getElementById("right_bottom_corner");
   if(corner)
   {
        corner.style.visibility = 'visible';
   }
}



// Default part is first one, so show it
if(typeof FRONT_PAGE != 'undefined')
{
    drawBorderFrontPage();
}
else
{
    if((getURLParam("part")) && (getURLParam("part") != ""))
    {
        displayPart(getURLParam("part"));
    }
    else
    {
        displayPart(0);
    }
    drawBorder();
}


/* FontSizeChangeTimer stuff */
initFontSizeChangeTimer();

function initFontSizeChangeTimer()
{
    fontCheck(receivechange);
}

function countHeights()
{
    if(typeof FRONT_PAGE != 'undefined')
    {
        drawBorderFrontPage();
    }
    else
    {
        drawBorder();
    }
}

function fontCheck(resultHandler){
    fontCheck.stop = false;    var checkdiv = document.getElementById('checkfontsizediv');    if(!checkdiv)
        return;
        
    var height = checkdiv.clientHeight;

    repeat();

    function repeat()    {
        if(checkdiv.clientHeight > height)        {            height = checkdiv.clientHeight;            resultHandler(height);        }
        else if(checkdiv.clientHeight < height)        {            height = checkdiv.clientHeight;            resultHandler(height);
//            window.location.reload();        }
        if(!fontCheck.stop)            setTimeout(repeat,250);
    }}function receivechange(height){
    countHeights();
}


/**
    Returns the given GET parameter value
*/
function getURLParam(strParamName)
{  var strReturn = "";  var strHref = window.location.href;  if ( strHref.indexOf("?") > -1 ){    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();    var aQueryString = strQueryString.split("&");    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){        var aParam = aQueryString[iParam].split("=");        strReturn = aParam[1];        break;      }    }  }  return strReturn;}

function pausecomp(Amount)
{
d = new Date() //today's date
while (1)
{
mill=new Date(); // Date Now
diff = mill-d; //difference in milliseconds
if( diff > Amount ) {break;}
}
}

/**
    Displays the chosen shop product category
*/
function costumeCategoryChosen(id)
{
    if ((id) && ( id > 0 ) && ( id <= 5))
    {
        document.location.href = 'costumes.php?category=' + id;
    }
}

/**
    Displays the front page
*/

function goToFrontPage()
{
        document.location.href = 'index.php';
}

function changelanguage(requesturl)
{
    document.location.href = requesturl + '?lang=' + document.getElementById('language_menu').value;
}

function selectActiveLanguage(lang)
{
    var langMenu = document.getElementById('language_menu');
    if(langMenu)
    {
        for(var i = 0; i < langMenu.options.length; i++)
        {
            if(langMenu.options[i].value == lang)
            {
                langMenu.selectedIndex = i;
                break;
            }
        }
    }
}

function changeLanguageToSelected()
{
    var pathName = document.location.protocol + '//' + document.location.host + document.location.pathname;
    var args = document.location.href.substring(pathName.length);
    var selectedLanguage = document.getElementById('language_menu').value;

    if(!args)
    {
        document.location.href = pathName + '?lang=' + selectedLanguage;
    }
    else
    {
        var args = args.split("&");
        var argsWithoutLang = new Array();
        
        for(var i = 0; i < args.length; i++)
        {
            var arg = args[i];
            var argParts = arg.split("=");
            var argName = argParts[0];
            var argValue = argParts[1];
            
            if(argName.substring(0,1) == '?')
                argName = argName.substring(1);
                
            if(argName != 'lang')
                argsWithoutLang[argsWithoutLang.length] = argName + '=' + argValue;
        }
        
        // Generate new arguments
        var argCount = argsWithoutLang.length;
        var argString = '?';
        
        for(var i = 0; i < argCount; i++)
        {
            argString = argString + argsWithoutLang[i] + '&';
        }  

        document.location.href = pathName + argString + 'lang=' + selectedLanguage;
    }
    
}

