﻿document.observe("dom:loaded", init);
if (navigator.userAgent.indexOf('iPhone') != -1)
    window.self = "http://m.slides.at/";


function init() {
    $('connectButton').onclick = toggleConnectBox;
    generateRating();
}

function toggleConnectBox() {
    var cForm = co = $('connect').getElementsByTagName('form')[0];
    if (cForm.style.display == 'none') {
        Effect.SlideDown(co);
        this.addClassName('open');
    }
    else {
        Effect.SlideUp(co);
        this.removeClassName('open');
    }
}

function generateRating() {
    generateListRating();
    generatePresentationRating();
}

function generateListRating() {
    var pList = $('presList');
    if (pList) {
        var pres = pList.getElementsByTagName('li');
        for (var i = 0; i < pres.length; i++) {
            var rating = pres[i].getElementsByClassName('rating')[0];
            var container = rating.getElementsByClassName('ratingContainer')[0];
            new Control.Rating(container, { value: rating.getElementsByTagName('span')[0].innerHTML, rated: true });
        }
    }
}

function generatePresentationRating() {

    var content = $('content');
    if (content) {
        var rating = content.getElementsByClassName('rating')[0];
        if (rating) {
            var identifier = content.getElementsByTagName('h3')[0].id.replace('h3_', '');
            var container = rating.getElementsByClassName('ratingContainer')[0];
            var rc = new Control.Rating(container, { value: rating.getElementsByTagName('span')[0].innerHTML, updateUrl: '/p/' + identifier + '/Rate', updateParameterName: 'rateValue' });
            rc.afterChange = function(rateValue) {
                var splitted = rating.getElementsByTagName('span')[0].innerHTML.split(' Rating');
                rating.getElementsByTagName('label')[0].innerHTML = (splitted[0] + 1) + ' Rating' + splitted[1];
            }
        }
    }
}

function valueLabel(element, label) {
    if (label)
        element.value = label;
    var text = element.value;
    var isPass = (element.type == "password");
    if (isPass)
        element.type = "text";
    element.onfocus = function() {
        if (this.value == text) {
            this.value = "";
            if (isPass)
                this.type = "password";
        }
    }
    element.onblur = function() {
        if (this.value == "") {
            this.value = text;
            if (isPass)
                this.type = "text";
        }
    }
}
