
jQuery(function(){

    clearInputs();

    fixTrans();

    initNav();

    initGallery();
    
    $('a.rsswidget[href^="http://"]').attr("target", "_blank");

});

// clear inputs

function clearInputs(){

    jQuery('input:text, input:password, textarea').each(function(){

        var _el = jQuery(this);

        var _val = _el.val();

        _el.bind('focus', function(){

            if(this.value == _val) this.value = '';

        }).bind('blur', function(){

            if(this.value == '') this.value = _val;

        });

    });

};



// init Gallery

function initGallery(){

    jQuery('div.gallery').slideshow({

        

    });
	
	
    jQuery('div.gallery-big').fadeGallery({

        autoRotation:true,

        switchTime:5000 //ms

    });

};



//create jQuery plugin

$.fn.slideshow = function(options){return new slideshow(this, options);}



//constructor

function slideshow(obj, options){this.init(obj,options)}



//prototype

slideshow.prototype = {

    init:function(obj, options) {

        this.options = $.extend({

            slides:'div.content-gallery ul >li',

            nextBtn:'a.link-next',

            prevBtn:'a.link-previous',

            pagingHolder:'div.paging',

            pagingTag:'li',

            createPaging:true,

            autoPlay:true,

            dynamicLoad:false,

            imgAttr:'alt',

            effect:'slideX',//fade, slideX, slideY,

            startSlide:0,

            switchTime:5000,

            animSpeed:650

        },options);

        

        this.mainHolder = $(obj);

        this.slides = $(this.options.slides,this.mainHolder);       

        this.nextBtn = $(this.options.nextBtn,this.mainHolder);

        this.prevBtn = $(this.options.prevBtn,this.mainHolder);

        this.dynamicLoad = this.options.dynamicLoad;

        this.imgAttr = this.options.imgAttr;

        this.animSpeed = this.options.animSpeed;

        this.switchTime = this.options.switchTime;

        this.effect = this.options.effect;

        this.autoPlay = this.options.autoPlay;

        this.previous = -1;

        this.current = this.options.startSlide;

        this.loadingFrame = 1;

        this.busy = false;

        this.direction = 1;

        this.timer;

        this.pagingArray = new Array;

        this.loadArray = new Array;

        this.preloader = new Array;

        this.slidesParent = this.slides.eq(0).parent();

        this.slideW = this.slidesParent.width();

        this.slideH = this.slidesParent.height();

        this.initPaging();

        this.setStyles();

        this.bindEvents();

        this.showSlide();

        

    },

    

    initPaging:function(){

        this.pagingHolder = $(this.options.pagingHolder,this.mainHolder);

        

        if (this.options.createPaging) {

            this.pagingHolder.each(function(i,obj){

                var _this = $(obj);

                _this.empty();

                var list = $('<ul>');

                for (var i = 0; i < this.slides.length; i++) $('<li><a href="#">' + (i + 1) + '</a></li>').appendTo(list);

                _this.append(list);

            }.bind(this));

        }

        

        this.paging = $(this.options.pagingTag, this.pagingHolder);

        var ratio = Math.ceil(this.paging.length / this.slides.length);

        for (var i = 0; i < ratio; i++) {

            this.pagingArray.push(this.paging.slice(i*this.slides.length, (i*this.slides.length)+this.slides.length));

        }

    },

    

    setStyles:function(){

        //loader

        if (this.dynamicLoad) {

            this.loader = $('<div class="loader">');

            this.loaderDiv = $('<div>').appendTo(this.loader)

            this.loader.append(this.loaderDiv).appendTo(this.mainHolder);

        }

        

        //slides

        if (this.effect == 'fade') {

            this.slides.css({display:'none',opacity:0});

            this.slides.eq(this.current).css({display:'block',opacity:1});

        } else if (this.effect == 'slideX'){

            this.slides.css({display: 'none',left:-this.slideW});

            this.slides.eq(this.current).css({display:'block',left:0});

        } else if (this.effect == 'slideY'){

            this.slides.css({display:'none',top:-this.slideH});

            this.slides.eq(this.current).css({display:'block',top:0});

        }

    },

    

    bindEvents:function(){

        this.nextBtn.bind('click',function(){

            if (!this.busy) this.nextSlide();

            return false;

        }.bind(this));

        

        this.prevBtn.bind('click',function(){

            if (!this.busy) this.prevSlide();

            return false;

        }.bind(this));

        

        for (var i = 0; i < this.pagingArray.length; i++) {

            this.pagingArray[i].each(function(i,obj){

                $(obj).bind('click',function(){

                    if (i != this.current && !this.busy) {

                        this.busy = true;

                        this.previous = this.current;

                        this.current = i;

                        this.showSlide();

                    }

                    return false;

                }.bind(this));

            }.bind(this))

        }

        

        if (this.dynamicLoad) this.loader.bind('click',this.abortLoading.bind(this));

    },

    

    nextSlide:function(){

        this.busy = true;

        this.previous = this.current;

        if (this.current < this.slides.length-1) this.current++

        else this.current = 0;

        this.direction = 1;

        this.showSlide();

    },

    

    prevSlide:function(){

        this.busy = true;

        this.previous = this.current;

        if (this.current > 0) this.current--

        else this.current = this.slides.length-1;

        this.direction = 1;

        this.showSlide();

    },

    

    showSlide:function(){

        var obj = this;

        var _current = this.current;

        clearTimeout(this.timer);

        

        if (typeof this.loadArray[_current] != 'undefined' || !this.dynamicLoad) {

            //slide already loaded

            if (this.previous != -1) this.switchSlide()

            else {

                obj.refreshStatus();

                if (obj.autoPlay) obj.startAutoPlay();

            }

        } else {

            //slide not loaded

            this.showLoading();

            

            var slide = this.slides.eq(this.current);

            var images = $(this.dynamicLoad,slide);

            var counter = 0;

            images.each(function(){

                var preloader = new Image;

                obj.preloader.push(preloader);

                var img = $(this);

                preloader.src = img.attr(obj.imgAttr);

                preloader.onload = function(){

                    counter++;

                    checkImages();

                }

                preloader.onerror = function(){

                    //ignore errors

                    counter++;

                    checkImages();

                }

            });

            

            function checkImages(){

                if (counter == images.length) {

                    images.each(function(){

                        var img = $(this);

                        img.attr('src',img.attr(obj.imgAttr));

                    });

                    obj.loadArray[_current] = 1;

                    obj.hideLoading();

                    if (obj.previous != -1) obj.switchSlide()

                    else {

                        obj.refreshStatus();

                        if (obj.autoPlay) obj.startAutoPlay();

                    } 

                }

            }

        }

    },

    

    switchSlide:function(){

        var obj = this;

        

        if (this.effect == 'fade') {

            this.slides.eq(this.previous).stop().animate({opacity:0},this.animSpeed,callback);

            this.slides.eq(this.current).stop().css({display:'block'}).animate({opacity:1},this.animSpeed);

        } else if (this.effect == 'slideX'){

            this.slides.eq(this.current).css({display:'block',left:this.slideW*this.direction}).animate({left:0},this.animSpeed);

            this.slides.eq(this.previous).animate({left:-this.slideW*this.direction},this.animSpeed,callback);

        } else if (this.effect == 'slideY'){

            this.slides.eq(this.current).css({display:'block',top:this.slideH*this.direction}).animate({top:0},this.animSpeed);

            this.slides.eq(this.previous).animate({top:-this.slideH*this.direction},this.animSpeed,callback);

        }

        

        function callback(){

            $(this).css({display:'none'});

            if (obj.autoPlay) obj.startAutoPlay();

            obj.busy = false;

        }

        

        this.refreshStatus();

    },

    

    refreshStatus:function(){

        for (var i = 0; i < this.pagingArray.length;i++) {

            this.pagingArray[i].eq(this.previous).removeClass('active');

            this.pagingArray[i].eq(this.current).addClass('active');

        }

    },

    

    showLoading:function(){

        var obj = this;

        this.loader.show();

        clearInterval(this.loadingTimer);

        obj.loadingTimer = setInterval(animateLoading, 66);

        

        function animateLoading(){

            if (!obj.loader.is(':visible')){

                clearInterval(obj.loadingTimer);

                return;

            }

            obj.loaderDiv.css('top', obj.loadingFrame * -40);

            obj.loadingFrame = (obj.loadingFrame + 1) % 12;

        }

    },

    

    hideLoading:function(){

        this.loader.hide();

    },

    

    abortLoading:function(){

        this.busy = false;

        this.loader.hide();

        this.current = this.previous;

        for (var i = 0; i < this.preloader.length; i++) {

            this.preloader[i].onload = null;

            this.preloader[i].onerror = null;

        }

        if (this.autoPlay) this.startAutoPlay();

    },

    

    startAutoPlay:function(){

        var obj = this;

        clearTimeout(obj.timer);

        obj.timer = setTimeout(function(){

            obj.nextSlide();

        },obj.switchTime);

    }

}



// bind scope function

Function.prototype.bind = function(scope) {

    var _function = this;

    return function() {

        return _function.apply(scope, arguments);

    }

};



// slideshow plugin

jQuery.fn.fadeGallery = function(_options){

    var _options = jQuery.extend({

        slideElements:'ul.gallery-content > li',

        pagerGener: true,

        pagerHold: 'div.paging',

        pagerLinks:'div.paging li',

        btnNext:'a.link-previous',

        btnPrev:'a.link-next',

        btnPlayPause:'a.play-pause',

        btnPlay:'a.play',

        btnPause:'a.pause',

        pausedClass:'paused',

        disabledClass: 'disabled',

        playClass:'playing',

        activeClass:'active',

        currentNum:false,

        allNum:false,

        startSlide:null,

        noCircle:false,

        caption:false,

        pauseOnHover:true,

        autoRotation:false,

        autoHeight:true,

        onChange:false,

        switchTime:3000,

        duration:650,

        event:'click'

    },_options);



    return this.each(function(){

        // gallery options

        var _this = jQuery(this);

        var _slides = jQuery(_options.slideElements, _this);

        var _btnPrev = jQuery(_options.btnPrev, _this);

        var _btnNext = jQuery(_options.btnNext, _this);

        var _btnPlayPause = jQuery(_options.btnPlayPause, _this);

        var _btnPause = jQuery(_options.btnPause, _this);

        var _btnPlay = jQuery(_options.btnPlay, _this);

        var _pauseOnHover = _options.pauseOnHover;

        var _autoRotation = _options.autoRotation;

        var _activeClass = _options.activeClass;

        var _disabledClass = _options.disabledClass;

        var _pausedClass = _options.pausedClass;

        var _playClass = _options.playClass;

        var _autoHeight = _options.autoHeight;

        var _duration = _options.duration;

        var _switchTime = _options.switchTime;

        var _controlEvent = _options.event;

        var _currentNum = (_options.currentNum ? jQuery(_options.currentNum, _this) : false);

        var _allNum = (_options.allNum ? jQuery(_options.allNum, _this) : false);

        var _startSlide = _options.startSlide;

        var _noCycle = _options.noCircle;

        var _onChange = _options.onChange;

        var _pagerGener = _options.pagerGener;

        var _pagerHold = jQuery(_options.pagerHold,_this);

        var _caption = jQuery(_options.caption,_this);

        var _captions = jQuery('>a',_caption);

        var _paging = '';

        if(_pagerGener){

            for(var i=0; i< _slides.length; i++){

                _paging += '<li><a href="#">'+(i+1)+'</a></li>';

            }

            _pagerHold.html('<ul>'+_paging+'</ul>');

        }

        var _pagerLinks = jQuery(_options.pagerLinks, _this);

        // gallery init

        var _hover = false;

        var _prevIndex = 0;

        var _currentIndex = 0;

        var _slideCount = _slides.length;

        var _timer;

        if(_slideCount < 2) return;

        _slides.eq(_currentIndex).parent().css({height:_slides.eq(_currentIndex).outerHeight(true)});

        _prevIndex = _slides.index(_slides.filter('.'+_activeClass));

        if(_prevIndex < 0) _prevIndex = _currentIndex = 0;

        else _currentIndex = _prevIndex;

        if(_startSlide != null) {

            if(_startSlide == 'random') _prevIndex = _currentIndex = Math.floor(Math.random()*_slideCount);

            else _prevIndex = _currentIndex = parseInt(_startSlide);

        }

        _slides.hide().eq(_currentIndex).show();

        _captions.hide().eq(_currentIndex).show();

        if(_autoRotation) _this.removeClass(_pausedClass).addClass(_playClass);

        else _this.removeClass(_playClass).addClass(_pausedClass);



        // gallery control

        if(_btnPrev.length) {

            _btnPrev.bind(_controlEvent,function(){

                prevSlide();

                return false;

            });

        }

        if(_btnNext.length) {

            _btnNext.bind(_controlEvent,function(){

                nextSlide();

                return false;

            });

        }

        if(_pagerLinks.length) {

            _pagerLinks.each(function(_ind){

                jQuery(this).bind(_controlEvent,function(){

                    if(_currentIndex != _ind) {

                        _prevIndex = _currentIndex;

                        _currentIndex = _ind;

                        switchSlide();

                    }

                    return false;

                });

            });

        }



        // play pause section

        if(_btnPlayPause.length) {

            _btnPlayPause.bind(_controlEvent,function(){

                if(_this.hasClass(_pausedClass)) {

                    _this.removeClass(_pausedClass).addClass(_playClass);

                    _autoRotation = true;

                    autoSlide();

                } else {

                    _autoRotation = false;

                    if(_timer) clearTimeout(_timer);

                    _this.removeClass(_playClass).addClass(_pausedClass);

                }

                return false;

            });

        }

        if(_btnPlay.length) {

            _btnPlay.bind(_controlEvent,function(){

                _this.removeClass(_pausedClass).addClass(_playClass);

                _autoRotation = true;

                autoSlide();

                return false;

            });

        }

        if(_btnPause.length) {

            _btnPause.bind(_controlEvent,function(){

                _autoRotation = false;

                if(_timer) clearTimeout(_timer);

                _this.removeClass(_playClass).addClass(_pausedClass);

                return false;

            });

        }



        // gallery animation

        function prevSlide() {

            _prevIndex = _currentIndex;

            if(_currentIndex > 0) _currentIndex--;

            else {

                if(_noCycle) return;

                else _currentIndex = _slideCount-1;

            }

            switchSlide();

        }

        function nextSlide() {

            _prevIndex = _currentIndex;

            if(_currentIndex < _slideCount-1) _currentIndex++;

            else {

                if(_noCycle) return;

                else _currentIndex = 0;

            }

            switchSlide();

        }

        function refreshStatus() {

            if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);

            if(_currentNum) _currentNum.text(_currentIndex+1);

            if(_allNum) _allNum.text(_slideCount);

            _slides.eq(_prevIndex).removeClass(_activeClass);

            _slides.eq(_currentIndex).addClass(_activeClass);

            if(_noCycle) {

                if(_btnPrev.length) {

                    if(_currentIndex == 0) _btnPrev.addClass(_disabledClass);

                    else _btnPrev.removeClass(_disabledClass);

                }

                if(_btnNext.length) {

                    if(_currentIndex == _slideCount-1) _btnNext.addClass(_disabledClass);

                    else _btnNext.removeClass(_disabledClass);

                }

            }

            if(typeof _onChange === 'function') {

                _onChange(_this, _currentIndex);

            }

        }

        function switchSlide() {

            _slides.eq(_prevIndex).fadeOut(_duration);

            _slides.eq(_currentIndex).fadeIn(_duration);

            _captions.eq(_prevIndex).hide();

            _captions.eq(_currentIndex).show();

            if(_autoHeight) _slides.eq(_currentIndex).parent().animate({height:_slides.eq(_currentIndex).outerHeight(true)},{duration:_duration,queue:false});

            refreshStatus();

            autoSlide();

        }



        // autoslide function

        function autoSlide() {

            if(!_autoRotation || _hover) return;

            if(_timer) clearTimeout(_timer);

            _timer = setTimeout(nextSlide,_switchTime+_duration);

        }

        if(_pauseOnHover) {

            _this.hover(function(){

                _hover = true;

                if(_timer) clearTimeout(_timer);

            },function(){

                _hover = false;

                autoSlide();

            });

        }

        refreshStatus();

        autoSlide();

    });

};



// fix png

var transparentImage = "images/none.gif";

function fixTrans()

{

    if (typeof document.body.style.maxHeight == 'undefined') 

    {

        var imgs = document.getElementsByTagName("img");

        for (i = 0; i < imgs.length; i++)

        {   

            if (imgs[i].src.indexOf(transparentImage) != -1)

            {

                return;

            }

            if (imgs[i].src.indexOf(".png") != -1)

            {

                var src = imgs[i].src;

                imgs[i].src = transparentImage;

                imgs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";

                imgs[i].style.display = "inline-block";

            }

        }   

    }

};



// init Nav

function initNav() {

    initAutoScalingNav({

        menuId: "nav1",

        tag: "a",

        liHovering: true,

        flexible: false,

        equalLinks: false,

        sideClasses: true,

        spacing: 0

    });

    initAutoScalingNav({

        menuId: "nav2",

        tag: "a",

        liHovering: true,

        flexible: false,

        equalLinks: false,

        sideClasses: false,

        spacing: 0

    });

}

function initAutoScalingNav(o) {

    if (!o.menuId) o.menuId = "nav";

    if (!o.tag) o.tag = "span";

    if (!o.spacing) o.spacing = 0;

    if (!o.constant) o.constant = 0;

    if (!o.minPaddings) o.minPaddings = 0;

    if (!o.liHovering) o.liHovering = false;

    if (!o.sideClasses) o.sideClasses = false;

    if (!o.equalLinks) o.equalLinks = false;

    if (!o.flexible) o.flexible = false;

    var nav = document.getElementById(o.menuId);

    if(nav) {

        nav.className += " scaling-active";

        var lis = nav.getElementsByTagName("li");

        var asFl = [];

        var lisFl = [];

        var width = 0;

        for (var i=0, j=0; i<lis.length; i++) {

            if(lis[i].parentNode == nav) {

                var t = lis[i].getElementsByTagName(o.tag).item(0);

                asFl.push(t);

                asFl[j++].width = t.offsetWidth;

                lisFl.push(lis[i]);

                if(width < t.offsetWidth) width = t.offsetWidth;

            }

            if(o.liHovering) {

                lis[i].onmouseover = function() {

                    this.className += " hover";

                }

                lis[i].onmouseout = function() {

                    this.className = this.className.replace("hover", "");

                }

            }

        }

        var menuWidth = nav.clientWidth - asFl.length*o.spacing - o.constant;

        if(o.equalLinks && width * asFl.length < menuWidth) {

            for (var i=0; i<asFl.length; i++) {

                asFl[i].width = width;

            }

        }

        width = getItemsWidth(asFl);

        if(width < menuWidth) {

            var version = navigator.userAgent.toLowerCase();

            for (var i=0; getItemsWidth(asFl) < menuWidth; i++) {

                asFl[i].width++;

                if(!o.flexible) {

                    asFl[i].style.width = asFl[i].width + "px";

                }

                if(i >= asFl.length-1) i=-1;

            }

            if(o.flexible) {

                for (var i=0; i<asFl.length; i++) {

                    width = (asFl[i].width - o.spacing - o.constant/asFl.length)/menuWidth*100;

                    if(i != asFl.length-1) {

                        lisFl[i].style.width = width + "%";

                    }

                    else {

                        if(navigator.appName.indexOf("Microsoft Internet Explorer") == -1 || version.indexOf("msie 8") != -1 || version.indexOf("msie 9") != -1)

                            lisFl[i].style.width = width + "%";

                    }

                }

            }

        }

        else if(o.minPaddings > 0) {

            for (var i=0; i<asFl.length; i++) {

                asFl[i].style.paddingLeft = o.minPaddings + "px";

                asFl[i].style.paddingRight = o.minPaddings + "px";

            }

        }

        if(o.sideClasses) {

            lisFl[0].className += " first-child";

            lisFl[0].getElementsByTagName(o.tag).item(0).className += " first-child-a";

            lisFl[lisFl.length-1].className += " last-child";

            lisFl[lisFl.length-1].getElementsByTagName(o.tag).item(0).className += " last-child-a";

        }

        nav.className += " scaling-ready";

    }

    function getItemsWidth(a) {

        var w = 0;

        for(var q=0; q<a.length; q++) {

            w += a[q].width;

        }

        return w;

    }

};
