$(window).load(function() {
    $('#content, #content>li, .miniitemholderHead>a, .miniitemholderBottom>a, .miniitemholderVert>a, .miniitemholderX>a').hide();
    $('#content').css({ height: '420px' });

    var isStoped1 = false,
        isStoped2 = false,
        isStoped3 = false,
        isStoped4 = false,
        isStoped5 = false,
        speed = 80,
        startEventName = "mouseenter",
        endEventName = "mouseleave";

    if (isTouch == true) {
        startEventName = "touchstart";
        endEventName = "touchend";
    }

    //var imgCSS = $.browser.msie ? { top: 0, left: 0} : { top: 0, width: 0, height: 0, left: '87px' }

    var hElem = $('nav>ul>li>img,nav>ul>li>ul'),
		mode = 'splash',
		changeBehaviorMode = function() {
		    mode = mode == 'splash' ? 'content' : 'splash'
		    if (mode == 'content')
		        $('nav').stop().animate({ marginTop: '-135px' }, 'fast', function() {
		            $('#content').fadeIn();
		            $('nav').removeClass('splash');

		            $('#main').css({ minHeight: "740px" });

		            var li = $('nav>ul>li')
		            li.parent().find('li:not(.active)').stop().animate({ top: '42px' }, 'easeOutQuart')
		            li.die().parent()
					.delegate('li:not(.active)', startEventName, function() {
					    $(this).stop().animate({ top: 0 }, 'fast', 'easeInQuart')
					})
					.delegate('li:not(.active)', endEventName, function() {
					    $(this).animate({ top: '42px' }, 'fast', 'easeOutQuart')
					})
					.delegate('li:not(.active)', 'click', function() {
					    $($(this).find('>a').attr('href')).fadeIn().siblings().hide()
					    $(this).stop().animate({ top: '-70px' }, 'fast', 'easeInQuad', function() {
					        $(this).animate({ top: 0 })
							    .addClass('active').siblings().removeClass('active')
					    }).siblings('.active').stop().animate({ top: '-70px' }, 'fast', 'easeInQuad', function() {
					        $(this).animate({ top: '42px' }, 'easeInQuad')
					    })
					    if (isHTML5 == true) { pauseHtml5Video(); }
					    return false;
					})
		            $('footer .privacy a').die().live('click', function() {
		                $(this.href.slice(this.href.indexOf('#'))).fadeIn().siblings().hide()
		                $('nav li.active').stop().animate({ top: '-70px' }, 'fast', 'easeInQuad', function() {
		                    $(this).animate({ top: '42px' }, 'easeInQuad').css({ zIndex: 9 })
		                }).removeClass('active')
		                return false
		            })
		        })
		}

    function pauseHtml5Video() {
        var len = document.getElementsByTagName("video").length;
        for (i = 0; i < len; i++) {
            var vid = document.getElementsByTagName("video").item(i);
            if (vid.paused == false)
            { vid.pause(); }
        }
    }

    function preload(id) {
        //Adding loading image
        $(id).empty().append('<div class="loadingBox" id="pageLoading"><img src="img/circle-light-loader.gif" alt="Loading..." /><p>Loading...</p></div>');
        $(id).show();

        //Ajax preloader
        $.ajax({
            url: id.toString().substring(1) + ".php",
            type: 'POST',
            //data: 'id=' + id.substring(1),
            data: isTouch == true ? 'flash=0' : 'flash=1',
            success: function(result) {
                //$(id).empty().append(result);
                $(id).append(result);
                Cufon.refresh();
                if (id == '#contacts') {
                    clearInput('form1');
                }
                else if (id == '#about' || id == '#multimedia') {
                    $(id + ' .tiny').tiny({ easing: 'easeInOutQuart', duration: 'slow' });
                }
                if (id == '#multimedia') {
                    $(id + ' .tiny').show(function() {
                        $(id + ' .tiny').tiny({ easing: 'easeInOutQuart', duration: 'slow' });
                        $('#multimedia .cat-tabs li').eq(0).siblings().removeClass('curr');
                        $('#multimedia .cat-cont>li').eq(0).siblings().hide();
                        $('#multimedia .tiny li a[href$=.jpg],a[href$=.png]').fancybox({ padding: 0, overlayColor: '#141311', overlayOpacity: .8, transitionIn: 'elastic', transitionOut: 'none' });
                        initSwipe();
                        $(id).fadeIn();
                    });
                }
                else { $(id).fadeIn(); }
                $('#pageLoading').fadeOut(500, function() {
                    $('#pageLoading').remove();
                    //Cufon.refresh();
                });
            }
        });
        return false;
    }

    $('nav>ul>li').die().parent()
		.delegate('li:not(.active)', 'click', function() {
		    changeBehaviorMode()
		    $(this).addClass('active')
		    //if ($(this).find('scroller_container')) { $jScroller.start() } else { $jScroller.stop() }
		    var page = $(this).find('>a').attr('href');
		    if ($(page).hasClass('empty')) {
		        preload(page);
		        $(page).removeClass('empty');
		    }
		    //*** $($(this).find('>a').attr('href')).fadeIn(); ***//
		    return false;
		})

    $('footer .privacy a').live('click', function() {
        changeBehaviorMode()
        //$(this.href.slice(this.href.indexOf('#'))).fadeIn()
        var page = this.href.slice(this.href.indexOf('#'));
        if ($(page).hasClass('empty')) {
            preload(page);
            $(page).removeClass('empty');
        }
        return false;
    })
    $('.soc_ico li').live('mouseover', function() {
        $(this).stop().animate({ left: 5 }, 200, function() { $(this).animate({ left: 0 }, 200) })
    })

    $('#multimedia .cat-tabs a').live('click', function() {
        var href = this.href.slice(this.href.indexOf('#')).replace('#', '.');
        pauseHtml5Video();
        $(this).parent().addClass('curr').siblings().removeClass('curr');
        $('#multimedia .cat-cont ' + href).fadeIn().siblings().hide();
        return false;
    })

    $("#content .p2_navi li").live('click', function() {
        var _curId = $(this).attr("id");
        var _curNum = _curId.substring(3);
        var _curInfoBox = $(this).closest(".page").find(".info_box");
        _curInfoBox.fadeOut("normal",
        function() {
            $("#" + _curId).addClass("active").siblings().removeClass("active");
            _curInfoBox.find(">div").hide();
            $("#ac" + _curNum).show()
            _curInfoBox.fadeIn();
        })
    });

    /*** Mini-Squares animation ***/
    $('.about')
		.live(startEventName, function() {
		    var items = $('.about .miniitemholderVert a');
		    //if (isTouch == true) { $('>a', this).animate({ paddingTop: '5px', height: '125px' }, speed * 5); }
		    function showMiniItem(_id, _num) {
		        if (_num < 6 && !isStoped1) {
		            var _curItem = _id + _num;
		            $(_curItem).fadeIn(speed, function() { showMiniItem(_id, ++_num) });
		        }
		    }
		    isStoped1 = false;
		    showMiniItem('#miv', 1);
		})
		.live(endEventName, function() {
		    isStoped1 = true;
		    $('.about .miniitemholderVert a').fadeOut(speed);
		    //if (isTouch == true) { $('>a', this).animate({ paddingTop: '40px', height: '90px' }, speed * 5); }
		})

    $('.apps')
		.live(startEventName, function() {
		    var items = $('.apps .miniitemholderHead a');
		    function showMiniItem(_id, _num) {
		        if (_num < 6 && !isStoped2) {
		            var _curItem = _id + _num;
		            $(_curItem).fadeIn(speed, function() { showMiniItem(_id, ++_num) });
		        }
		    }
		    isStoped2 = false;
		    showMiniItem('#mih', 1);
		})
		.live(endEventName, function() {
		    isStoped2 = true;
		    $('.apps .miniitemholderHead a').fadeOut(speed);
		})

    $('.el')
		.live(startEventName, function() {
		    $('.el .miniitemholderX a').fadeIn();
		    $('.mixSplitter').stop().animate({ width: '130px' }, speed * 4, 'easeInQuad');
		})
		.live(endEventName, function() {
		    $('.el .miniitemholderX a').fadeOut(speed, function() { $('.mixSplitter').css({ width: 0 }) });
		})


    $('.os')
		.live(startEventName, function() {
		    var items = $('.os div a');
		    function showMiniItem(_id, _num) {
		        if (_num < 11 && !isStoped3) {
		            var _curItem = _id + _num;
		            $(_curItem).fadeIn(speed, function() { showMiniItem(_id, ++_num) });
		        }
		    }
		    isStoped3 = false;
		    showMiniItem('#mih', 6);
		})
		.live(endEventName, function() {
		    isStoped3 = true;
		    $('.os div a').fadeOut(speed);
		})

    $('.cont')
		.live(startEventName, function() {
		    var items = $('.cont .miniitemholder a');
		    function showMiniItem(_id, _num) {
		        if (_num < 11 && !isStoped5) {
		            var _curItem = _id + _num;
		            $(_curItem).fadeIn(speed, function() { showMiniItem(_id, ++_num) });
		        }
		    }
		    isStoped5 = false;
		    showMiniItem('#miv', 6);
		})
		.live(endEventName, function() {
		    isStoped5 = true;
		    $('.cont .miniitemholderVert a').fadeOut(speed);
		})

});
