var _margintop = 50;
var debug = 1;
$(document).ready(function(){
    $('#footer-logo').attr('href', $('#unit-link').text());
    $('.hproduct').live('_selected', _selected);
    $('.hproduct').live('_centered', _centered);
    $('.hproduct').live('_blur', _blur);
    $(document).bind('keydown', function(event){
        if (event.keyCode === 38){
            _back();
        }
    });
    $(document).bind('keydown', flashNav);
    $('#next').live('click', flashNav);
    $('#back').live('click', flashNav);
    //checkHeight();
    //$(window).resize(checkHeight);
    /*$(window).bind('popstate', function(event) {
        var state = evt.originalEvent.state;
        if(state)
            _back();
    });*/
    if (navigator.userAgent.match(/Android|iPhone/i)) {
        $(window).bind('orientationchange', function(event) {
            if (window.orientation == 90 || window.orientation == -90 || window.orientation == 270) {
                $('meta[name="viewport"]').attr('content', 'initial-scale=0.35');
            } else {
                $('meta[name="viewport"]').attr('content', 'initial-scale=0.4');
            }
        }).trigger('orientationchange');
    }
    $('#slider').show();
    if($('.start').hasClass('up')){
        _selected($('.start').first().attr('name'));
    }
    
});

function _selected(item){
    if(item.type==='_selected'){
        item=$(this).attr('name');
    }
    var help = $('textarea[name="'+item+'"]:first');
    var url = '';
    if(help.hasClass('link')){
        MeinFenster = window.open(help.attr('href'));
        MeinFenster.focus();
    } else {
        if(help.attr('types')==='em_website_navigation' && debug==0){
            url = $('#navigation_link').attr('href');
            try{
                var stateObj = {node: item};
                var state = help.attr('href');
                history.pushState(stateObj, "page 2", state);
            }catch(e){}
            var data = {url: item}
            $.post(url, data, function(data){                
                $('#slider').fadeOut(500, function(){
                    $('#slider').html(data);
                    setTimeout("$('#slider').fadeIn(500);", 300);
                })
                /*$('#slider').animate({top: "-=1000px"},800,function(){
                    $('#slider').html(data);
                    $('#slider').animate({top: '+=2000px'}, 1,function(){
                        $('#slider').show();
                        $('#slider').animate({top: '-=1000px'},800, function(){});
                    });
                });*/
                $('#footer-logo').attr('href', $('#unit-link').text());
                $('#next').bind('click', flashNav);
                $('#back').bind('click', flashNav);
            });
        } else if(help.attr('types')==='em_website_content' && help.attr('width')){    
            url = $('#content_link').attr('href');
            var bigger = $('.overlay').clone();
            bigger.css({position: 'absolute', zIndex: 20, marginLeft: '-150px', left: '50%', top: '50%', marginTop: '-'+(150+_margintop)+'px', display: 'block'});
            bigger.addClass('big');
            bigger.children('div').css({display: 'block'});
            bigger.appendTo($('body'));
            bigger.animate({marginLeft: '-'+help.attr('width')/2+'px', marginTop: '-'+(help.attr('height')/2+_margintop)+'px'}, 500);
            bigger.children('div').animate({width: help.attr('width'), height: help.attr('height')}, 500, function(){
                var data = {url: item}
                $.getJSON(url,data, function(data){   
                    bigger.children('div').first().html(data.html);
                    /*if (data.configuration!=null){
                        var configuration = {};
                        var flashvars = {};
                        configuration.src = data.player;
                        configuration.width = data.configuration.width;
                        configuration.height = data.configuration.height;
                        configuration.version = [10,0];
                        configuration.onFail = function(params, version, flashvars) {
                            new Gallery(data.flashId,data.configuration.width,data.configuration.height,data.variable.overbgcolor,data.variable.overcolor,data.variable.overfgcolor,data.variable.skin,data.variable.outalpha,data.variable.fixedthumbs,data.variable.access,data.variable.ids,data.variable.galleryname);
                            return true;
                        };                
                        configuration.quality = "high";
                        configuration.scale = "noScale";
                        configuration.salign = "TL";
                        configuration.wmode = "window";
                        configuration.bgcolor = data.parameters.bgcolor;
                        configuration.allowfullscreen = data.parameters.allowfullscreen;
                        flashvars.domain = "rup2011.neuim.net";
                        flashvars.site_access = data.variable.access;
                        flashvars.ids = data.variable.ids;
                        flashvars.upbgcolor = data.variable.upbgcolor;
                        flashvars.upfgcolor = data.variable.upfgcolor;
                        flashvars.overbgcolor = data.variable.overbgcolor;
                        flashvars.overfgcolor = data.variable.overfgcolor;
                        flashvars.overcolor = data.variable.overcolor;
                        flashvars.outalpha = data.variable.outalpha;
                        flashvars.fixedthumbs = data.variable.fixedthumbs;
                        flashvars.galleryname = data.variable.galleryname;
                        flashvars.trackevents = "false";
                        flashembed(data.flashId, configuration, flashvars);
                    }*/
                });
                if(!help.hasClass('contact')){
                    $(document).bind('touchstart', closeOverlay);
                    $('body').bind('click', closeOverlay);                    

                    bigger.bind('click', function(event){
                        event.stopPropagation();
                    });
                    $('iframe').bind('touchstart', function(event){
                        event.stopPropagation();
                    });
                    bigger.bind('touchstart', function(event){
                        event.stopPropagation();
                    });
                }
                $('span#close').bind('click', closeOverlay);
                $('#close').bind('touchstart', closeOverlay);                
                $(document).unbind('keydown', flashNav);
                $('#submit').live('click',function(){
                    var data = $('form').first().serializeArray();                    
                    $.post(url, data, function(data){
                       bigger.children('div').first().html(data.html);
                    }, 'json');
                });
                
            });        
        } else if(help.attr('types')==='em_website_navigation') {
            window.location = help.attr('href');
        }
    }
}

function videoStopped(){
    closeOverlay();
}

function closeOverlay(event){
    $(document).unbind('touchstart', closeOverlay);
    $('#close').unbind('touchstart', closeOverlay);
    $('body').unbind('click', closeOverlay);
    $('span').unbind('click', closeOverlay);
    $('#submit').die('click');
    $(document).bind('keydown', flashNav);
    var bigger = $('.big').last();
    bigger.animate({marginLeft: '-150px', marginTop: '-'+(150+_margintop)+'px'}, 500);
    bigger.children('div').animate({width: '300', height: '300'}, 500, function(){bigger.remove();});
}

function _noFlash(item, active){
    var url = $('#html_link').attr('href');
    $('#box-holder').html('');
    $('a#back').css('margin-left', '-650px');
    $('a#next').css('margin-left', '618px');
    var data = {url: item, recent: active};
    $.post(url, data, function(data){
        $('#box-holder').html(data);
    });

}



$('a.back').live('click', function(e){
    e.preventDefault();
    _back();
});


function _back(){
    var help = $('a.back').first();
    if(help.attr('href') && debug==0){
        try{
            var stateObj = {node: help.attr('name')};
            var state = help.attr('href'); 
            history.pushState(stateObj, "page 2", state);
        } catch(e){}
        var data = { 
            url: help.attr('name'),
            recent: help.attr('recent')};
        $.post(help.attr('access'), data, function(data){
                $('#slider').fadeOut(500, function(){
                    $('#slider').html(data);
                    setTimeout("$('#slider').fadeIn(500);", 300);
                })
        });
    } else if(help.attr('href')){
        $('#slider').fadeOut(500)
        //post_to_url(help.attr('href'), {recent: help.attr('recent')});
        window.location = help.attr('href')+'?recent='+help.attr('recent');
    }
}

function _centered(item){
    if(item.type==='_centered'){
        item=$(this).attr('name');
    }
    $('.linear').css('cursor', 'pointer');
    var help = $('textarea[name="'+item+'"]:first');
    if(help.attr('types') === 'em_website_content' && !help.attr('width')){
            $(this).children('.linear').css('cursor', 'default');
    }
    $('#text').show();
    $('#text').html(help.text());
    /*
    $('.box').css('visibility', 'visible');
    var length = $('.box').filter(function() {
        return ($(this).css('visibility') == 'visible');
    }).length;

    if(length>7){
        while ($('.box').filter(function() {
                return ($(this).css('visibility') == 'visible');
            }).length-7>0){  
            $('.box').filter(function() {
                    return ($(this).css('visibility') == 'visible');
                }).last().css('visibility','hidden');
            if($('.box').filter(function() {
                 return ($(this).css('visibility') == 'visible');
                }).length-7>0){
                $('.box').filter(function() {
                        return ($(this).css('visibility') == 'visible');
                    }).first().css('visibility','hidden');
            }
        }
    }*/
    //reloadFont();
}

function _blur(){
    $('#text').hide();
}


function flashNav(event){
    var flashMovie = $('object').first();
    var flash = document.getElementById(flashMovie.attr('id'));
    if($(event.currentTarget).attr('id')==='next' || event.keyCode===39)
        flash.changeScroll(1);
    if($(event.currentTarget).attr('id')==='back' || event.keyCode===37)
        flash.changeScroll(-1);
    if(event.keyCode===13 || event.keyCode==40)
        flash.selectActive();
}

function checkHeight(event){
    if ($(window).height()<630){
        _margintop = 0;
        $('#slider').css('margin-top', '48px');
        $('#footer').hide();
    } else {
        $('#slider').css('margin-top', '0px');
        $('#footer').show();
        _margintop = 50;
    }
}

function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default, if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
}


