﻿// page init
jQuery(function () {
    initOpenClose();
    initGallery();
});

// initGallery, slideshow
function initGallery() {

    jQuery("#gallery").cycle({
        fx: 'scrollHorz',
        pager: '#switcher'
    });

//    jQuery('.slide-block').each(function () {
//        var holder = jQuery(this),
//			el = jQuery('.frame > ul > li');

//        if (el.length > 1) {
//            holder.scrollGallery({
//                sliderHolder: '.frame',
//                pagerLinks: '.switcher li',
//                circleSlide: true,
//                pauseOnHover: true,
//                autoRotation: true,
//                switchTime: 4000,
//                duration: 2000
//            })
//        }
//    });
}

// open-close init, sidebar
function initOpenClose() {
    jQuery('.accordion li').OpenClose({
        activeClass: 'opened',
        opener: 'a.opener',
        slider: 'div.slide',
        effect: 'slide',
        animSpeed: 500
    });
}

// open-close plugin
jQuery.fn.OpenClose = function (_options) {
    // default options
    var _options = jQuery.extend({
        activeClass: 'active',
        opener: '.opener',
        slider: '.slide',
        animSpeed: 400,
        animStart: false,
        animEnd: false,
        effect: 'fade',
        event: 'click'
    }, _options);

    return this.each(function () {
        // options
        var _holder = jQuery(this);
        var _slideSpeed = _options.animSpeed;
        var _activeClass = _options.activeClass;
        var _opener = jQuery(_options.opener, _holder);
        var _slider = jQuery(_options.slider, _holder);
        var _animStart = _options.animStart;
        var _animEnd = _options.animEnd;
        var _effect = _options.effect;
        var _event = _options.event;
        if (_slider.length) {
            _opener.bind(_event, function () {
                if (!_slider.is(':animated')) {
                    if (typeof _animStart === 'function') _animStart();
                    if (_holder.hasClass(_activeClass)) {
                        _slider[_effect == 'fade' ? 'fadeOut' : 'slideUp'](_slideSpeed, function () {
                            if (typeof _animEnd === 'function') _animEnd();
                        });
                        _holder.removeClass(_activeClass);
                    } else {
                        _holder.addClass(_activeClass);
                        _slider[_effect == 'fade' ? 'fadeIn' : 'slideDown'](_slideSpeed, function () {
                            if (typeof _animEnd === 'function') _animEnd();
                        });
                    }
                }
                return false;
            });
            if (_holder.hasClass(_activeClass)) _slider.show();
            else _slider.hide();
        }
    });
}

//// scrolling gallery plugin
//jQuery.fn.scrollGallery = function (_options) {
//    var _options = jQuery.extend({
//        sliderHolder: '> div',
//        slider: '> ul',
//        slides: '> li',
//        pagerLinks: 'div.pager a',
//        btnPrev: 'a.link-prev',
//        btnNext: 'a.link-next',
//        activeClass: 'active',
//        disabledPrevClass: 'prev-disabled',
//        disabledNextClass: 'next-disabled',
//        generatePagination: 'div.pg-holder',
//        curNum: 'em.scur-num',
//        allNum: 'em.sall-num',
//        circleSlide: true,
//        pauseClass: 'gallery-paused',
//        pauseButton: 'none',
//        pauseOnHover: true,
//        autoRotation: false,
//        stopAfterClick: false,
//        switchTime: 5000,
//        duration: 650,
//        easing: 'swing',
//        event: 'click',
//        splitCount: false,
//        afterInit: false,
//        vertical: false,
//        step: false
//    }, _options);

//    return this.each(function () {
//        // gallery options
//        var _this = jQuery(this);
//        var _sliderHolder = _options.sliderHolder ? jQuery(_options.sliderHolder, _this) : _this;
//        var _slider = jQuery(_options.slider, _sliderHolder);
//        var _slides = jQuery(_options.slides, _slider);
//        var _btnPrev = jQuery(_options.btnPrev, _this);
//        var _btnNext = jQuery(_options.btnNext, _this);
//        var _pagerLinks = jQuery(_options.pagerLinks, _this);
//        var _generatePagination = jQuery(_options.generatePagination, _this);
//        var _curNum = jQuery(_options.curNum, _this);
//        var _allNum = jQuery(_options.allNum, _this);
//        var _pauseButton = jQuery(_options.pauseButton, _this);
//        var _pauseOnHover = _options.pauseOnHover;
//        var _pauseClass = _options.pauseClass;
//        var _autoRotation = _options.autoRotation;
//        var _activeClass = _options.activeClass;
//        var _disabledPrevClass = _options.disabledPrevClass;
//        var _disabledNextClass = _options.disabledNextClass;
//        var _easing = _options.easing;
//        var _duration = _options.duration;
//        var _switchTime = _options.switchTime;
//        var _controlEvent = _options.event;
//        var _step = _options.step;
//        var _vertical = _options.vertical;
//        var _circleSlide = _options.circleSlide;
//        var _stopAfterClick = _options.stopAfterClick;
//        var _afterInit = _options.afterInit;
//        var _splitCount = _options.splitCount;

//        // gallery init
//        if (!_slides.length) return;

//        if (_splitCount) {
//            var curStep = 0;
//            var newSlide = $('<slide>').addClass('split-slide');
//            _slides.each(function () {
//                newSlide.append(this);
//                curStep++;
//                if (curStep > _splitCount - 1) {
//                    curStep = 0;
//                    _slider.append(newSlide);
//                    newSlide = $('<slide>').addClass('split-slide');
//                }
//            });
//            if (curStep) _slider.append(newSlide);
//            _slides = _slider.children();
//        }

//        var _currentStep = 0;
//        var _sumWidth = 0;
//        var _sumHeight = 0;
//        var _hover = false;
//        var _pause = false;
//        var _stepWidth;
//        var _stepHeight;
//        var _stepCount;
//        var _offset;
//        var _timer;

//        _slides.each(function () {
//            _sumWidth += $(this).outerWidth(true);
//            _sumHeight += $(this).outerHeight(true);
//        });

//        // calculate gallery offset
//        function recalcOffsets() {
//            if (_vertical) {
//                if (_step) {
//                    _stepHeight = _slides.eq(_currentStep).outerHeight(true);
//                    _stepCount = Math.ceil((_sumHeight - _sliderHolder.height()) / _stepHeight) + 1;
//                    _offset = -_stepHeight * _currentStep;
//                } else {
//                    _stepHeight = _sliderHolder.height();
//                    _stepCount = Math.ceil(_sumHeight / _stepHeight);
//                    _offset = -_stepHeight * _currentStep;
//                    if (_offset < _stepHeight - _sumHeight) _offset = _stepHeight - _sumHeight;
//                }
//            } else {
//                if (_step) {
//                    _stepWidth = _slides.eq(_currentStep).outerWidth(true) * _step;
//                    _stepCount = Math.ceil((_sumWidth - _sliderHolder.width()) / _stepWidth) + 1;
//                    _offset = -_stepWidth * _currentStep;
//                    if (_offset < _sliderHolder.width() - _sumWidth) _offset = _sliderHolder.width() - _sumWidth;
//                } else {
//                    _stepWidth = _sliderHolder.width();
//                    _stepCount = Math.ceil(_sumWidth / _stepWidth);
//                    _offset = -_stepWidth * _currentStep;
//                    if (_offset < _stepWidth - _sumWidth) _offset = _stepWidth - _sumWidth;
//                }
//            }
//        }

//        // gallery control
//        if (_btnPrev.length) {
//            _btnPrev.bind(_controlEvent, function () {
//                if (!_btnPrev.hasClass(_disabledPrevClass)) {
//                    if (_stopAfterClick) stopAutoSlide();
//                    prevSlide();
//                }
//                return false;
//            });
//        }
//        if (_btnNext.length) {
//            _btnNext.bind(_controlEvent, function () {
//                if (!_btnNext.hasClass(_disabledNextClass)) {
//                    if (_stopAfterClick) stopAutoSlide();
//                    nextSlide();
//                }
//                return false;
//            });
//        }
//        if (_generatePagination.length) {
//            _generatePagination.empty();
//            recalcOffsets();
//            var _list = $('<ul />');
//            for (var i = 0; i < _stepCount; i++) $('<li><a href="#">' + (i + 1) + '</a></li>').appendTo(_list);
//            _list.appendTo(_generatePagination);
//            _pagerLinks = _list.children();
//        }
//        if (_pagerLinks.length) {
//            _pagerLinks.each(function (_ind) {
//                jQuery(this).bind(_controlEvent, function () {
//                    if (_currentStep != _ind) {
//                        if (_stopAfterClick) stopAutoSlide();
//                        _currentStep = _ind;
//                        switchSlide();
//                    }
//                    return false;
//                });
//            });
//        }

//        // gallery animation
//        function prevSlide() {
//            recalcOffsets();
//            if (_currentStep > 0) _currentStep--;
//            else if (_circleSlide) _currentStep = _stepCount - 1;
//            switchSlide();
//        }
//        function nextSlide() {
//            recalcOffsets();
//            if (_currentStep < _stepCount - 1) _currentStep++;
//            else if (_circleSlide) _currentStep = 0;
//            switchSlide();
//        }
//        function refreshStatus() {
//            if (_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentStep).addClass(_activeClass);
//            if (!_circleSlide) {
//                _btnPrev.removeClass(_disabledPrevClass);
//                _btnNext.removeClass(_disabledNextClass);
//                if (_currentStep == 0) _btnPrev.addClass(_disabledPrevClass);
//                if (_currentStep == _stepCount - 1) _btnNext.addClass(_disabledNextClass);
//            }
//            if (_curNum.length) _curNum.text(_currentStep + 1);
//            if (_allNum.length) _allNum.text(_stepCount);
//        }
//        function switchSlide() {
//            recalcOffsets();
//            if (_vertical) _slider.animate({ marginTop: _offset }, { duration: _duration, queue: false, easing: _easing });
//            else _slider.animate({ marginLeft: _offset }, { duration: _duration, queue: false, easing: _easing });
//            refreshStatus();
//            autoSlide();
//        }

//        // autoslide function
//        function stopAutoSlide() {
//            if (_timer) clearTimeout(_timer);
//            _this.addClass(_pauseClass);
//            _autoRotation = false;
//        }
//        function autoSlide() {
//            if (!_autoRotation || _hover || _pause) 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();
//            });
//        }
//        recalcOffsets();
//        refreshStatus();
//        autoSlide();

//        // pause buttton
//        if (_pauseButton.length) {
//            _pauseButton.click(function () {
//                if (_this.hasClass(_pauseClass)) {
//                    _this.removeClass(_pauseClass);
//                    _autoRotation = true;
//                    autoSlide();
//                } else {
//                    _this.addClass(_pauseClass);
//                    stopAutoSlide();
//                }
//                return false;
//            });
//        }

//        _this.bind('pause', function () {
//            _pause = true;
//            if (_timer) clearTimeout(_timer);
//        }).bind('play', function () {
//            _pause = false;
//            autoSlide();
//        })

//        if (_afterInit && typeof _afterInit === 'function') _afterInit(_this, _slides);
//    });
//}

// mobile browsers detect
browserPlatform = {
    platforms: [
		{uaString: ['BlackBerry', 'midp'], cssFile: 'blackberry.css' }, // Blackberry <5
		{uaString: ['symbian', 'midp'], cssFile: 'symbian.css' }, // Symbian phones
		{uaString: ['opera', 'mobi'], cssFile: 'opera.css' }, // Opera Mobile
		{uaString: ['msie', 'ppc'], cssFile: 'ieppc.css' }, // IE Mobile <6
		{uaString: 'iemobile', cssFile: 'iemobile.css' }, // IE Mobile 6+
		{uaString: 'webos', cssFile: 'webos.css' }, // Palm WebOS
		{uaString: 'Android', cssFile: 'android.css' }, // Android
		{uaString: ['BlackBerry', '6.0', 'mobi'], cssFile: 'blackberry6.0.css' }, // Blackberry 6+
		{uaString: 'ipad', cssFile: 'ipad.css', miscHead: '<meta name="viewport" content="width=device-width" />' }, // iPad
		{uaString: ['safari', 'mobi'], cssFile: 'safari.css', miscHead: '<meta name="viewport" content="width=device-width" />'} // iPhone and other webkit browsers
	],
    options: {
        cssPath: 'Css/',
        mobileCSS: 'allmobile.css'
    },
    init: function () {
        this.checkMobile();
        this.parsePlatforms();
        return this;
    },
    checkMobile: function () {
        if (this.uaMatch('mobi') || this.uaMatch('midp') || this.uaMatch('ppc') || this.uaMatch('webos')) {
            this.attachStyles({ cssFile: this.options.mobileCSS });
        }
    },
    parsePlatforms: function () {
        for (var i = 0; i < this.platforms.length; i++) {
            if (typeof this.platforms[i].uaString === 'string') {
                if (this.uaMatch(this.platforms[i].uaString)) {
                    this.attachStyles(this.platforms[i]);
                    break;
                }
            } else {
                for (var j = 0, allMatch = true; j < this.platforms[i].uaString.length; j++) {
                    if (!this.uaMatch(this.platforms[i].uaString[j])) {
                        allMatch = false;
                    }
                }
                if (allMatch) {
                    this.attachStyles(this.platforms[i]);
                    break;
                }
            }
        }
    },
    attachStyles: function (platform) {
        var head = document.getElementsByTagName('head')[0], fragment;
        var cssText = '<link rel="stylesheet" href="' + this.options.cssPath + platform.cssFile + '" type="text/css"/>';
        var miscText = platform.miscHead;
        if (platform.cssFile) {
            if (document.body) {
                fragment = document.createElement('div');
                fragment.innerHTML = cssText;
                head.appendChild(fragment.childNodes[0]);
            } else {
                document.write(cssText);
            }
        }
        if (platform.miscHead) {
            if (document.body) {
                fragment = document.createElement('div');
                fragment.innerHTML = miscText;
                head.appendChild(fragment.childNodes[0]);
            } else {
                document.write(miscText);
            }
        }
    },
    uaMatch: function (str) {
        if (!this.ua) {
            this.ua = navigator.userAgent.toLowerCase();
        }
        return this.ua.indexOf(str.toLowerCase()) != -1;
    }
}.init();
