function openWindow(url,width,height) {day = new Date();id = day.getTime();window.open(url, id, "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+",left="+((screen.width - width)/2)+",top="+((screen.height - height)/2));return false;}
function isEmail(address) {var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;if(reg.test(address) == false) {return false;} else {return true;}}
function externalLinks() {if (!document.getElementsByTagName) return;var anchors = document.getElementsByTagName("a");for (var i=0; i<anchors.length; i++) {var anchor = anchors[i];if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")anchor.target = "_blank";}}

$(document).ready(function() {
    browserDetection();
    fixIE6alphaTransparency();
    externalLinks();
    attachNewsticker();
    attachScrollbars();
    attachSlide();
    attachSounds();
    attachFormContact();
    attachFormLogin();
    attachFormTeacher();
    attachUploader();
    attachPlayers();
    attachFaqs();
});

function attachNewsticker() {
    $('.ticker').newsticker();
}

function attachScrollbars() {
    if (document.getElementById("scrollbars") != null) {
        $('#scrollbars').jScrollPane({
            showArrows:true,
            scrollbarWidth:16,
            scrollbarMargin:40,
            dragMinHeight:16,
            dragMaxHeight:21,
            topCapHeight: 25,
            bottomCapHeight: 25,
            reinitialiseOnImageLoad: true
        });
    }
}

function attachFaqs() {
    if ($('dl.faqs').length > 0) {
        $('dl.faqs dd').hide();
        $('dl.faqs dt').click(function () {
            $(this).next().toggle();
        });
    }
}

function attachSlide() {
    if ($('#slide').length > 0) {
        flashembed('slide', {src: "resources/media/slide.swf", wmode: 'transparent'});
    }
}

function attachSounds() {
    if ($('#sounds').length > 0) {
        flashembed('sounds', {src: "resources/media/sound.swf", wmode: 'transparent'});
    }
}

function attachFormContact() {
    if ($('#form-contact').length > 0) {
        $('#form-contact-submit').click(function() {
            $('#response').html('<span class="loader"></span>');
            $.post('ajax/contact.php', $("#form-contact").serialize(), function (data, textStatus) {
                $("#response").empty();
                $("#response").html(data['message']);
            }, 'json');
            return false;
        });
    }
}

function attachFormLogin() {
    if ($('#form-login').length > 0) {
        $('#form-login-submit').click(function() {
            $('#response').html('<span class="loader"></span>');
            $.post('ajax/login.php', $("#form-login").serialize(), function (data, textStatus) {
                if (data['code'] == 1) {
                    window.location = data['url'];
                }
                $("#response").empty();
                $("#response").html(data['message']);
            }, 'json');
            return false;
        });
    }
}

function attachFormTeacher() {
    if ($('#form-teacher').length > 0) {
        $('#form-teacher-submit').click(function() {
            $('#response').html('<span class="loader"></span>');
            $.post('ajax/teacher.php', $("#form-teacher").serialize(), function (data, textStatus) {
                $("#response").empty();
                $("#response").html(data['message']);
            }, 'json');
            return false;
        });
    }
}

function attachUploader() {
    if ($('#photo').length > 0) {
        var upload1 = new AjaxUpload('photo', {
            action: 'ajax/upload.php',
            name: 'source[]',
            data: {
                type : 'photo',
                key : $('#token').attr('value')
            },
            autoSubmit: true,
            responseType: 'json',
            onSubmit : function(file , ext){
                if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
                    alert('Error: invalid file extension.');
                    return false;
                } else {
                    $('#photo').remove();
                    $('#photo-upload').html('<span class="loader"></span>');
                }
            },
            onComplete: function(file, response) {
                $('#photo-upload').empty();
                $('#photo-upload').html(response['message']);
                upload1.destroy();
            }
        });
        var upload2 = new AjaxUpload('video', {
            action: 'ajax/upload.php',
            name: 'source[]',
            data: {
                type : 'video',
                key : $('#token').attr('value')
            },
            autoSubmit: true,
            responseType: 'json',
            onSubmit : function(file , ext){
                if (! (ext && /^(avi|mpeg|mov|mpg|wmv|mp4)$/.test(ext))){
                    alert('Error: invalid file extension.');
                    return false;
                } else {
                    $('#video').remove();
                    $('#video-upload').html('<span class="loader"></span>');
                }
            },
            onComplete: function(file, response) {
                $('#video-upload').empty();
                $('#video-upload').html(response['message']);
                upload2.destroy();
            }
        });
        var upload3 = new AjaxUpload('video1', {
            action: 'ajax/upload.php',
            name: 'source[]',
            data: {
                type : 'video1',
                key : $('#token').attr('value')
            },
            autoSubmit: true,
            responseType: 'json',
            onSubmit : function(file , ext){
                if (! (ext && /^(avi|mpeg|mov|mpg|wmv|mp4)$/.test(ext))){
                    alert('Error: invalid file extension.');
                    return false;
                } else {
                    $('#video1').remove();
                    $('#video1-upload').html('<span class="loader"></span>');
                }
            },
            onComplete: function(file, response) {
                $('#video1-upload').empty();
                $('#video1-upload').html(response['message']);
                upload3.destroy();
            }
        });
        var upload4 = new AjaxUpload('video2', {
            action: 'ajax/upload.php',
            name: 'source[]',
            data: {
                type : 'video2',
                key : $('#token').attr('value')
            },
            autoSubmit: true,
            responseType: 'json',
            onSubmit : function(file , ext){
                if (! (ext && /^(avi|mpeg|mov|mpg|wmv|mp4)$/.test(ext))){
                    alert('Error: invalid file extension.');
                    return false;
                } else {
                    $('#video2').remove();
                    $('#video2-upload').html('<span class="loader"></span>');
                }
            },
            onComplete: function(file, response) {
                $('#video2-upload').empty();
                $('#video2-upload').html(response['message']);
                upload4.destroy();
            }
        });
    }
}

function attachPlayers() {
    if ($('#video-player1').length > 0) {
        $f("video-player1", "resources/media/flowplayer-3.1.2.swf", {clip: {autoPlay: false,autoBuffering: true}});
    }
    if ($('#video-player2').length > 0) {
        $f("video-player2", "resources/media/flowplayer-3.1.2.swf", {clip: {autoPlay: false,autoBuffering: true}});
    }
    if ($('#video-player3').length > 0) {
        $f("video-player3", "resources/media/flowplayer-3.1.2.swf", {clip: {autoPlay: false,autoBuffering: true}});
    }
}