/**
 * Created by JetBrains PhpStorm.
 * User: Flame
 * Date: 23.05.11
 * Time: 22:06
 * To change this template use File | Settings | File Templates.
 */
var opacit = 0.8; // Начальная прозрачность
var url = 'http://francesco-marconi.com/'; // Полный url
var interval = 0;

$(document).ready(function() {
    
    $(document).mousemove(function(e) {
        inter.scroll(e);
    })

    routes.init()
    inter.init(opacit);
    inter.autoslide(1, 30000); // Запуск автослайда

    // Обработка кликов по стрелкам в галерее
    $('#arl').live('click', function() {
        inter.slideFiltered('back', '.gallery')
    });
    $('#arr').live('click', function() {
        inter.slideFiltered('forward', '.gallery')
    });

    // Обработка кликов по переключателю страниц
    $('.pager').find('li').live('click', function() {
        if ($(this).hasClass('current')) return;
        $('.pager').find('.current').removeClass('current');
        $(this).addClass('current');
        var pageId = $(this).text() * 1;
        var parent = $(this).closest('dd')
        parent.find('.page').hide();
        parent.find('.page' + pageId).fadeIn();
    })

    // Обработка кликов по разделам на странице our world
    $('.historyofbrand').find('dt').live('click', function() {
        if ($(this).hasClass('current')) return;
        $(this).closest('dl').find('dt.current').removeClass('current');
        $(this).addClass('current')
        $(this).closest('dl').find('dd').hide();
        $(this).next('dd').fadeIn();
    })
})

// Обработка резайза окна
$(window).bind('resize', function() {
    inter.fit();
})

var inter = {
    init: function(o) {
        $('#background, #bags').height($(window).height())
        $('#background img').fadeTo('fast', o)
//        $('#fader').css({backgroundPosition: (1 - o) * 100 + '% center'})
        $('.historyofbrand').find('dt.current').next('dd').show();


        $('#scrd').live('click', function(){
            $('.wr').stop();
            $('.wr').animate({
                top: '-350px'
            }, 1000, 'swing', function(){
                        $('#scrup').fadeIn()
                    })
            $(this).fadeOut()
        });
        $('#scrup').live('click', function() {
            $('.wr').stop();
            $('.wr').animate({
                top: '0px'
            }, 1000, 'swing', function(){
                        $('#scrd').fadeIn()
                    })
            $(this).fadeOut()
        });

        $('.coltwo').columnize({columns: 2})
        inter.sliderInit();

        
        inter.fit();
    },
    sliderInit : function() {
        $('#fader').slider({
            value: 100-window.opacit*100,
            change: function(event, ui) {
                window.opacit = 1-$('#fader').slider('value')*0.01;
                $('#background img').fadeTo('fast', window.opacit);
            }
        });
    },
    submitContact : function() {
        $('#msgform').submit(function(event){
            event.preventDefault();
            var f = $(this);
            var url = f.attr('action');
            var filled = 0;
            f.find('input[type="text"], textarea').each(function() {
                if ($(this).val() == '' || $(this).val() == 'Fill this field, please!') {
                    $(this).val('Fill this field, please!').click(function() {
                        $(this).val('');
                    })
                } else {
                    filled = filled + 1;
                }
            })

            if(filled < 5) {
                return
            } else {
                var name = f.find('input[name="name"]').val();
                var email = f.find('input[name="email"]').val();
                var city = f.find('input[name="city"]').val();
                var subj = f.find('input[name="subj"]').val();
                var messa = $('#msg').val();
                $.get(
                        url,
                        {
                            n: name,
                            email: email,
                            city: city,
                            subj: subj,
                            m: messa
                        },
                        function(data) {
                            $('#msgform').hide();
                            $('#msgsent').fadeIn('slow').html();
                            setTimeout(function() {
                                $('#msgsent').hide();
                                $('#msgform').find('input, textarea').val('')
                                $('#msgform').fadeIn('slow');
                            }, 4000)
//                    alert(data)
                        })
            }
        })
    },
    open: function(id) {
        id = id * 1
        switch (id) {
            case 0:
                $('#m').find('.active').removeClass('active');
                $('.p').hide();
                inter.fit()
                break
            case 1:
                $('#m').find('.active').removeClass('active');
                $('#m' + id).addClass('active');
                var page = $('.p' + id);
                $('.p').hide();
                $('.p' + id).fadeIn('fast')
                inter.fit();
                break
            case 3:
                $('#m').find('.active').removeClass('active');
                $('#m' + id).addClass('active');
                var page = $('.p' + id);
                $('.p').hide();
                $('.p' + id).fadeIn('fast')
                inter.fit();
                break
            case 2:
                opacit = 1
                inter.init(opacit);
                $('#m').find('.active').removeClass('active');
                $('#m20').addClass('active')
                inter.fit()
                break
        }
    },

    slideFiltered: function(dir, id) {
        switch (dir) {
            case 'forward':
                if (!$(id).find('img:visible').is('img:last-child')) {
                    $(id).find('img:visible').fadeOut('fast').next('img').fadeIn('slow');
                } else {
                    $(id).find('img:visible').fadeOut('fast');
                    $(id).find('img').first().fadeIn('slow');
                }
                break
            case 'back':
                if (!$(id).find('img:visible').is('img:first-child')) {
                    $(id).find('img:visible').fadeOut('fast').prev('img').fadeIn('slow');
                } else {
                    $(id).find('img:visible').fadeOut('fast');
                    $(id).find('img:last-child').fadeIn('slow');
                }
                break
        }
    },

    slide: function(dir, id) {
        switch (dir) {
            case 'forward':
                if (!$(id).find('img:visible').is('img:last-child')) {
                    $(id).find('img:visible').fadeOut('fast').next('img').fadeIn('slow');
                } else {
                    $(id).find('img:visible').fadeOut('fast');
                    $(id).find('img:first-child').fadeIn('slow');
                }
                break
            case 'back':
                if (!$(id).find('img:visible').is('img:first-child')) {
                    $(id).find('img:visible').fadeOut('fast').prev('img').fadeIn('slow');
                } else {
                    $(id).find('img:visible').fadeOut('fast');
                    $(id).find('img:last-child').fadeIn('slow');
                }
                break
        }
    },

    autoslide: function(o, t) {
        switch (o) {
            case 1:
                if (!t) t = 6000;
                interval = setInterval(function() {
                    inter.slide('forward', '#background')
                }, t)
                break
            case 0:
                clearInterval(interval);
                break
        }
    },

    fit: function() {
        var ww = $(window).width()
        $('body').height($(window).height())
        var wh = $('body').height()
        $('#background, #bags, #ads').width(ww)
        $('#background, #bags').css({minWidth: wh+100 })
        $('#ads').css({minWidth: wh + 100 + 'px'})
        $('#background, #bags, #ads').height(wh)
//        $('#background').height($('body').height())
        $('#background').find('img').width($('#background').width())
//        $('#background').find('img').height($('#background').height()+400)
        $('#ads').find('img').width($('#ads').width())
        $('#bags').find('img').height($('#bags').height()+200)
        $('#collection').height(wh).width(ww)
        $('.content').height($(window).height()-150);
//        $('#bags').find('img').width($(window).width()-50)

        if($(window).height() < 750) {
//            $('#scrd').show(); // Появление контролов-скроллеров
            $('.content').css({marginTop: 85})
            $('.header').css({marginTop: 10})
           $('.h dl').css({marginTop: '-20px'})
            $('.h dd').css({top: '42px'})
//           $('.h').css({marginTop: '-10px'})
        } else {
            $('.h').css({marginTop: '3px'})
            $('.h dl').css({marginTop: '-10px'})
            $('.h dd').css({top: '52px'})
            $('.content').css({marginTop: 177})
            $('.header').css({marginTop: 41})
//            $('.sp').hide();
        }

        if(ww < 800) {
            $('.cite').css({marginLeft: '120px'})
            $('.h').css({marginLeft: '-270px'})
        } else {
            $('.cite').css({marginLeft: '429px'})
            $('.h').css({marginLeft: '-60px'})
        }

        $(window).scroll(function(){
            $('#background').css({bottom: $('#background').scrollTop()})
        })
    },

    scroll: function(e) {
        if ($('#background').size()) {
            var h = $('#background').children('img').height(); // высота картинки
            var wh = $(window).height();
            var dif = h - wh;
            var offset = (0 - e.pageY / 1000) * dif;
            $('#background').children('img').css({top: offset})
        } else if ($('#ads').size()) {
            var h = $('#ads').find('img:visible').height(); // высота картинки
            var wh = $(window).height();
            var dif = h - wh;
            offset = Math.round((0 - e.pageY / 1000) * dif);
            $('#ads').find('img').css({top: offset})
        }
    }
}

var routes = {
    init: function() {
        $.router(/^/, function(m, id) {

            $.ajax({
                url: url+ '/main.html',
                error: function() {alert('Data loading error! Try to reload page.')},
                success: function(data) {
                    $('body').html(data)
                    $('#m').find('.active').removeClass('active');
                    $('.p').hide();
                    $('#fader').hide();
                    $('.wr').css({minHeight:0});
                    $('body').height($(window).height())
                    opacit = 1;
                    inter.init(opacit);
                    inter.fit()
                }
            })

        })
        $.router(/^ourworld/, function(m, id) {

            $.ajax({
                url: url + '/main.html',
                error: function() {
                    alert('Data loading error! Try to reload page.')
                },
                success: function(data) {
                    $('body').html(data)
                    opacit = 0.2
                    inter.init(opacit);
                    inter.open(1)
                }
            })

        })
        $.router(/^contacts/, function(m, id) {
            $.ajax({
                url: url + '/main.html',
                error: function() {
                    alert('Data loading error! Try to reload page.')
                },
                success: function(data) {
                    $('body').html(data)
                    opacit = 0.2
                    inter.init(opacit);
                    inter.open(3)
                    inter.submitContact();
                }
            })

        })
        $.router(/^collections/, function(m, id) {

            $.ajax({
                url: url + '/collection.html',
                error: function() {
                    alert('Data loading error! Try to reload page.')
                },
                success: function(data) {
                    $('body').html(data)
                    inter.open(2)
                    $('#ads, #adsw').hide();
                    $('#bagsw').show().find('a').removeClass('curcol').first().addClass('curcol');
                    $('#bags').show().children('div').hide().first().show().addClass('gallery').find('img').hide().first().show()
                }
            })

        })
        $.router(/^collections\/bags/, function(m, id) {
            $.ajax({
                url: url + '/collection.html',
                error: function() {
                    alert('Data loading error! Try to reload page.')
                },
                success: function(data) {
                    $('body').html(data)
                    inter.open(2)
                    $('#ads, #adsw').hide();
                    $('#bagsw').show().find('a').removeClass('curcol').first().addClass('curcol');
                    $('#bags').show().children('div').hide().first().show().addClass('gallery').find('img').hide().first().show()
                }
            })

        })
        $.router(/^collections\/ads/, function(m, id) {

            $.ajax({
                url: url + '/collection.html',
                error: function() {
                    alert('Data loading error! Try to reload page.')
                },
                success: function(data) {
                    $('body').html(data)
                    inter.open(2)
                    $('#bags, #bagsw').hide();
                    $('#adsw').show().find('a').removeClass('curcol').first().addClass('curcol');
                    $('#ads').show().children('div').hide().first().show().find('img').hide().first().show().addClass('gallery').find('img').hide().first().show()
                }
            })


        })
        $.router(/^collections\/ads\/(\d+)/, function(m, id) {

            $.ajax({
                url: url + '/collection.html',
                error: function() {
                    alert('Data loading error! Try to reload page.')
                },
                success: function(data) {
                    $('body').html(data)
                    inter.open(2)
                    $('#bags').hide();
                    $('#bagsw').hide();
                    $('#adsw').find('a').removeClass('curcol');
                    $('#adsw').show().find('a:contains(' + id + ')').addClass('curcol');
                    $('#ads').show().children('div').removeClass('gallery').hide();
                    $('#ads' + id).addClass('gallery').show().find('img').hide().first().show();
                }
            })


        })
        $.router(/^collections\/bags\/(\d+)/, function(m, id) {

            $.ajax({
                url: url + '/collection.html',
                error: function() {
                    alert('Data loading error! Try to reload page.')
                },
                success: function(data) {
                    $('body').html(data)
                    inter.open(2)
                    $('#ads').hide();
                    $('#adsw').hide();
                    $('#bagsw').find('a').removeClass('curcol');
                    $('#bagsw').show().find('a:contains(' + id + ')').addClass('curcol');
                    $('#bags').show().children('div').removeClass('gallery').hide();
                    $('#bags' + id).addClass('gallery').show().find('img').hide().first().show();
                }
            })
        })
    }
}
