/**
 * Copyright (c) Doddle Media mail@doddlemedia.com http://www.doddlemedia.com
 * Date: 24 July 2010
 * @author Pavlos Sideris
 * @version 1.0
 */
var DModal = {
    overlay: {
        show: function(selector,callBack){
            if(!$('#modalOverlay').length){
                $(document.body).prepend('<div id="modalOverlay"></div>');
                $('#modalOverlay').click(function(){DModal.viewport.hide()})
            }
            $('#modalOverlay').css({
                position: 'absolute',
                top: 0,
                left:0,
                background: '#00000F',
                width: '100%',
                height: $(document).height() + 'px',
                opacity: 0,
                zIndex: 1000
            })
            $('#modalOverlay').css({display: 'block'}).animate({opacity:0.75},300,function(){
                DModal.viewport.show(selector,callBack);
            })
         },
         hide: function(){
            $('#modalOverlay').animate({opacity:0},100,function(){
                $(this).css({display: 'none'})
            })
         }
    },
    viewport: {
        width: 0,
        height: 0,
        showClose: true,
        show: function(selector,callBack){
            if(!$('#modalViewport').length){
                $(document.body).prepend('<div id="modalViewport"></div>');
            }
            $('#modalViewport').css({
                position: 'absolute',
                top: '10%',
                left: '50%',
                width: 0,
                height: '2px',
                background: '#fff',
                zIndex: 1001
            })
            $('#modalViewport').animate({
                width: DModal.viewport.getWidth(selector)+'px',
                marginLeft: -DModal.viewport.getWidth(selector)*0.5+'px'
            },400,function(){
                $(this).animate({
                    height: DModal.viewport.getHeight(selector)+'px'
                },600,function(){
                    DModal.viewport.closeBtn();
                }).html($(selector).html());
                if(typeof callBack == 'function') callBack();
            })
        },
        hide: function(){
            $('#modalOverlay,#modalViewport').stop();
            $('#modalViewport').animate({
                width: '2px',
                marginLeft: 0
            }, 400, function(){
                $(this).animate({
                    height: 0
                },400,function(){
                    DModal.overlay.hide()
                    $(this).css({display:'none'})
                })
            }).html('')
        },
        getWidth: function(selector){
            if(!this.width) this.width = $(selector).width();
            return this.width;
        },
        getHeight: function(selector){
            if(!this.height) this.height = $(selector).height();
            return this.height;
        },
        closeBtn: function(){
            if(this.showClose){
                $('<a id="modalClose" href="javascript:void(0)"></a>').appendTo('#modalViewport').css({
                    position: 'absolute',
                    top:'-11px',
                    right: '-11px',
                    width: '36px',
                    height: '36px',
                    background: 'url(/imgs/modal_close.png) no-repeat 0 bottom'
                }).hover(function(){
                    $(this).css({backgroundPosition: '0 top'})
                },function(){
                    $(this).css({backgroundPosition: '0 bottom'})
                }).click(function(){
                    DModal.viewport.hide()
                })
            }
        }
    },
    load: function(selector,callBack){
        this.overlay.show(selector,callBack);
    }
}

var DForm = {
    invalid: [],
    validate: function(obj,callBack){
        $('[name]',obj).each(function(k,v){
            $(v).removeClass('error');
            if(!$(v).val()){
                DForm.invalid.push(v);
            }else if(v.name.indexOf('email') != -1){
                if(!DForm.validEmail($(v).val())) DForm.invalid.push(v);
            }else if(v.name.indexOf('phone') != -1){
                if(!/^(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*$/.test($(v).val())) DForm.invalid.push(v);
            }
        })
        if(typeof callBack == 'function') callBack();
        if(this.invalid.length!=0){
            $(this.invalid).each(function(i,v){
                $(v).addClass('error');
            })
            this.invalid = [];
            return false;
        }
        return true;
    },
    validEmail: function(email) {
        var iChars = " /:,;";
        for (var k=0; k<iChars.length; k++) {
            var badChar = iChars.charAt(k);
            if (email.indexOf(badChar) > -1) {
            return false;
            }
        }
        var atPos = email.indexOf("@",1);
        if (atPos == -1) {
            return false;
        }
        if (email.indexOf("@",atPos+1) != -1) {
            return false;
        }
        var periodPos = email.indexOf(".",atPos);
        if (periodPos == -1) {
            return false;
        }
        if (periodPos+3 > email.length)    {
            return false;
        }
        return true;
    },
    submit: function(obj,url,callBack){
        $.ajax({
            type: 'POST',
            url: url,
            data: $('[name]',obj),
            success: function(response){
                DForm.success(obj,callBack);
            },
            error: function(){
            }
        })
    },
    success: function(obj,callBack){
        $(obj).fadeTo(300, 0.15);
        $('<div id="DFormSuccess"></div>')
        .appendTo($(obj).parent())
        .html('<span style="color:#fff;"><b>Thank You</b><br />We will be in touch shortly.<br /><br /><a class="DFormClose" href="javascript:void(0)" style="color:#fff;text-decoration:underline;">OK</a></span>')
        .css({
            width: '50%',
            position:'absolute',
            top:'25%',
            left:'50%',
            marginLeft:'-25%',
            background:'#3B5998',
            textAlign: 'center',
            padding:'20px 0',
            fontSize:'1.4em'
        });
        $('.DFormClose').click(function(){
            $('#DFormSuccess').remove();
            if(typeof callBack == 'function') callBack();
        })
    }
}
