function openNextPage(url, time){
	pageDelay = setTimeout("location.href = '"+url+"'", (time*1000));
}

// Emulate IE's attachEvent for Mozilla... harder to do the other way around
if(typeof window.attachEvent == "undefined") {
    window.attachEvent = function(event, callback) {
        window.addEventListener(event.replace(/^on/, ''), callback, false);
    };
}

/**
 * Popup window openers
 */
 
function open_popup(uri, name, width, height) {
    // open a new window
    var newwin = window.open(uri, name, 'resizable=1,scrollbars=1,width='+width+',height='+height);
    
    // if window.focus is supported, give the popup focus
    if (window.focus) { newwin.focus() } 
    
}

function open_gallery(story) {
    open_popup(story,'spin_gallery',780, 750);
}
function open_media_viewer(uri,filetype) {
    open_popup('/mediaviewer/?uri='+uri+'&filetype='+filetype, 'vibe_media', 800, 520);
}
function open_poll_form(story) {
    open_popup('/poll/?mode=vote&story_id='+story, 'vibe_polls', 320, 370);
}
function open_poll_results(story) {
    open_popup('/poll/?story_id='+story, 'vibe_polls', 320, 570);
}
function open_election_form(story) {
    open_popup('/elections/?mode=vote&story_id='+story, 'vibe_polls', 320, 370);
}
function open_election_results(story) {
    open_popup('/elections/?story_id='+story, 'vibe_polls', 320, 570);
}
function open_contest(uri) {
    location.href = uri;
}

function resizePollToFit() {
    var agt = navigator.userAgent.toLowerCase();
    var is_major = parseInt(navigator.appVersion);
    var is_ie    = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    var is_ie5   = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0") !=-1));
    var is_ie5_5 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
    var is_mac = (agt.indexOf("mac")!=-1);
       
    if (window.opera || ((is_ie5 || is_ie5_5) && !is_mac)) {
      // Opera and IE5/Win only
      window.resizeTo(330, document.body.scrollHeight + 50);
    } else {
      // Everyone else
      window.resizeTo(330, document.body.offsetHeight + 50);
    }
}

/**
 * Home page tabs 
 */
 
function initTabs(container) {
    var tabList = document.getElementById(container);
    var tabs = tabList.getElementsByTagName("li");
    tabs[0].parentNode.currentlySelected = tabs[0];
    for(var i = 0; i < tabs.length; i++) {
        tabs[i].onclick = function () { 
            return setActiveTab(this, container); 
        };
    }    
}

function setActiveTab(self, container) {
    if(self.parentNode.currentlySelected == self) return false;
    var siblings = self.parentNode.getElementsByTagName("li");
    for(var i = 0; i < siblings.length; i++) {
        siblings[i].className = siblings[i].className.replace(/selected/, "");
    }
    self.className = self.className + " selected";
    self.parentNode.currentlySelected = self;
    
    var classRegex = new RegExp(container + "-");
    var targetFrame = document.getElementById(self.id.replace(classRegex, ""));
    var siblingFrames = targetFrame.parentNode.getElementsByTagName("div");
    for(var j = 0; j < siblingFrames.length; j++) {
        if(siblingFrames[j].id) {
            siblingFrames[j].style.display = "none";
        }
    }
    targetFrame.style.display = "block";
    return false;
}

function initBigStoryTabs() {
    initTabs("big-stories-tabs");
    //    initSlideshow();
}
 
function initFeedbackTabs() {
    initTabs("feedback-tabs");
}

function initSlideshow() {
     var doc = document.getElementById("big-stories-controls");
    if (doc) {
        slideshowTimer = setInterval("changeSlides(1);", 5000);
        doc.playing = true;
    }
    showSlideshowButton('playing');
}

function prevSlide() {
    stopSlideshow();
    changeSlides(-1);
}

function nextSlide() {
    stopSlideshow();
    changeSlides(1);
}

function startStopShow() {
    var doc = document.getElementById("big-stories-controls");
    if (doc.playing) {
        // Show is playing, so stop it
        stopSlideshow();   
    } else {
        // Show isn't playing, so start it
        initSlideshow();
    }
}

function stopSlideshow() {
    var doc = document.getElementById("big-stories-controls");
    doc.playing = false;
    clearInterval(slideshowTimer);
    showSlideshowButton('paused');
}

function showSlideshowButton(button) {
    var span = document.getElementById("pause");
    var link = span.getElementsByTagName("img")[0];
    
    if (button == 'paused') {
        link.src = link.src.replace(/playing/, "paused");
    } else {
        link.src = link.src.replace(/paused/, "playing");
    }
}

function changeSlides(dir) {
    var tabList = document.getElementById("big-stories-tabs");
    var tabs = tabList.getElementsByTagName("li");
    
    var currTab = tabs[0].parentNode.currentlySelected;
    
    var newTab;
    if (dir < 0) {
      newTab = currTab.previousSibling;
      while (true){
        if (!newTab) {
          newTab = currTab.parentNode.lastChild;
        }
        if (newTab.nodeType == 1) break;
        newTab = newTab.previousSibling;
      }
    } else {
      newTab = currTab.nextSibling;
      while (true){
        if (!newTab) {
          newTab = currTab.parentNode.firstChild;
        }
        if (newTab.nodeType == 1) break;
        newTab = newTab.nextSibling;
      }
    }
    
    setActiveTab(newTab, "big-stories-tabs");
}

/**
 * Calendar event popups
 */

function openCalendarEvent(url) {
    var calEvent = window.open(url, "event", "width=300,height=200,status=no,resizable=yes,menubar=no,toolbar=no");
}
 
/**
 * Navigation menu
 */
 
startList = function(navRoot) {
    if(document.all) { // IE only
        var sfEls = navRoot.getElementsByTagName("LI");
        for (var i=0; i<sfEls.length; i++) {
            sfEls[i].onmouseover=function() {
                this.className+="over ";
            }
            sfEls[i].onmouseout=function() {
                this.className=this.className.replace(new RegExp("over\\b"), "");
            }
        }
    }
}

//window.onload = function() {
window.attachEvent("onload", function() { 
    startList(document.getElementById("dropdown")); 
});


/**
 * Ratings Ajax
 */
function clearHover($obj) {
    var inputs = $obj.parentNode.getElementsByTagName("input");
    for(var i = 0; i < inputs.length; i++) {
        inputs[i].src = inputs[i].src.replace(/full/, "none");
    }
}

function hoverRating() {
    var inputs = this.parentNode.getElementsByTagName("input");
    for(var i = 0; i < inputs.length; i++) {
        if(i < this.value) {
            inputs[i].src = inputs[i].src.replace(/none/, "full");
        } else break;
    }    
}

function showRating(container, rating) {
    if(isNaN(rating)) return false;
    var inputs = container.getElementsByTagName("input");
    for(var i = 0; i < inputs.length; i++) {
        if(i < rating) {
            var $type = (rating - i == .5) ? "half" : "full";
            inputs[i].src = inputs[i].src.replace(/none/, $type);
        }
        inputs[i].onmouseover = null;
        inputs[i].onmouseout = null;
        inputs[i].onclick = function() { return false; }
        inputs[i].style.cursor = "default";
    }
    container.firstChild.data = "Avg: "; // change text label
    container.parentNode.onmouseout = null;
}

function findStoryId(container) {
    var inputs = container.getElementsByTagName("input");
    for(var i = 0; i < inputs.length; i++) {
        if(inputs[i].type == "hidden" && inputs[i].name == "story_id") {
            return inputs[i].value;
        }
    }
}

function vote() {
    var score = this.value;
    var story_id = findStoryId(this.parentNode.parentNode);
    var url = "/rating/vote.php?method=ajax&story_id=" + story_id + "&score=" + score;
    var container = this.parentNode;
    
    if(window.ActiveXObject) {
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
    } else {
        var xmlhttp = new XMLHttpRequest();
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4) {
            showRating(container, xmlhttp.responseText);
        }
    };
    xmlhttp.send(score);
    
    return false;
}

window.attachEvent("onload", function() {
    var allforms = document.getElementsByTagName("form");
    for(var j = 0; j < allforms.length; j++) {
        if(allforms[j].className == "vote-form" || allforms[j].id == "vote-form") {
            allforms[j].onmouseout = function() { clearHover(this) };
            var inputs = allforms[j].getElementsByTagName("input");
            for(var i = 0; i < inputs.length; i++) {
                inputs[i].onmouseover = hoverRating;
                inputs[i].onmouseout = function() { this.src = this.src.replace(/full/, "none"); }
                inputs[i].onclick = vote;
            }
        }
    }
});


