$(function(){
    $('#product_val').val("");
    window.body_click = true;
    if(typeof($('#attach_file').val()) != "undefined")
        if($('#attach_file').val() != "") showAttachedFile();

    $('body').click(function(event) {
        if(window.body_click && $('.support-options').css('display') == "block") {
            $('.support-options').hide();
            $('.support-select-button').removeClass('pressed');
        }
        window.body_click = true;
    });

    $('.support-label-check').click(function(event) {
        toggleCheckBoxes($(this).children().html().toLowerCase());
    });

    $('.support-check').click(function(event) {
        toggleCheckBoxes($(this).attr('id'));
    });

    $('.support-label-check2').click(function(event) {
        var id = $(this).attr('id').replace("_label", "");
        if($("#" + id).hasClass("checked")) {
            $("#" + id).removeClass('checked');
            $("#" + id + "_val").val('false');
        } else {
            $("#" + id).addClass('checked');
            $("#" + id + "_val").val('true');
        }
    });

    $('.support-check2').click(function(event) {
        var id = $(this).attr('id');
        if($("#" + id).hasClass("checked")) {
            $("#" + id).removeClass('checked');
            $("#" + id + "_val").val('false');
        } else {
            $("#" + id).addClass('checked');
            $("#" + id + "_val").val('true');
        }
    });

    $('#btn_submit').click(function(event) {
        if(check_form()) {
            $('#support-act').val("send");
            document.forms.support.submit();
            return true;
        }
        return false;
    });

    $('.support-attach').click(function(event) {
        alert('Attach file');
    });

    $('#product_label').click(function() {
        toggleOptionsList();
    });

    $('.selected-item').click(function() {
        toggleOptionsList();
    });

    $('.support-option').click(function() {
        $('.selected-item span').html($(this).html());
        //$('.selected-item').removeClass('default');
        $('#product_val').val($(this).html());
        $('.support-option.selected').each(function() {
            $(this).removeClass('selected');
        });
        $(this).addClass('selected');
    });

    $('#attach_file').change(function() {
        showAttachedFile();
    });

    $('#light').click(function() {
        $('#light').html('<iframe width="640" height="360" src="http://www.youtube.com/embed/HikaEE70604" frameborder="0" allowfullscreen></iframe>');
        $('#fade').hide();
    });

    $('#fade').click(function() {
        $('#light').html('<iframe width="640" height="360" src="http://www.youtube.com/embed/HikaEE70604" frameborder="0" allowfullscreen></iframe>');
        $('#fade').hide();
    });
})

function toggleCheckBoxes(id) {
    if(id == 'none') {
        $('.support-check').each(function() {
            $(this).removeClass('checked');
        });
    } else {
        $('#none').removeClass('checked');
        $("#none_val").val('false');
    }
    if($("#" + id).hasClass("checked")) {
        $("#" + id).removeClass('checked');
        $("#" + id + "_val").val('false');
        if(!$("#iphone").hasClass("checked") && !$("#ipad").hasClass("checked") && !$("#ipod").hasClass("checked")) {
            $("#none").addClass('checked');
            $("#none_val").val('true');
        }
    } else {
        $("#" + id).addClass('checked');
        $("#" + id + "_val").val('true');
    }
}

function showAttachedFile() {
    $('#attached_file strong').html($('#attach_file').val().replace(/(.*)\\/, ""));
    $('#attached_file').show();
}

function toggleOptionsList() {
    window.body_click = false;
    if($('.support-options').css('display') == "none") {
        $('.support-select-button').addClass('pressed');
        $('.support-options').show();
    } else {
        $('.support-options').hide();
        $('.support-select-button').removeClass('pressed');
    }
}

function check_form() {
    var error = "";
    if($('#support_name').val().split(" ").join("") == "") error += " - 'Name' field is empty or incorrect\r\n";
    if($('#support_email').val().split(" ").join("") == "" || !is_valid_email($('#support_email').val())) error += " - 'Email' field is empty or incorrect\r\n";
    if($('#product_val').val().replace(/\s/, '') == "") error += " - 'Product' is not selected\r\n";
    if($('#support_text').val().split(" ").join("") == "") error += " - 'Text' field is empty or incorrect\r\n";

    if(error !== "") {
        error = "Some data is wrong:\r\n\r\n" + error + "\r\nCheck data and fill again.";
        alert(error);
        return false;
    }

    return true;
}

function show_video() {
    var arrPageSizes = ___getPageSize();
    $('#fade').css({
        width:  arrPageSizes[0],
        height: arrPageSizes[1]
    });
    $('#light').html('<iframe width="640" height="360" src="http://www.youtube.com/embed/HikaEE70604" frameborder="0" allowfullscreen></iframe>');
    $('#fade').show();
    return false;
}

function check_subscribe_form() {
    if(!is_valid_email($('#subscribe_mail').val())) {
        alert('E-mail is in wrong format. Please, check your e-mail and try again.');
        return false;
    }
    return true;
}

function check_banner_subscribe_form() {
    if(!is_valid_email($('#banner_subscribe_mail').val())) {
        alert('E-mail is in wrong format. Please, check your e-mail and try again.');
        return false;
    }
    return true;
}

function is_valid_email(email) {
    var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return filter.test(email);
}

function ___getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {    
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {    // all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth; 
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }    
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else { 
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){    
        pageWidth = xScroll;        
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
};

