/*
		CSS Browser Selector v0.4.0 (Nov 02, 2010)
		Rafael Lima (http://rafael.adm.br)
		http://rafael.adm.br/css_browser_selector
		License: 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',m='mobile',h=document.documentElement,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.6')?g+' ff3 ff3_6':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('blackberry')?m+' blackberry':is('android')?m+' android':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?m+' j2me':is('iphone')?m+' iphone':is('ipod')?m+' ipod':is('ipad')?m+' ipad':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win'+(is('windows nt 6.0')?' vista':''):is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);

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 PostBackOnReturn(event, postbackMethod) {
    if (event) {
        if (event.keyCode == 13) {
            __doPostBack(postbackMethod, '');
        }
    }
}
String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, ''); }

function resizeLightbox(height) {
    $('.lightboxContent').attr('height', height + 'px');
}
function goToByScrollY(id) {
    $('html,body').animate({ scrollTop: 0 }, 'slow');
}
function goToByScroll(id) {
    $('html,body').animate({ scrollTop: $("#" + id).offset().top }, 'slow');
}
function goToByScrollObj(el) {
    $('html,body').animate({ scrollTop: el.offset().top }, 'slow');
}
function closeLightBox() {
    $('#lightBox').hide();
    $('.lightBoxBg').hide();
}

$(document).ready(function () {
    $('.mainNav fieldset input').focus(function () {
        $('.mainNav fieldset, .mainNav fieldset a.btnSearch').addClass('focus');
    });
    $('.mainNav fieldset input').focusout(function () {
        $('.mainNav fieldset, .mainNav fieldset a.btnSearch').removeClass('focus');
    });
    $('fieldset.askUs textarea').click(function () {
        $(this).siblings('input').show();
    });

    setupDefaultTextReset($('.mainNav input'), 'SEARCH');

    // LIGHTBOX
    $('a.btnOpenLightBox').click(function () {
        $('.lightBoxBg').show();
        $('#lightBox').show();
        var itemId = $(this).attr("name");
        drawIFrame(itemId);
        positionLightBox();
        return false;
    });

    $('a.btnCloseLightBox').click(function () {
        alert("we are here!");
        $('.lightBoxBg').hide();
        $('#lightBox').hide();
    });

    $('.lightBoxBg').click(function () {
        $('.lightBoxBg').hide();
        $('#lightBox').hide();
    });

    function drawIFrame(itemId) {
        var winH = $('body').outerHeight();
        var parentId = $('#hdId').val();
        $('#lightBox').html('<a href="#" class="btnCloseLightBox" onClick="closeLightBox()">close</a><iframe src="/LightBoxContent.aspx?id=' + itemId + '&parent=' + parentId + '" frameborder="0" scrolling="no" width="800" class="lightboxContent"></iframe> <a href="#" class="btnCloseLightBox lbFooter" onClick="closeLightBox()">close</a>');
    }

    function positionLightBox() {
        // popup gallery button positioning
        var winH = $(window).height();
        var winW = $('body').outerWidth();
        var popGalW = $('.lightBox').width();
        $('#lightBox').css('left', ((winW / 2) - ($('#lightBox').width() / 2)) + 'px');
    }
    
    // REFERENCES DESCRIPTION TOGGLER
    $('.referenceRow a.btnToggleDescription').click(function () {
        $(this).siblings('.text').css('height', 'auto'),
	    $(this).addClass('active');
        $(this).bind('click', activeClick);
    });
    function activeClick() {
        $(this).siblings('.text').css('height', '160px'),
	    $(this).removeClass('active');
        $(this).unbind('click', activeClick);
    }

    // INTERACTIVE VIDEO PANEL 
    $('a.btnInteractive').click(function () {
        $('.btnInteractiveOpen').show();
        return false;
    });
    $('.btnInteractiveOpen a').click(function () {
        $('.btnInteractiveOpen').hide();
        return false;
    });

    //footer contact form
    setupDefaultTextReset($('#tbFooterMessage'), 'Type here to send us a message');
    setupDefaultTextReset($('#tbFooterName'), 'Your Name');
    setupDefaultTextReset($('#tbFooterEmail'), 'Your Email');
    $('#lbFooterSend').click(function () {
        //validation
        $('#pFooterError').html('');
        $('#pFooterError').hide();
        if ($('#tbFooterMessage').val().trim() == '' ||
            $('#tbFooterMessage').val() == 'Type here to send us a message') {
            $('#pFooterError').append('Please enter your message<br />');
        }
        if ($('#ddlFooterSubject').val() == 'Select Message Subject') {
            $('#pFooterError').append('Please select your subject<br />');
        }
        if ($('#tbFooterName').val().trim() == '' ||
            $('#tbFooterName').val() == 'Your Name') {
            $('#pFooterError').append('Please enter your name<br />');
        }
        if ($('#tbFooterEmail').val().trim() == '' ||
            $('#tbFooterEmail').val() == 'Your Email') {
            $('#pFooterError').append('Please enter your email<br />');
        }
        else if (!isValidEmail($('#tbFooterEmail').val())) {
            $('#pFooterError').append('Please enter a valid email<br />');
        }

        if ($('#pFooterError').html() != '') {
            //error
            $('#pFooterError').show();
        }
        else {
            //send contact
            $('#divFooterWait').show();
            $.post("/handlers/ContactHandler.ashx", { message: $('#tbFooterMessage').val(), subject: $('#ddlFooterSubject').val(), name: $('#tbFooterName').val(), email: $('#tbFooterEmail').val() }, function (data) {
                $('#divFooterWait').hide();

                if (!data.Error) {
                    $('#pFooterSuccess').html(data.SuccessMessage);
                    $('#pFooterSuccess').show();

                    $('#tbFooterMessage, #ddlFooterSubject, #tbFooterName, #tbFooterEmail, #lbFooterSend').hide();
                }
                else {
                    $('#pFooterError').html('An error had occurred. Please try again later.');
                    $('#pFooterError').show();
                }
            });
        }

        return false;
    });

    //ask us about
    setupDefaultTextReset($('#tbSideAskMessage'), 'Type here to send us a message');
    setupDefaultTextReset($('#tbSideAskName'), 'Your Name');
    setupDefaultTextReset($('#tbSideAskEmail'), 'Your Email');
    $('#lbSideAskSend').click(function () {
        //validation
        $('#pSideAskError').html('');
        $('#pSideAskError').hide();
        if ($('#tbSideAskMessage').val().trim() == '' ||
            $('#tbSideAskMessage').val() == 'Type here to send us a message') {
            $('#pSideAskError').append('Please enter your message<br />');
        }
        if ($('#tbSideAskName').val().trim() == '' ||
            $('#tbSideAskName').val() == 'Your Name') {
            $('#pSideAskError').append('Please enter your name<br />');
        }
        if ($('#tbSideAskEmail').val().trim() == '' ||
            $('#tbSideAskEmail').val() == 'Your Email') {
            $('#pSideAskError').append('Please enter your email<br />');
        }
        else if (!isValidEmail($('#tbSideAskEmail').val())) {
            $('#pSideAskError').append('Please enter a valid email<br />');
        }

        if ($('#pSideAskError').html() != '') {
            //error
            $('#pSideAskError').show();
        }
        else {
            //send contact
            $('#divSideAskWait').show();
            $.post("/handlers/SideAskHandler.ashx", { pageurl: document.location.href, message: $('#tbSideAskMessage').val(), name: $('#tbSideAskName').val(), email: $('#tbSideAskEmail').val() }, function (data) {
                $('#divSideAskWait').hide();

                if (!data.Error) {
                    $('#pSideAskSuccess').html(data.SuccessMessage);
                    $('#pSideAskSuccess').show();

                    $('#tbSideAskMessage, #tbSideAskName, #tbSideAskEmail, #lbSideAskSend').hide();
                }
                else {
                    $('#pSideAskError').html('An error had occurred. Please try again later.');
                    $('#pSideAskError').show();
                }
            });
        }

        return false;
    });

    $('.tweet').click(function () {
        var url = 'http://twitter.com/share?url=' + encodeURIComponent(window.location);
        if ($(this).attr('href') != '#')
            url = 'http://twitter.com/share?url=' + encodeURIComponent($(this).attr('href'));
        window.open(url, 'twitter', 'width=500,height=300');
        return false;
    });
    $('.fbShare').click(function () {
        var url = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(window.location);
        if ($(this).attr('href') != '#')
            url = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent($(this).attr('href'));
        window.open(url, 'facebook', 'width=500,height=300');
        return false;
    });
});

function showProductInfo(itemId) {
    if ($('#productButton-' + itemId).length > 0) {
        $('#productButton-' + itemId).click();
    }
}

