/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var doscrollDown = false;
var doscrollUp = false;
var target;
var mobile = false;
var topvalue = 0;
var myScroll;
var lastFinger = false;
var vPos = 0;
finger = 0;
var lastValue = 0;
$(document).ready(function(){
    if ($.browser.opera) {
        target = "html";
    } else {
        target = "html,body";
    }
    mobile = (navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod');

    $(window).resize(function(){
        resize();
    });
    resize();
    positionNavigation();

    $(".navigation-sub > li > a").click(function(){
        $(".navigation-sub ul").hide();
        $(this).parent().children('ul').show();
        positionRightNavigation();
        if($(this).parent().children('ul').find('li').length>0){
            return false;

        }
    });

    $('input').each(function(){
        if($(this).val().length == 0){
            $(this).val($(this).attr('default'));
        }
    });

    $('input').focusin(function(){
        if($(this).val() == $(this).attr('default')){
            $(this).val('');
        }
    });

    $('form').submit(function(){
        $('input').each(function(){
            if($(this).val() == $(this).attr('default')){
                $(this).val('');
            }
        });
    });

    $('input').focusout(function(){
        if($(this).val() == ''){
    //            $(this).val($(this).attr('default'));
    }
    });

    if($("#content-text").height()>430){
        $("#updownnavi").show();
    }
    if(mobile){
        $("#down").bind( "touchstart", function(e){
            doscrollDown = true;
            topvalue = finger;
            //            topvalue = $("#content-text").position().top;
            scrollDown();
        });
        $("#down").bind('touchend',function(e){
            doscrollDown = false;
            finger = topvalue;
            vPos+=finger;
        });
        $("#up").bind( "touchstart", function(e){
            doscrollUp = true;
            topvalue = finger;
            scrollUp();
            e.preventDefault();
        });
        $("#up").bind('touchend',function(e){
            doscrollUp= false;
            finger = topvalue;
            vPos+=finger;
        });
    } else {

        $("#down").hover(
            function (){
                doscrollDown = true;
                setTimeout('scrollDown()',50);
            },
            function (){
                doscrollDown = false;
            }
            );

        $("#up").hover(
            function (){
                doscrollUp = true;
                setTimeout('scrollUp()',50);
            },
            function (){
                doscrollUp = false;
            }
            );
    }
    if(mobile && $("#content-text").height()>400)
    {
//        console.log('content-text: '+$("#content-text").height());
//        console.log('content-text: '+parseInt($("#content-text").css('padding-bottom')));
//        console.log('window: '+$("window").height());
//        console.log('container: '+$("#container").height());
        node = document.getElementById('content-text');
        node.ontouchend = function(e){
        }
        node.ontouchstart = function(e){
            var touch = e.touches[0];
            lastFinger =  touch.pageY - finger;
        }
        node.ontouchmove = function(e){
            if(e.touches.length == 1){ // Only deal with one finger
                var touch = e.touches[0]; // Get the information for finger #1
                if(!lastFinger){
                    lastFinger = touch.pageY;
                }
                height = $("#content-text").height()-400;
                finger =  (touch.pageY-lastFinger);
                vPos+=finger;
                //                }
                finger = (finger<0)?finger:0;
//                console.log("a"+finger);
                finger = ((-finger)<(height))?finger:(-height+10);
                //                console.log(finger + "#"+(height));
                lastValue = finger;
                $("#content-text").css('-webkit-transform','translateY('+finger+'px)');
                return false;
            }
        }
    //                myScroll = new iScroll('container'
    //                ,
    //                {
    //                    zoom:true
    //                }
    //            );
    }

});


function resize(){
    $windowheight = $(window).height();
    $windowwidth = $(window).width();
    $bottom = $windowheight - 675;
    if($bottom<0){
        $bottom = 0;
    }
    $("#content-text").css('padding-bottom',$bottom+50);
    if($windowheight<800){
        top = (1200 - $windowheight)/2;
        top = (top<100)? top:100;
        $('#container').css('margin-top',-top);
        $("#bg-top").css('margin-top',-top);
        $("#bg-left").css('margin-top',-top);
        $("#bg-right").css('margin-top',-top);
        $("#bg-bottom").css('margin-top',-top);
        $("#bg-left").height($(document).height()+top);
        $("#bg-right").height($(document).height()+top);
        $("#bg-bottom").height($("#bg-left").height()-685);

    } else {
        $('#container').css('margin-top','0');
        $("#bg-top").css('margin-top','0');
        $("#bg-left").css('margin-top','0');
        $("#bg-right").css('margin-top','0');
        $("#bg-bottom").css('margin-top','0');
        $("#bg-left").height($(document).height());
        $("#bg-right").height($(document).height());
        //        $("#bg-bottom").height(($(window).height()+top)-$('#bg-bottom').css('height'));
        $("#bg-bottom").height($("#bg-left").height()-685);
    }
    if($windowwidth < 1600){
        left = (1600 - $windowwidth)/2;
        left = (left>320)? 320 : left;
        //        console.log(left);
        $('#container').css('margin-left',-left);
        $("#bg-top").css('margin-left',-left);
        $("#bg-left").css('margin-left',-left);
        $("#bg-right").css('margin-left',-left);
        $("#bg-bottom").css('margin-left',-left);
    //        $("#bg-top").width($("#bg-left").width() + left);
    //        $("#bg-bottom").width($("#bg-right").width() + left);
    } else {
        $('#container').css('margin-left','0');
        $("#bg-top").css('margin-left','0');
        $("#bg-left").css('margin-left','0');
        $("#bg-right").css('margin-left','0');
        $("#bg-bottom").css('margin-left','0');
    }

}

function positionNavigation(){
    positionLeftNavigation();
    positionRightNavigation();
}

function positionLeftNavigation(){
    $elements = $('#navigation-left').height();
    $container = 1000;

    $margin = (($container - $elements)/2)-5;
    $('#navigation-left').css('margin-top',$margin);

}

function positionRightNavigation(){
    $elements = $('#navigation-right').height();
    $container = 1000;

    $margin = (($container - $elements)/2)-5;
    $('#navigation-right').css('margin-top',$margin);
}


function scrollDown(){
    if(doscrollDown){
            
        if(mobile){
            height = $("#content-text").height()-400;
            topvalue = (topvalue-400>-height)? topvalue-400:-height;
            $("#content-text").css('-webkit-transform','translateY('+topvalue+'px)');
        } else {
            topvalue = (topvalue+10<$("#content-text").height()-400)? (topvalue+10) : $("#content-text").height();
            $(target).scrollTop(topvalue);
        }
        if(mobile){
        //            setTimeout('scrollDown()',1);
        } else {
            setTimeout('scrollDown()',10);
        }
    }
}
function scrollUp(){
    if(doscrollUp){
        if(mobile){
            
            topvalue = (topvalue+400<0)? topvalue+400:0;
            $("#content-text").css('-webkit-transform','translateY('+(topvalue)+'px)');
        } else {
            topvalue = (topvalue-10>0)? (topvalue-10) : 0;
            $(target).scrollTop(topvalue);
            setTimeout('scrollUp()',10);
        }
    }
}
