﻿function theme(scope) {
    scope = scope == undefined ? document : scope;

    // Skin Form Elements
    $(scope).find('input,textarea').each(function () {

        var cssclass = '';
        var obj = $(this);

        switch (obj.attr('type')) {

            case 'text': case 'password':
                cssclass = 'txt_' + obj.attr('class').replace('aspNetDisabled ', '') + (obj.attr('disabled') ? ' disabled' : '');
                obj.wrap('<div class="' + cssclass + '">');
                defaultText(obj);
                break;

            case 'checkbox':
                cssclass = 'chk' + (obj.is(':checked') ? ' selected' : '');
                obj.click(function () {
                    obj.parent().toggleClass('selected');
                });
                obj.wrap('<div class="' + cssclass + '">');
                break;

            case 'radio':
                cssclass = 'rad' + (obj.is(':checked') ? ' selected' : '');
                obj.wrap('<div class="' + cssclass + '">');
                obj.click(function () {
                    var elementName = obj.attr('name');
                    $(':radio[name=' + elementName + ']').each(function (index) {
                        $(this).parent().removeClass('selected');
                        if ($(this).attr('id') == obj.attr('id'))
                            $(this).parent().addClass('selected');
                    });
                });
                break;

            case 'button': case 'submit': case 'image': case 'file':
                //Do nothing
                break;

            default:
                //this is a textarea because it has no type
                cssclass = 'txt_multiline_' + obj.attr('class') + (obj.attr('disabled') ? ' disabled' : '');
                cssparentclass = 'txt_multiline_' + obj.attr('class') + '_bottom' + (obj.attr('disabled') ? ' disabled' : '');
                obj.wrap('<div class="' + cssparentclass + '"><div class="' + cssclass + '">');

                defaultText(obj);
                break;
        }
    });

    $(scope).find('select').each(function () {
        s = $(this);
        css = s.attr('class').replace('aspNetDisabled ', '');

        if (!s.attr('multiple')) {
            s.wrap('<div class="sel_' + css + '"></div>');
            c = s.parent();
            c.children().before('<div class="sel_' + css + '_text">&nbsp;</div>').each(function () {
                if (this.selectedIndex >= 0) $(this).prev().text(this.options[this.selectedIndex].innerHTML)
            });
            c.css('position', 'relative');
            s.css({ 'opacity': 0, 'position': 'relative', 'z-index': 100 });
            var t = c.children().prev();
            t.css({ 'opacity': 100, 'overflow': 'hidden', 'position': 'absolute', 'text-indent': '0px', 'z-index': 1, 'top': 0, 'left': 0 });

            if (!(s.attr('disabled'))) {
                c.children().click(function () {
                    t.text((this.options.length > 0 && this.selectedIndex >= 0 ? this.options[this.selectedIndex].innerHTML : ''));
                });
                c.children().change(function () {
                    t.text((this.options.length > 0 && this.selectedIndex >= 0 ? this.options[this.selectedIndex].innerHTML : ''));
                });
            } else {
                s.wrap('<div class="sel_' + css + ' disabled"></div>');
            }
        }
    });

}

function defaultText(obj) {
    if (obj.attr('title') != '') {
        obj.val(obj.attr('title'));
    }
    obj.focus(function () {
        if (this.value == $(this).attr('title')) {
            this.value = '';
        }
    });
    obj.blur(function () {
        if (this.value == '' && $(this).attr('title') != '') {
            this.value = $(this).attr('title');
        }
    });
}

// Add menu actions
function menu() {
    $('ul.topnav li a').mouseover(function () {
        $(this).parent().find('ul.subnav').slideDown('medium').show();
        $(this).parent().hover(function () { }, function () {
            $(this).parent().find('ul.subnav').fadeOut('medium');
        });
    }).hover(function () {
        $(this).addClass('subhover');
    }, function () {
        $(this).removeClass('subhover');
    });
}

// Filmstrip
var o = '';
function filmStrip() {
    try {
        o = $('.filmstrip').tinyscrollbar({ axis: 'x', sizethumb: 40 });
    }
    catch (err) { }
}

function apply() {
    theme();
    menu();
    $("a[rel^='prettyPhoto']").prettyPhoto();
    $('.cycle').cycle({ timeout: 6000 });
    setTimeout('filmStrip()', 5000);
}

function pageLoad() {
    $(function () {
        apply();
    });
}
