var application = {
    switchb: function(){
        var list = $('.switch-center a');
        var mainImg = $('.content-main');
        list.each(function(i){
            $(this).click(function(){
                if ($(this).hasClass('active'))
                    return false;
                list.removeClass('active');
                $(this).addClass('active');
                mainImg.toggleClass('no-bg');
                
                return false;
            });
        });
        list.filter(':eq(1)').addClass('active');
        $(".show_menu").click(function(){
            $(".form-z").show();
            return false;
        });
        $(".show_form").click(function(){
            $(".form-k").show();
            return false;
        });
        $(".closex").click(function(){
            $(".form-z").hide();
            return false;
        });
        $(".closef").click(function(){
            $(".form-k").hide();
            return false;
        });
        $('.menu-main div span').click(function(){
            var parent = $(this).parent();
            (parent.hasClass('show')) ? parent.nextAll('.hide').show() : parent.prevAll('.show').show() ;
            parent.hide();
        });
    },
    gradientText :function(){
        
        $('.menu-link').filter(':even').gradientText({
            colors: ['#FF8400', '#FFFFFF']
        });
        $('.menu-link').filter(':odd').gradientText({
            colors: ['#FFFFFF', '#FF8400']
        });
    },
    corner: function()
    {
        $('.corner-bevel').corner("bevel 15px");
        
        $('.hidden-gallery').css({visibility: 'hidden', display: 'block'});
        $('.hg-bg .hg-bg-bevel').corner("bevel 10px").css('position', 'fixed');
        $('.hidden-gallery').css({visibility: 'visible', display: 'none'});
        
        $('.form-z').css({visibility: 'hidden', display: 'block'}).corner("10px").css({visibility: 'visible', display: 'none'});
        $('.menu-main .hide').css({visibility: 'visible', display: 'none'});
        /*$('.corner-top').corner("bevel top");
        $('.corner-bottom').corner("bevel bottom");*/
        //$('.switch-center .active').corner("10px");
    },
    gallery: {
        n: {
            gallery: null,
            slider: null,
            list: null,
            mPhoto: null,
            activeList: null,
            galleryActive: 0,
            galleryAnimateBlock: false,
            title: null,
            wppGallery: null,
            nextPhoto: null,
            prevPhoto: null
        },
        init: function()
        {
            var self = this;
            self.n.gallery      = $('.hidden-gallery');
            self.n.slider       = self.n.gallery.find('.hg-slider');
            self.n.list         = self.n.slider.children();
            self.n.mPhoto       = self.n.gallery.find('.hg-image-main');
            self.n.mPhotoLeft   = self.n.gallery.find('.hg-image-left');
            self.n.mPhotoRight  = self.n.gallery.find('.hg-image-right');
            self.n.title        = self.n.gallery.find('h2');
            self.n.slideLeft    = $('.hg-list .slide-left');
            self.n.slideRight   = $('.hg-list .slide-right');
            self.n.sliderWidth  = 0;
            self.n.incLeft      = 0;
            
            self.n.mPhoto.load(function(){
                $(this).css('visibility', 'visible').fadeTo(100, 1);
            });
            self.n.mPhotoLeft.load(function(){
                $(this).css('visibility', 'visible').fadeTo(100, 0.5);
            });
            self.n.mPhotoRight.load(function(){
                $(this).css('visibility', 'visible').fadeTo(100, 0.5);
            });
            
            $('.g-tag').click(function(){
                self.n.list.removeClass('enable');
                self.n.activeList = self.n.list.filter('.'+$(this).attr('rel'));
                self.n.activeList.addClass('enable');
                
                var imageXPath = ($(this).children('img').attr('id') && '.i-'+$(this).children('img').attr('id')) || ':first';
                
                self.n.gallery.show();
                
                self.showImage(self, self.n.activeList.filter(imageXPath), true);
                //self.n.wppGallery.reConf();
                
                self.enable_disableGallery(self);
                return false;
            });
            
            $('.hg-close').click(function(){
                self.n.list.removeClass('enable');
                self.showImage(self, null);
                self.n.gallery.hide();
                return false;
            });
            
            self.makePreview(self);
            self.initGallery(self);
        },
        makePreview: function(self)
        {
            self.n.list.click(function(){
                self.showImage(self, $(this));
                return false;
            });
            
            self.n.mPhotoLeft.parent().click(function(){
                self.showImage(self, self.n.prevPhoto);
                return false;
            });
            
            self.n.mPhotoRight.parent().click(function(){
                self.showImage(self, self.n.nextPhoto);
                return false;
            });
        },
        showImage: function(self, link, start)
        {
            if (link == null || !link.length)
            {
                self.n.mPhoto.hide();
                return;
            }
            if (link.hasClass('active'))
                return;
            
            self.n.list.removeClass('active');
            link.addClass('active');
            
            if (start)
                self.makeStartShift(self, link);
            
            self.moveSlider(self, link);
            
            self.loadPhoto(self, self.n.mPhoto,      null, link);
            self.loadPhoto(self, self.n.mPhotoLeft,  self.n.mPhotoLeft.next(),  link.prev('.enable'), 'left');
            self.loadPhoto(self, self.n.mPhotoRight, self.n.mPhotoRight.next(), link.next('.enable'), 'right');
        },
        loadPhoto: function(self, target, arrow, photoLink, offset)
        {
            if (!photoLink.length){
                target.hide();
                if (arrow)
                    arrow.hide();
                return;
            }
            
            if (arrow)
                arrow.fadeTo(100, 0.5);
            
            target.fadeTo(100, 0.01, function(){
                var size = photoLink.attr('rel').split('-');
                if (typeof offset != 'undefined')
                {
                    size[0] = size[0] * 0.8;
                    size[1] = size[1] * 0.8;
                }
                
                if (offset == 'left')  {
                    target.css('left',  (-0.6*size[0]));
                    self.n.prevPhoto = photoLink;
                }
                if (offset == 'right') {
                    target.css('right', (-0.6*size[0]));
                    self.n.nextPhoto = photoLink;
                }
                target.attr('src', null);
                
                target
                .attr({
                    src: photoLink.attr('href'),
                    width: size[0],
                    height: size[1]
                })
                .css('visibility', 'hidden')
                .show();
                
                if (target.get(0).complete)
                    target.css('visibility', 'visible').fadeTo(100, (typeof offset == 'undefined' ? 1 : 0.5 ));
            });
        },
        enable_disableGallery: function(self)
        {
            self.n.sliderWidth = calcWidth(self.n.activeList);
            if (self.n.sliderWidth > 700) {
                self.n.slideRight.show();
                return;
            } 
            self.n.slideLeft.hide();
            self.n.slideRight.hide();
        },
        makeStartShift: function(self, active)
        {
            var sum = active.outerWidth(true);
            active.prevAll('.enable').each(function(){
                if ((sum += $(this).outerWidth(true)) > 700){
                    self.n.incLeft -= $(this).outerWidth(true);
                    self.n.galleryActive++;
                }
            });
            if (self.n.incLeft < 0)
                self.n.galleryActive--;
        },
        moveSlider: function(self, active)
        {
            var currOffset = calcWidth(active.prevAll('.enable'), active) + (parseInt(self.n.slider.css('left')) || 0);
            
            if (currOffset <= 0)
                self.n.slideLeft.click();
            else if(currOffset > 700)
                self.n.slideRight.click();
        },
        initGallery: function(self)
        {
            var slideLeft  = self.n.slideLeft;
            var slideRight = self.n.slideRight;
            //self.n.galleryActive = self.n.activeList;
            slideLeft.click(function(){
                if (self.n.galleryAnimateBlock === true)
                    return false;
                
                slideRight.show();
                
                self.n.galleryAnimateBlock = true;
                var left = (parseInt(self.n.slider.css('left')) || 0) + self.n.activeList.filter(':eq('+(self.n.galleryActive-1)+')').outerWidth(true);
                
                self.n.galleryActive--;
                
                self.n.slider.animate({left: left}, 250, function(){
                    self.n.galleryAnimateBlock = false;
                    if (self.n.galleryActive == 0)
                        slideLeft.hide();
                });
                
                return false;
            });
            
            slideRight.click(function(){
                if (self.n.galleryAnimateBlock === true)
                    return false;
                
                slideLeft.show();
                
                self.n.galleryAnimateBlock = true;
                
                if (self.n.incLeft != 0){
                    left = self.n.incLeft;
                    self.n.incLeft = 0;
                }
                else
                    left = (parseInt(self.n.slider.css('left')) || 0) - self.n.activeList.filter(':eq('+self.n.galleryActive+')').outerWidth(true);
                
                self.n.galleryActive++;
                
                self.n.slider.animate({left: left}, 250, function(){
                    self.n.galleryAnimateBlock = false;
                    if (calcWidth(self.n.activeList.filter(':gt('+(self.n.galleryActive-1)+')')) < 700)
                        slideRight.hide();
                });
                
                return false;
            });
        }
    }
}
$(document).ready(function(){
    (function() {
        this.switchb();
        this.corner();
        this.gallery.init();
        this.gradientText();
        //this.preloadImages();
    }).call(application);
});

function calcWidth(list, element)
{
    var sum = 0;
    list.each(function(){
        sum += $(this).outerWidth(true);
    });
    
    if (typeof element != 'undefined')
        sum += element.outerWidth(true);
    
    return sum;
}

$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}
