$.ajaxSetup({
	type: 'GET',
	timeout: 30000,
	dataType: 'html',
	error: function(xhr) {
            alert('Error: ' + xhr.status + ' ' + xhr.statusText);
	}
});


function toggleLanguageSelector() {
    if ($('#language_selector_box').css('display') == 'none')
        $('#language_selector_top').addClass('language_selector_top_bg');
    else
        $('#language_selector_top').removeClass('language_selector_top_bg')
    if ($('#support_panel').css('display') == 'block')
        $('#support_panel').slideToggle('fast');
    $('#language_selector_box').slideToggle('fast');
}

function toggleProductSelector() {
    if ($('#product_selector_box').css('display') == 'none') {
        if ($('#tool_selector_box').css('display') == 'block')
            toggleToolSelector();
        $('#product_selector_top').addClass('product_selector_top_bg');
        $('#toggle_product_selector').addClass('product_selected_menu');
    }
    else {
        $('#product_selector_top').removeClass('product_selector_top_bg');
        $('#toggle_product_selector').removeClass('product_selected_menu');
    }
    $('#product_selector_box').slideToggle('fast');
}

function toggleToolSelector() {
    if ($('#tool_selector_box').css('display') == 'none') {
        if ($('#product_selector_box').css('display') == 'block')
            toggleProductSelector();
        $('#tool_selector_top').addClass('tool_selector_top_bg');
        $('#toggle_tool_selector').addClass('tool_selected_menu');
    }
    else {
        $('#tool_selector_top').removeClass('tool_selector_top_bg');
        $('#toggle_tool_selector').removeClass('tool_selected_menu');
    }
    $('#tool_selector_box').slideToggle('fast');
}

function switchTab(tab, target) {
    //alert($('#language').html());
    target_id = '#' + target;
    $(".tab_content_container").each(function(){
        $(this).hide();
    });
    $('#tabContainer li a').removeClass('active');
    $(tab).addClass('active');
    $(target_id).show();
    if (target != 'tab_content_1' && target != 'tab_content_6') {
        if ($(target_id).html() == '') {
            $(target_id).html($('#dialog_loader').html());
            $(target_id).load('/product-v3.php?ajax=lang='+$('#language').html()+'&product_id='+$('#product_id').html()+'&tab='+target);
        }
    }
}

function loadDialog(click_id) {
    show_id = '#dialog';
    $(show_id).html($('#dialog_loader').html());
    $(show_id).dialog('open');
    $.ajax({
        type: 'GET',
        url: '/load-file.php',
        data: 'url=' + escape($(click_id).attr('href')) + '&id=' + click_id + '&r=' + Math.random(),
        dataType: 'html',
        success: function(response) {
            $(show_id).html(response);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            $(show_id).html('XML HTTP REQUEST: ' + XMLHttpRequest + ' TEXT STATUS: ' + textStatus + ' ERROR THROWN: ' + errorThrown);
        }
    });
}

function showTooltip() {
    if ($('.mouseover_text').css('display') == 'none')
        $('.mouseover_text').slideDown('fast');
}

function hideTooltip() {
    if ($('.mouseover_text').css('display') == 'block')
        $('.mouseover_text').slideUp('fast');
}

function showMoreInfo(id) {
    e_id = '#' + id;
    if ($(e_id).css('display') == 'none') {
        $(e_id).css('top', 0); //reset
        $(e_id).css('display', 'block');
        w_height = $(window).height();
//        w_width = $('.main_content').width()
        t_height = $(e_id).height();
        t_offset_y = $(e_id).offset().top;
//        t_width = $(e_id).width();
        scrollTop = $(window).scrollTop();
//        $(e_id).css('top', (w_height - t_height)/2 + (scrollTop > 0 ? scrollTop - 85 : 0 - 85) + 'px');
//        $(e_id).css('left', (w_width - t_width)/2 + 'px');
        if ((t_height + t_offset_y - scrollTop) > w_height - 10)
            $(e_id).css('top', - t_height + 10);
        //alert('Window Height: ' + w_height + 'px\n' + 'Element Height: ' + t_height + 'px\n' + 'Element Offset Top y: ' + t_offset_y + 'px\n' + 'Scroll Top: ' + scrollTop + 'px');
    }
        //$('#' + id).slideDown('fast');
}

function hideMoreInfo(id) {
    if ($('#' + id).css('display') == 'block')
        $('#' + id).css('display', 'none')
        //$('#' + id).slideUp('fast');
}

function getDimensions() {
    alert($(window).width());
    alert($(window).height());
}

$(document).ready(function(){
    //$("body").css("overflow", "auto");

    $('#toggle_language_selector').click(function() {
        toggleLanguageSelector();
    });

    $('#language_selector_box').mouseleave(function() {
        toggleLanguageSelector();
    });

    $('#toggle_product_selector').click(function() {
        toggleProductSelector();
    });

    $('#product_selector_box').mouseleave(function() {
        toggleProductSelector();
    });

    $('#toggle_tool_selector').click(function() {
        toggleToolSelector();
    });

    $('#tool_selector_box').mouseleave(function() {
        toggleToolSelector();
    });

    $('#customer_service').click(function() {
        if ($('#language_selector_box').css('display') == 'block')
                toggleLanguageSelector();
            $('#support_panel').slideToggle('fast');
    });

    $('#support_panel').mouseleave(function() {
        $('#support_panel').slideToggle('fast');
    });

    $('#see_more_testimonials').click(function() {
        if ($('#more_testimonials').css('display') == 'none') {
            $('#more_testimonials').slideDown('normal');
            $('#see_more_testimonials_box').css('display', 'none');
            $('#see_less_testimonials_box').css('display', 'block');
            $('#more_testimonials').css('display', 'block');
        }
    });
    $('#see_less_testimonials').click(function() {
        if ($('#more_testimonials').css('display') == 'block') {
            $('#more_testimonials').slideUp('normal');
            $('#see_less_testimonials_box').css('display', 'none');
            $('#see_more_testimonials_box').css('display', 'block');
            $('#more_testimonials').css('display', 'none');
        }
    });

    $('#see_more_awards').click(function() {
        if ($('#more_awards').css('display') == 'none') {
            $('#more_awards').slideDown('normal');
            $('#see_more_awards_box').css('display', 'none');
            $('#see_less_awards_box').css('display', 'block');
            $('#more_awards').css('display', 'block');
        }
    });
    $('#see_less_awards').click(function() {
        if ($('#more_awards').css('display') == 'block') {
            $('#more_awards').slideUp('normal');
            $('#see_less_awards_box').css('display', 'none');
            $('#see_more_awards_box').css('display', 'block');
            $('#more_awards').css('display', 'none');
        }
    });

    $('.open_live_chat').click(function() {
        window.open('/livesupport/livehelp.php?department='+$('#chat_department').html()+'&amp;cslheg=1&amp;serversession=1', 'chat54050872', 'width=585,height=390,menubar=no,scrollbars=1,resizable=1');
    });

    $('#buy_now').click(function() {
        window.location.href = ($('#buy_now_url').html().replace(/&amp;/gi, '&'));
    });

    $('#download_trial').click(function() {
        window.location.href = ($('#download_trial_url').html().replace(/&amp;/gi, '&'));
    });

//    $('.create_account').click(function() {
//        //window.location.href = ($('#create_account_url').html().replace(/&amp;/gi, '&'));
//    });

    $('button#continue_button').mouseover(function() {
        continue_button = $('#continue_button_img').attr('src');
        continue_button = continue_button.replace('button_continue', 'button_continue_on');
        $('#continue_button_img').attr('src', continue_button);
    });

    $('button#continue_button').mouseout(function() {
        continue_button = $('#continue_button_img').attr('src');
        continue_button = continue_button.replace('button_continue_on', 'button_continue');
        $('#continue_button_img').attr('src', continue_button);
    });

    $('button#login_button').mouseover(function() {
        login_button = $('#login_button_img').attr('src');
        login_button = login_button.replace('button_login', 'button_login_on');
        $('#login_button_img').attr('src', login_button);
    });

    $('button#login_button').mouseout(function() {
        login_button = $('#login_button_img').attr('src');
        login_button = login_button.replace('button_login_on', 'button_login');
        $('#login_button_img').attr('src', login_button);
    });

    $('button#create_account_button').mouseover(function() {
        create_account_button = $('#create_account_button_img').attr('src');
        create_account_button = create_account_button.replace('button_create_account', 'button_create_account_on');
        $('#create_account_button_img').attr('src', create_account_button);
    });

    $('button#create_account_button').mouseout(function() {
        create_account_button = $('#create_account_button_img').attr('src');
        create_account_button = create_account_button.replace('button_create_account_on', 'button_create_account');
        $('#create_account_button_img').attr('src', create_account_button);
    });

    // Dialog
    $('.dialog, .dialog_3, .dialog_tuneup').click(function() {
        var height = 475;
        var id = '#' + this.id;
        if ($(id).hasClass('mtp_dialogs')) {
            return;
        }
        else if ($(id).hasClass('dialog')) {
            height = 650;
        }
        else if ($(id).hasClass('dialog_3')) {
            height = 475;
        }
        else if ($(id).hasClass('dialog_tuneup')) {
            height = 350;
        }
        else {
            height = 475;
        }
        $("#dialog").dialog('option', 'height', height);        
        title_extra = id == '#join_now_popup' || id == '#join_now_popup2' ? $('#join_msc_now_button').attr('alt') : (id == '#westcoast_labs' ? $(id + '_img').attr('alt') : $(id).html());
        title = id == '#mysecureusb_popup' ? 'by MYSecurityCenter' : 'MYSecurityCenter - ' + title_extra;
        $('#ui-dialog-title-dialog').html(title);
        $(function() {
            $("#dialog").dialog({
                bgiframe: true,
                width: 600,
                height: height,
                resizable: false,
                autoOpen: false,
                modal: modal,
                title: title,
                show: 'slide',
                zIndex: 9999999,
                open: function(event, ui) {
                    //alert(jQuery.browser.version);
                    if (jQuery.browser.mozilla) {
                        $('body').css('overflow-y', 'hidden');
                    }
                    else {
                        $('html').css('overflow-y', 'hidden');
                    }
                },
                close: function(event, ui) {
                    $("#dialog").dialog('option', 'height', 0);
                    if (jQuery.browser.mozilla) {
                        $('body').css('overflow-y', 'auto');
                    }
                    else {

                        $('html').css('overflow-y', 'auto'); // html {overflow-y: visible;}
                        $('html').css('overflow-x', 'hidden');
                    }
                }
            });
        });
        $("#dialog").focus();
        loadDialog(id);
    });


//  $('.dialog_tuneup').click(function() {
//        //$("body").css("overflow", "hidden");
//        //alert('ID:' + this.id);
//        var id = '#' + this.id;
//        title_extra = id == '#join_now_popup' || id == '#join_now_popup2' || id == '#westcoast_labs' ? $(id).attr('alt') : $(id).html();
//        title = id == '#mysecureusb_popup' ? 'by MYSecurityCenter' : 'MYSecurityCenter - ' + title_extra;
//        $('#ui-dialog-title-dialog').html(title);
//        $(function() {
//            $("#dialog").dialog({
//                bgiframe: true,
//                width: 500,
//                height: 350,
//                resizable: false,
//                autoOpen: true,
//                modal: modal,
//                title: title,
//                show: 'slide',
//                zIndex: 9999999
//            });
//        });
//        $("#dialog").focus();
//        loadDialog(id);
//    });

    $('.dialog_2').click(function() {
        //$("body").css("overflow", "hidden");
        //var id = '#' + this.id;
        title = 'by MYSecurityCenter';
        $('#ui-dialog-title-dialog').html(title);
        $(function() {
            $("#dialog_2").dialog({
                bgiframe: true,
                width: 600,
                height: 225,
                resizable: false,
                autoOpen: false,
                modal: modal,
                title: title,
                show: 'slide'
            });
        });
       $('#dialog_2').dialog('open');
    });

//    $('.dialog_3').click(function() {
//        $("#dialog").dialog('destroy');
//        title = 'by MYSecurityCenter';
//        var id = '#' + this.id;
//        $('#ui-dialog-title-dialog').html(title);
//        $(function() {
//            $("#dialog").dialog({
//                bgiframe: true,
//                width: 600,
//                height: 450,
//                resizable: false,
//                autoOpen: false,
//                modal: modal,
//                title: title,
//                show: 'slide'
//            });
//        });
//       $('#dialog').dialog('open');
//       loadDialog(id);
//    });

});

function submitPopupRequestForm(popupId) {
    url = '';
    switch (popupId) {
        case 1:url = '/mysecureusb-popup.php';break;
        case 2:url = '/join-msc-popup.php';break;
    }
    $('#submit').css('display', 'none');
    $('#submit_loader').css('display', 'block');
    $.getJSON(
        url,
        {
        ajax: '',
        lang: $('#language').html(),
        submit_request: '',
        name: $('#name').attr('value'),
        email: $('#email').attr('value'),
        dial_code: $('#dial_code').attr('value'),
        telephone: $('#telephone').attr('value')
        },
        function(response) {
            if (response.name.status) {
                $('#name_status').removeClass('error');
                $('#name_status').addClass('success');
                $('#name_error').html('');
            }
            else {
                $('#name_status').removeClass('success');
                $('#name_status').addClass('error');
                $('#name_error').html(response.name.error);
            }
            if (response.email.status) {
                $('#email_status').removeClass('error');
                $('#email_status').addClass('success');
                $('#email_error').html('');
            }
            else {
                $('#email_status').removeClass('success');
                $('#email_status').addClass('error');
                $('#email_error').html(response.email.error);
            }
            if (response.telephone.status) {
                $('#telephone_status').removeClass('error');
                $('#telephone_status').addClass('success');
                $('#telephone_error').html('');
            }
            else {
                $('#telephone_status').removeClass('success');
                $('#telephone_status').addClass('error');
                $('#telephone_error').html(response.telephone.error);
            }
            $('#name').attr('value', response.name.value);
            $('#email').attr('value', response.email.value);
            $('#telephone').attr('value', response.telephone.value);
            if (response.valid_status) {
                if (!response.db.status) {
                    $('#confirmation').html(response.db.error);
                }
                response = null;
                $('#request_form').css('display', 'none');
                $('#confirmation').css('display', 'block');
                $('#auto_close').css('display', 'block');
                displayTimeOut();
            }
            else {
                $('#submit_loader').css('display', 'none');
                $('#submit').css('display', 'block');
            }
        }
    );
}

var seconds = 30;
var intervalId;
function displayTimeOut() {
    $('#seconds_left').html(seconds--);
    if (seconds <= 0) {
        clearInterval(intervalId);
        $('#dialog').dialog('close');
        $('#dialog').html('');
        seconds = 30;
        return;
    }
    intervalId = setTimeout("displayTimeOut()", 1000);
}

