/*CSS Browser Selector v0.3.4 (Sep 29, 2009)Rafael Lima (http://rafael.adm.br)http://rafael.adm.br/css_browser_selectorLicense: 
http://creativecommons.org/licenses/by/2.5/Contributors: http://rafael.adm.br/css_browser_selector#contributors*/
function css_browser_selector(u) { var ua = u.toLowerCase(), is = function(t) { return ua.indexOf(t) > -1; }, g = 'gecko', w = 'webkit', s = 'safari', o = 'opera', h = document.getElementsByTagName('html')[0], b = [(!(/opera|webtv/i.test(ua)) && /msie\s(\d)/.test(ua)) ? ('ie ie' + RegExp.$1) : is('firefox/2') ? g + ' ff2' : is('firefox/3.5') ? g + ' ff3 ff3_5' : is('firefox/3') ? g + ' ff3' : is('gecko/') ? g : is('opera') ? o + (/version\/(\d+)/.test(ua) ? ' ' + o + RegExp.$1 : (/opera(\s|\/)(\d+)/.test(ua) ? ' ' + o + RegExp.$2 : '')) : is('konqueror') ? 'konqueror' : is('chrome') ? w + ' chrome' : is('iron') ? w + ' iron' : is('applewebkit/') ? w + ' ' + s + (/version\/(\d+)/.test(ua) ? ' ' + s + RegExp.$1 : '') : is('mozilla/') ? g : '', is('j2me') ? 'mobile' : is('iphone') ? 'iphone' : is('ipod') ? 'ipod' : is('mac') ? 'mac' : is('darwin') ? 'mac' : is('webtv') ? 'webtv' : is('win') ? 'win' : is('freebsd') ? 'freebsd' : (is('x11') || is('linux')) ? 'linux' : '', 'js']; c = b.join(' '); h.className += ' ' + c; return c; }; css_browser_selector(navigator.userAgent);

$(document).ready(function() {
    $('#openList').click(function() {
        $(this).css('display', 'none');
        $('#clientList').slideDown();
        $('#closeList').css('display', 'block');
    });
    $('#closeList').click(function() {
        $(this).css('display', 'none');
        $('#clientList').slideUp();
        $('#openList').css('display', 'block');
    });

    //contact form
    setupDefaultTextReset($('div.footerForm #tbMessage'), 'Message');
    setupDefaultTextReset($('div#FormContainer textarea.comment'), 'Message');
    setupDefaultTextReset($('div.footerForm #tbName'), 'Your Name');
    setupDefaultTextReset($('div#FormContainer input.commentorName'), 'Your Name');
    setupDefaultTextReset($('div.footerForm #tbEmail'), 'Your Email');
    setupDefaultTextReset($('div#FormContainer input.commentorEmail'), 'Your Email');
    setupDefaultTextReset($('div.footerForm #tbCaptcha'), '?');

    $('div.footerForm a.buttonSend').click(function() {
        //validation
        $('div.error').html('');
        $('div.error').css('display', 'none');
        if (trim($('div.footerForm #tbMessage').val()) == '' ||
            $('div.footerForm #tbMessage').val() == 'Message') {
            $('div.error').append('Please enter your message<br />');
        }
        if (trim($('div.footerForm #tbName').val()) == '' ||
            $('div.footerForm #tbName').val() == 'Your Name') {
            $('div.error').append('Please enter your name<br />');
        }
        if (trim($('div.footerForm #tbEmail').val()) == '' ||
            $('div.footerForm #tbEmail').val() == 'Your Email') {
            $('div.error').append('Please enter your email<br />');
        }
        else if (!isValidEmail($('div.footerForm #tbEmail').val())) {
            $('div.error').append('Please enter a valid email<br />');
        }
        /*if (trim($('div.footerForm #tbCaptcha').val()) == '' ||
        $('div.footerForm #tbCaptcha').val() == '?') {
        $('div.error').append('Please enter how many fingers you see<br />');
        }*/
        if ($('div.error').html() != '') {
            //error
            $('div.error').css('display', 'block');
        }
        else {
            //send contact
            $('div.wait').css('display', 'block');
            $.post("/handlers/ContactHandler.ashx", { message: $('div.footerForm #tbMessage').val(), name: $('div.footerForm #tbName').val(), email: $('div.footerForm #tbEmail').val()/*, captcha: $('div.footerForm #tbCaptcha').val(), hash: $('label.obfuscate span.spanHash').html()*/ }, function(data) {
                $('div.wait').css('display', 'none');

                if (data.indexOf('Error:invalid captcha') != -1) {
                    $('div.error').css('display', 'block');
                    $('div.error').html('Incorrect number of fingers');
                }
                else if (data.indexOf('Error:') != -1) {
                    $('div.error').css('display', 'block');
                    $('div.error').html('An error had occured. Please try again later.');
                }
                else if (data.indexOf('Success') != -1) {
                    $('div.success').css('display', 'block');
                    $('div.footerForm label.obfuscate').css('display', 'none');
                    $('div.footerForm a.buttonSend').css('display', 'none');
                }
            });
        }

        return false;
    });
    var tipdisplayed = false;
    $('div.portfolioDetail a.pItem').click(function(e) {
        if ($('div.portfolioDetail a.pItem span.state').html() == '0') {
            $('div.portfolioDetail a.pItem').animate({ height: $('span.inner').height() }, 1000);
            $('div.portfolioDetail a.pItem span.state').html('1');
            $('div.portfolioDetail a.pItem span#spanMoreLess').removeClass('moreImageButton');
            $('div.portfolioDetail a.pItem span#spanMoreLess').addClass('lessImageButton');
            if (!tipdisplayed) {
                tipdisplayed = true;
                $('div.portfolioDetail span.viewlesstip').css('position', 'absolute');
                $('div.portfolioDetail span.viewlesstip').css('left', e.pageX + 20);
                $('div.portfolioDetail span.viewlesstip').css('top', e.pageY - 10);
                $('div.portfolioDetail span.viewlesstip').fadeIn(500, function() {
                    setTimeout(function() {
                        $('div.portfolioDetail span.viewlesstip').fadeOut(1000, null);
                    }, 10000);
                });
            }
        }
        else {
            $('div.portfolioDetail a.pItem').animate({ height: '320' }, 1000);
            $('div.portfolioDetail a.pItem span.state').html('0');
            $('div.portfolioDetail a.pItem span#spanMoreLess').removeClass('lessImageButton');
            $('div.portfolioDetail a.pItem span#spanMoreLess').addClass('moreImageButton');
        }
    });
    $('div.indexPortfolio a.pItem span.inner img').click(function() {
        window.location = $(this).parents('a.pItem').attr('href');
    });
    $('div.portfolioDetail a.pItem span.inner img').mousemove(function(e) {
        $('div.portfolioDetail span.viewlesstip').css('left', e.pageX + 20);
        $('div.portfolioDetail span.viewlesstip').css('top', e.pageY - 10);
    });
    $('div.portfolioDetail a.pItem span.inner img').mouseout(function() {
        $('div.portfolioDetail span.viewlesstip').css('display', 'none');
    });

    //subscribe functions
    $('#fsSubscribe').mouseover(function() {
        $('#fsSubscribe>label').fadeOut(100);
    });
    $('#fsSubscribe>a.buttonSignup').click(function() {
        subscribe();
        return false;
    });
    $('input:text').focus(function(event) {
        $(this).addClass('focus');
    });
    $('input:text').blur(function(event) {
        $(this).removeClass('focus');
    });
    $('#fsSubscribe input#tbUpdates').keyup(function(event) {
        if (event.keyCode == '13') {
            subscribe();
        }
    });
    setupDefaultTextReset($('#fsSubscribe input#tbUpdates'), 'type your email address here');
});
function subscribe() {
    var email = $('#fsSubscribe input#tbUpdates').val();
    $('#fsSubscribe span.error').html('');
    $('#fsSubscribe input#tbUpdates').removeClass('error');

    if (trim(email) == '' ||
        trim(email) == 'type your email address here') {
        $('#fsSubscribe span.error').html('Please enter your email address');
    }
    else if (!isValidEmail(email)) {
        $('#fsSubscribe span.error').html('Please enter a valid email address');
    }
    if ($('#fsSubscribe span.error').html() != '') {
        $('#fsSubscribe span.error').fadeIn(500);
        $('#fsSubscribe input#tbUpdates').addClass('error');
        setTimeout(function() { $('#fsSubscribe span.error').fadeOut(500); }, 3000);
    }
    else {
        //send
        $('#fsSubscribe span.wait').show();
        $.post("/handlers/SubscriptionHandler.ashx", { email: email }, function(data) {
            $('#fsSubscribe span.wait').hide();

            if (data.indexOf('Error:') != -1) {
                $('#fsSubscribe span.error').fadeIn(500);
                $('#fsSubscribe span.error').html('An error had occured. Please try again later.');
            }
            else if (data.indexOf('Success') != -1) {
                $('#fsSubscribe span.success').fadeIn(500);
            }
        });
    }
}
function setupDefaultTextReset(obj, message) {
    obj.click(function() {
        if (obj.val() == message)
            obj.val('');
    });
    obj.blur(function() {
        if (obj.val() == '')
            obj.val(message);
    });
}
function isValidEmail(email) {
    var regex = /([\w\d\-_]+)(\.[\w\d\-_]+)*@([\w\d\-_]+\.)([\w\d\-_]+\.)*([\w]{2,3})/;
    if (regex.test(email))
        return true;
    else
        return false;
}
function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
    chars = chars || '\\s';
    return str.replace(new RegExp('^[" + chars + "]+', 'g'), '');
}
function rtrim(str, chars) {
    chars = chars || '\\s';
    return str.replace(new RegExp('[" + chars + "]+$', 'g'), '');
}
function PostBackOnReturn(event, postbackMethod) {
    if (event) {
        if (event.keyCode == 13) {
            __doPostBack(postbackMethod, '');
        }
    }
}
String.prototype.startsWith = function(str) { return (this.match("^" + str) == str); }
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
/*~~~~~ start blog ~~~~~*/
$(document).ready(function() {
    $('.blog-reply').click(function() {
        $('.blog-reply-comment').html('');
        $(this).parents('div.comment').nextAll('.blog-reply-comment').eq(0).html(
        '<div class="commentFormContainer" id="replyform">' +
          '<div class="clearer"></div>' +
          '<textarea id="tbBlogCommentComment" name="tbBlogCommentComment" class="comment" rows="5" style="padding:8px 10px;">Message</textarea>' +
          '<label for="tbBlogCommentName"><input id="tbBlogCommentName" name="tbBlogCommentName" type="text" class="commentorName" value="Your Name" /></label>' +
          '<label for="tbBlogCommentEmail"><input id="tbBlogCommentEmail" name="tbBlogCommentEmail" type="text" class="commentorEmail" value="Your Email" /></label>' +
          '<p class="error blog-reply-error" style="display:none"></p>' +
          '<a href="#" class="buttonSend blog-reply-send">Send</a>' +
          '<a href="javascript:void(0)" class="blog-reply-cancel">cancel</a>' +
          '<p class="blog-reply-wait">submitting..</p>' +
          '<div class="clearer"></div>' +
        '</div><div class="clearer"></div>');

        setupDefaultTextReset($('div.blog-reply-comment div.commentFormContainer textarea.comment'), 'Message');
        setupDefaultTextReset($('div.blog-reply-comment div.commentFormContainer input.commentorName'), 'Your Name');
        setupDefaultTextReset($('div.blog-reply-comment div.commentFormContainer input.commentorEmail'), 'Your Email');

        window.location.hash = '#replyform';

        $('.blog-reply-cancel').click(function() {
            $(this).parents('.blog-reply-comment').html('');
        });
        $('.blog-reply-send').click(function() {
            var name = $(this).parent().find('label input[name=tbBlogCommentName]').val();
            var email = $(this).parent().find('label input[name=tbBlogCommentEmail]').val();
            var comment = $(this).siblings('textarea[name=tbBlogCommentComment]').val();

            //validation
            $(this).siblings('p.blog-reply-error').html('');
            $(this).siblings('p.blog-reply-error').css('display', 'none');
            var isValid = true;
            var errorMsg = '';
            if (comment.trim() == '' || comment.trim().toLowerCase() == 'message') {
                isValid = false;
                errorMsg += 'Please enter your message<br />';
            }
            if (name.trim() == '' || name.trim().toLowerCase() == 'your name') {
                isValid = false;
                errorMsg += 'Please enter your name<br />';
            }
            if (email.trim() == '' || email.trim().toLowerCase() == 'your email') {
                isValid = false;
                errorMsg += 'Please enter your email address<br />';
            }
            else if (!isValidEmail(email)) {
                isValid = false;
                errorMsg += 'Please enter a valid email address<br />';
            }
            if (!isValid) {
                $(this).siblings('p.blog-reply-error').html(errorMsg);
                $(this).siblings('p.blog-reply-error').css('display', 'block');
                return false;
            }

            var commentID = $(this).parents('.blog-reply-comment').next('input:hidden').val();
            $('p.blog-reply-wait').html('Submitting... Please Wait');
            $('p.blog-reply-wait').css('display', 'block');

            jQuery.post('/handlers/BlogCommentReply.ashx', { commentid: commentID, name: name, email: email, comment: comment }, function(data) {
                $('p.blog-reply-wait').css('display', 'none');
                if (data.startsWith('Success')) {
                    if (data.startsWith("Success:posted")) {
                        $('#divBlogReplyComment-' + commentID).html('<div class="reply"><p>' + comment + '<br /><span class="commentor">' + name + '</span></p></div>');
                    }
                    else {
                        $('#divBlogReplyComment-' + commentID).html('<div class="reply"><p>' + comment + '<br /><em>Your reply is now submitted and pending approval</em><br /><span class="commentor">' + name + '</span></p></div>');
                    }
                }
                else {
                    $('#divBlogReplyComment-' + commentID + ' div.commentFormContainer p.blog-reply-error').html('An error had occurred. Please try again later.');
                    $('#divBlogReplyComment-' + commentID + ' div.commentFormContainer p.blog-reply-error').css('display', 'block');
                }
            });
            return false;
        });
        return false;
    });
});
/*~~~~~ end blog ~~~~~*/
