﻿var ui = function() {
    var self;
    var controls = {};
    var strings = {};

    return {
        init: function(args) {
            this.controls = args.controls;
            this.strings = args.strings;
            self = this;
            if (args.hasForm) this.form_init();
        },
        default_init: function(args) {
            this.init(args);
            $('textarea').focus(function() { $(this).attr('value', ''); $('email_outer').removeClass('input'); $('email_outer').addClass('input_focus'); });
            $('textarea').blur(function() { if ($(this).val() == '') $(this).attr('value', self.strings.Message); $('email_outer').removeClass('input_focus'); $('email_outer').addClass('input'); });
            $('input').focus(function() { $(this).attr('value', ''); $('message_outer').removeClass('input'); $('message_outer').addClass('input_focus'); });
            $('input').blur(function() { if ($(this).val() == '') $(this).attr('value', self.strings.Email); $('message_outer').removeClass('input_focus'); $('message_outer').addClass('input'); });
            $('#btnSend').click(function() {
                var message = $(self.controls.Message).val().trim();
                var email = $(self.controls.Email).val().trim();
                var valid = (message != self.strings.Message && message != '');
                if (!valid) $(self.controls.Message).effect('highlight', {}, 3000);
                valid = (email != self.strings.Email && email != '' && self.validateEmail(email));
                if (!valid) {
                    $(self.controls.Email).effect('highlight', {}, 3000);
                    $(self.controls.Email).attr('value', self.strings.Email);
                }
                if (valid) {
                    thewebsiteshop.Services.SiteService.WebEnquiry(message, email);
                    $('#contact').fadeOut('slow');
                    $('#contact').hide();
                    $('#thanks').fadeIn('slow');
                    $('#thanks').animate({ opacity: 1.0 }, 3000)
                    $('#thanks').fadeOut('slow', function() {
                        $(self.controls.Message).attr('value', self.strings.Message);
                        $(self.controls.Email).attr('value', self.strings.Email);
                        $('#contact').fadeIn('slow');
                    });
                }
            });
        },
        form_init: function() {
        },
        blog_init: function(args) {
            this.init(args);
            this.make_link(this.controls.feed_item);
            $(this.controls.feed_item).click(function() {
                // TODO : Make a service call to update read count
                window.open($(this).find('input[@type=hidden]')[0].value);
            });
        },
        slider_init: function() {
            if (!$(this.controls.intro_box).visible) return;
            var self = this;
            $(sliderargs[slidercounter]).effect('slide', {}, 3000, function() {
                setInterval('self.slider()', 9000);
            });
        },
        slider: function() {
            var self = this;
            $(sliderargs[slidercounter]).fadeOut(1000, function() {
                self.slidercounter == 6 ? self.slidercounter = 0 : self.slidercounter++;
                $(self.sliderargs[self.slidercounter]).effect('slide', {}, 3000)
            });
        },
        make_link: function(el) {
            $(el).mouseover(function() { $(this).addClass("hover"); });
            $(el).mouseout(function() { $(this).removeClass("hover"); });
        },
        getBriefQuestions: function(sender, args) {
            $.getJSON(serviceUrl + 'GetBriefQuestions', this.loadBriefQuestions);
        },
        loadBriefQuestions: function(sender, args) {
            var items = sender.items;
            //    	            function(data){
            //              $.each(data.items, function(i,item){
            //                $("<img/>").attr("src", item.media.m).appendTo("#images");
            //                if ( i == 3 ) return false;
            //              });
            //            });
        },
        test_init: function(args) {
            var self = this;
            $('#button').click(function() {
                SiteService.GetKeywords('http://www.thewebsiteshop.co.uk', '', self.getKeywordsCompleted);
            });
        },
        getKeywordsCompleted: function() {
            $get("keywords").innerText = results;
        },
        validateEmail: function(inputvalue) {
            var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
            return pattern.test(inputvalue);
        }
    }
};