﻿function UpdatePrize() {

    if (spanPr != undefined) {

        //reached last item so reset
        if (currentItem == items - 1) {
            currentItem = 0;
        }
        else {
            currentItem += 1;
        }
        spanPr.innerHTML = "<a href=\"" + competitionUrl + prizes[currentItem].url + "\">" + prizes[currentItem].callToAction + "</a>";
        ScrollIn();
    }
}

function ScrollOut() {
    //intervalId = window.setInterval("Scroll()", 100);
    var x = parseInt(spanPr.style.left);
    x -= 3;
    spanPr.style.left = x + "px";

    if (x > -284) {
        if (timeoutTime > 30) {
            timeoutTime -= 5;
        }
        setTimeout("ScrollOut()", timeoutTime);
    } else {

        //set position to right
        spanPr.style.left = "284px";
        //set timeout
        //timeoutTime = 0;

        UpdatePrize();
    }
}

function ScrollIn() {
    var x = parseInt(spanPr.style.left);
    //set x to be offset to the right
    x -= 3;

    spanPr.style.left = x + "px";

    if (x > 0) {
        if (timeoutTime < 100 && x < 50) {
            timeoutTime += 2;
        }
        setTimeout("ScrollIn()", timeoutTime);
    } else {
        timeoutTime = 100;
        spanPr.style.left = "0px";
        window.setTimeout("ScrollOut()", 5000);
    }
}

function Search() {

    var search = document.getElementById("ctl00_txtS");

    if (search.value == "PRIZE SEARCH") {
        search.value = "";
    }
    else if(search.value == "") {
        search.value = "PRIZE SEARCH";
    }
}

var newwindow;
function popup(url)
{
	newwindow = window.open(url,'name','height=700,width=700');
	if (window.focus) {newwindow.focus()}
}

function AlignContent(grHeightDiv, divToGrow) {
    //calculate height
    var greatestHeight =  GetElementTotalHeight(grHeightDiv);
    //set new height on element we're growing
    SetNewElementHeight(divToGrow, greatestHeight);
}

function GetElementTotalHeight(element) {
    var height = element.offsetHeight + getOffset(element);

//    for (var i = 0; i < element.childNodes.length; i++) {
//        if (element.childNodes[0].offsetHeight != undefined) {
//            height += element.childNodes[0].offsetHeight + getOffset(element.childNodes[0]);
//        }
//    }
    return height;
}

function SetNewElementHeight(Element, totalHeight) {
    var currentHeight = Element.offsetHeight + getOffset(Element);

    if (totalHeight > currentHeight) {
        //need to increase height
        Element.style.height = ((totalHeight - getOffset(Element)) - 2) + 'px';
    }
    else {
        Element.style.height = ((currentHeight - getOffset(Element))) + 'px';
    }
}

function getOffset(element) {
    var top = 0;
    while (element.offsetParent) {
        //add on this elements offset
        top += element.offsetTop;

        //search parent element
        element = element.offsetParent;
    }
    //add final elements offset/or add on base element as there is no parent
    top += element.offsetTop;
    return top;
}
