if (!Clip) var Clip = {};
Clip.Util = {};

Clip.Util.Form = {}

Clip.Util.Form.unserialize = function( form, query ) {
    var args = query.split('&');
    var hash = new Object();
    var ele = Form.getElements(form);

    var input = new Array();
    ele.each( function(e) {
            if( e.type == 'checkbox' || e.type == 'radio'){
                if(!hash[e.name]){
                    hash[e.name] = new Array();
                }
                hash[e.name].push(e);
            }else{
                hash[e.name] = e;
            }
        } );
    args.each( function(a){
            var key_val = a.split('=');
            var e = hash[key_val[0]];
            if(e){
                if( typeof(e.type) == 'undefined' ) {
                    e.each( function( el ) {
                            if( el.type == 'checkbox' || el.type == 'radio'){
                                if(el.value == key_val[1]){
                                    input.push(el.name)
                                    el.checked = true;
                                }
                            }
                        })
                 } else {
                    if(key_val[1] && !key_val[1].match(/^-+$/) ){
                        input.push(e.name);
                        e.value = decodeURI(key_val[1]);
                    }
                }
            }
        } );
    return input;
}


Clip.Util.Characters = Class.create();
Clip.Util.Characters.prototype = {
    args: $H(),
    timeout: null,
    text_count: -1,
    max_length: null,
    over_class: null,
    initialize: function( args ){
        this.args = $H(args);
        this.count();
        Event.observe($(this.args.textarea), 'blur', function(){ this.clear() }.bind(this));
        Event.observe($(this.args.textarea), 'focus', function(){ this.loop() }.bind(this));
    },
    count: function() {
        nl = 0
        if(a = $(this.args.textarea).value.match(/\n/g)){
            nl = a.length;
        }
        if(this.args.over_class){
            $(this.args.showarea).className = $(this.args.textarea).value.length + nl> this.args.max_length ? this.args.over_class : '';
        }else{
            $(this.args.showarea).style.color = $(this.args.textarea).value.length + nl > this.args.max_length ? 'red' : 'black';
        }
        $(this.args.showarea).innerHTML = $(this.args.textarea).value.length + nl;
    },
    loop: function() {

        if($(this.args.textarea) && this.text_count != $(this.args.textarea).value.length){
            this.text_count = $(this.args.textarea).value.length;
            this.count();
            if(this.timeout == null){
                this.timeout = setInterval( function(){ this.loop() }.bind(this) , 100);
            }
        }
    },
    clear: function() {
        clearInterval(this.timeout);
        this.timeout = null;
        this.text_count = -1;
    }
}

Clip.Util.Lightbox = function( path, type ) {
    if( type == 'movie' ){
	y = Clip.Util.getScroll() + 100;
        new Lightbox({ url: path + '&top=' + y + '&t=' + (new Date().getTime()) }).show();
    }else{
        a = document.createElement('a');
        a.href = path;
        new Lightbox().show(a)
    }
}

Clip.Util.getScroll = function() {
    var yScroll;
    if (self.pageYOffset) {
	yScroll = self.pageYOffset;
    }
    else if ( document.documentElement && document.documentElement.scrollTop ) { // Explorer 6 Strict
	yScroll = document.documentElement.scrollTop;
    }
    else if (document.body) { // all other Explorers
	yScroll = document.body.scrollTop;
    }
    return yScroll;
}
Clip.Util.changeMainImage = function(type) {
    if(type == 'movie'){
        $('movie_on').style.display = '';
        $('movie_off').style.display = 'none';
        $('photo_on').style.display = 'none';
        $('photo_off').style.display = '';
        $('main_pic').style.display = 'none';
        $('main_mov').style.display = '';
    }else if(type == 'photo'){
        $('movie_on').style.display = 'none';
        $('movie_off').style.display = '';
        $('photo_on').style.display = '';
        $('photo_off').style.display = 'none';
        $('main_pic').style.display = '';
        $('main_mov').style.display = 'none';
    }
}