/* SWF
----------------------------------------------- */
var planetLang = planetLang || 'ru';
var skipIntro = skipIntro || false;
swfobject.embedSWF('swf/loader.swf', 'planetSWF', '912', '470', '10.1.0', 'swf/expressInstall.swf', {'language': planetLang, 'isSkipIntro': skipIntro,  'dataURL': '/swf/xml/' + planetLang + '.xml'}, {'wmode': 'opaque', 'base': '/swf/'});


/* DOM Ready
----------------------------------------------- */
$(function(){
    $('.teasers').listFlow();
    $('.gallery').listFlow();

    $('label.placeholder').placeholder();
    $('.feedback select').jSelect({speed: 50});
    $('.feedback form').formSubmit();

    $('.hoverable').hoverable();

    $('.gallery .list a').fancybox({
        autoScale: true,
        titleShow: false,
        padding: 0,
        overlayColor: '#000',
        overlayOpacity: 0.5
    });

    $('.awards .list a').fancybox({
        autoScale: true,
        titleShow: false,
        padding: 0,
        overlayColor: '#000',
        overlayOpacity: 0.5
    });

    $('.ph-tooltip').tooltip({
        delay: 0,
        fade: 100,
        track: true,
        showURL: false,
        extraClass: 'tooltip'
    });
});



/* hoverable
----------------------------------------------- */
(function($){
$.fn.hoverable = function(value){
    var hoverClass = value || 'hover';

    return this.each(function(){
        var $el = $(this);

        $el.hover(
            function(){
                $el.addClass(hoverClass);
            },
            function(){
                $el.removeClass(hoverClass);
            }
        );
    });
};
})(jQuery);


/* formSubmit
----------------------------------------------- */
(function($){
$.fn.formSubmit = function(o){
    o = $.extend({
        url: '',
        type: 'post',
        speed: 'normal',
        dataType: 'json'
    }, o || {});

    return this.each(function(){
        var $form = $(this),
            $inputs = $('input, select, textarea', $form),
            $submit = $('.i-submit .button', $form),
            $submitButton = $('.i-submit input:submit, .i-submit button:submit', $form),
            $notice = $('.notice', $form),
            $loading = $('.loading', $form),
            $status = $('.status', $form),
            submitURL = o.url || $form.attr('action');

        $form.validate({
            errorPlacement: function(error, el) {
            },
            highlight: function(el, errorClass) {
                $(el).closest('.i').addClass('i-error');
            },
            unhighlight: function(el, errorClass) {
                $(el).closest('.i').removeClass('i-error');
            },
            submitHandler: function() {
                $inputs.blur();
                $submitButton.attr('disabled', 'disabled');
                $notice.fadeOut(o.speed);
                $submit.fadeOut(o.speed, function(){
                    $loading.fadeIn(o.speed, function(){
                        $form.ajaxSubmit({
                            url: submitURL + '&ajax=1',
                            type: o.type,
                            beforeSubmit: function(){
                                $inputs.attr('disabled', 'disabled');
                                //$('.jselect').unbind();
                            },
                            success: function(responseText, statusText, xhr, $form){
                                $loading.fadeOut(o.speed, function(){
                                    if(responseText.errors)
                                    {
                                      $.each(responseText.errors, function(i, el)
                                      {
                                        $(el).closest('div.i').addClass('i-error');
                                      });
                                      $submitButton.attr('disabled', '').fadeIn(o.speed);
                                      $inputs.attr('disabled', '');
                                      $submit.fadeIn(o.speed);
                                      $notice.fadeIn(o.speed);
                                    }
                                    if(responseText.mailSent == '1')
                                    {
                                      $status.fadeIn(o.speed);
                                    }
                                    else
                                    {
                                      $submitButton.attr('disabled', '').fadeIn(o.speed);
                                      $inputs.attr('disabled', '');
                                      $submit.fadeIn(o.speed);
                                      $notice.fadeIn(o.speed);
                                    }
                                    
                                });
                            }
                        });
                    });
                });
            }
        });
    });
};
})(jQuery);



/* placeholder
----------------------------------------------- */
(function($){
$.fn.placeholder = function(value){
    var className = value || 'placeholder';

    return this.each(function(){
        var $label = $(this),
            placeholder = $label.text(),
            $input = $('#' + $label.attr('for')),
            isTextarea = ($input.get(0).nodeName.toLowerCase() == 'textarea');

        $input.data('placeholder', placeholder);

        if ($input.val() == '' || $input.val() == placeholder) {
            if (isTextarea) $input.text(placeholder);
            $input.val(placeholder).addClass(className);
        }

        $input.focus(function(){
            if ($input.val() == placeholder) {
                if (isTextarea) $input.text('');
                $input.val('').removeClass(className);
            }
        }).blur(function(){
            if ($input.val() == '') {
                if (isTextarea) $input.text(placeholder);
                $input.val(placeholder).addClass(className);
            }
        });
    });
};
})(jQuery);



/* jSelect
----------------------------------------------- */
(function($){
$.fn.jSelect = function(o){
    o = $.extend({
        speed: 'normal'
    }, o || {});

    var $doc = $(document),
        isExpanded = false;

    return this.each(function(){
        var $select = $(this),
            selectWidth = this.offsetWidth,
            $optionArray = $('option', $select),
            $optionSelected = $optionArray.filter(':selected'),
            optionSelectedText = $optionSelected.text(),

            $jSelect = $('<div class="jselect" />'),
            $jSelectTitle = $('<div class="jselect-title" />').appendTo($jSelect),
            $jSelectTitleWrap = $('<div class="jselect-title-wrap" />').appendTo($jSelectTitle),
            $jSelectTitleSPAN = $('<span class="placeholder" />').appendTo($jSelectTitleWrap),
            $jSelectList = $('<div class="jselect-list" />').appendTo($jSelect),
            $jSelectListUL = $('<ul />').appendTo($jSelectList),
            $jSelectListArr = $('<div class="jselect-arr"></div>').appendTo($jSelectTitle);

        $select.hide();
        $jSelect.width(selectWidth);
        $jSelectTitleSPAN.text(optionSelectedText);

        if ($optionSelected.hasClass('placeholder')) {
            $jSelectTitleSPAN.addClass('placeholder');
        }

        $optionArray.each(function(i){
            var $option = $(this);

            if ($option.hasClass('placeholder')) {
                return;
            }

            $('<li />', {
                text: $option.text(),
                click: function(){
                    set($option);
                }
            }).hover(
                function(){
                    $(this).addClass('hover');
                },
                function(){
                    $(this).removeClass('hover');
                }
            ).appendTo($jSelectListUL);
        });

        $jSelect.insertAfter($select);

        $jSelect.click(function(e){
            e.stopPropagation();

            if ($jSelect.hasClass('jselect-expanded')) {
                hide();
            } else {
                if (isExpanded) {
                    hide();
                    show();
                } else {
                    show();
                }
            }
        });

        $doc.click(function(){
            if (isExpanded) {
                hide();
            }
        });

        function show() {
            $jSelect.addClass('jselect-expanded');
            $jSelectList.show();
            isExpanded = true;
        }

        function hide() {
            $('.jselect').removeClass('jselect-expanded');
            $('.jselect-list').hide();
            isExpanded = false;
        }

        function set($option) {
            $jSelectTitleSPAN.text($option.text()).removeClass('placeholder');
            $select.val($option.val()).change();
        }
    });
};
})(jQuery);



/* listFlow
----------------------------------------------- */
(function($){
$.fn.listFlow = function(o){
    o = $.extend({
        speed: 'normal',
        vertical: false,
        resize: false
    }, o || {});

    return this.each(function(){
        var $root = $(this),

            $viewport = $('.list-viewport', $root),
            viewportWidth = 0, viewportHeight = 0,

            $canvas = $('.list-canvas', $root),
            canvasWidthCurrent = 0, canvasWidthNew = 0, canvasHeightCurrent = 0, canvasHeightNew = 0,

            $list = $('.list', $root),
            listWidth, listHeight,

            $items = $('.i', $list),
            itemsCount = $items.length,

            $br = $('.br', $list),

            colsWidth = $items.outerWidth(),
            colsGap = parseInt($items.css('margin-right')),
            colsWidthFull = colsWidth + colsGap,
            colsCount = colsVisibleCount = 1,

            rowsHeight = $items.height(),
            rowsGap = $br.height() || 0,
            rowsHeightFull = rowsHeight + rowsGap,
            rowsCount = 0, rowsVisibleCount = 0,

            $controlPrev = $('.list-control-prev', $root),
            $controlNext = $('.list-control-next', $root),

            pagesCountCurrent = 0, pagesCountNew = 0,
            pageIndexCurrent = 0, pageIndexNew = 0,

            busy = false;

        if (itemsCount == 0) {
            return;
        }


        $controlPrev.click(function(){
            if (busy) {
                return;
            }

            pageIndexNew = pageIndexCurrent - 1;

            setPage();
        });


        $controlNext.click(function(){
            if (busy) {
                return;
            }

            pageIndexNew = pageIndexCurrent + 1;
            
            setPage();
        });


        renderList(true);


        if (o.resize) {
            $(window).resize(function(){
                renderList();
            });
        }


        function renderList(isInit) {
            setVars();

            if (canvasWidthNew == canvasWidthCurrent && canvasHeightNew == canvasHeightCurrent) {
                return;
            }

            $list.width(listWidth).height(listHeight);

            if (isInit) {
                $canvas.width(canvasWidthNew).height(canvasHeightNew);
                canvasWidthCurrent = canvasWidthNew;
                canvasHeightCurrent = canvasHeightNew;
                renderControls();
            } else {
                $canvas.animate({
                    width: canvasWidthNew,
                    height: canvasHeightNew
                }, {
                    duration: o.speed,
                    queue: false,
                    complete: function(){
                        canvasWidthCurrent = canvasWidthNew;
                        canvasHeightCurrent = canvasHeightNew;
                        renderControls();
                    }
                });
            }
        }


        function renderControls() {
            if (pagesCountNew == pagesCountCurrent) {
                return;
            }

            if (pagesCountNew < 2) {
                $controlPrev.fadeOut(o.speed);
                $controlNext.fadeOut(o.speed);
                return;
            }

            if (pageIndexNew > 0) {
                $controlPrev.fadeIn(o.speed);
            } else {
                $controlPrev.fadeOut(o.speed);
            }

            if (pageIndexNew + 1 < pagesCountNew) {
                $controlNext.fadeIn(o.speed);
            } else {
                $controlNext.fadeOut(o.speed);
            }
        }


        function setPage() {
            busy = true;

            if (pageIndexNew < 0 || pageIndexNew >= pagesCountNew) {
                busy = false;
                return;
            }

            renderControls();

            var offsetLeft = offsetTop = 0,
                animateOpts = {
                    duration: o.speed,
                    queue: false,
                    complete: function(){
                        pageIndexCurrent = pageIndexNew;
                        busy = false;
                    }
                };

            if (o.vertical) {
                offsetTop = $list.position().top + ( ( pageIndexCurrent - pageIndexNew ) * ( canvasHeightNew + rowsGap ) );

                if (offsetTop > 0) {
                    offsetTop = 0;
                } else if (listHeight + offsetTop < canvasHeightNew + rowsGap) {
                    offsetTop = -( listHeight - canvasHeightNew );
                }

                $list.animate({top: offsetTop}, animateOpts);
            } else {
                offsetLeft = $list.position().left + ( ( pageIndexCurrent - pageIndexNew ) * ( canvasWidthNew + colsGap ) );

                if (offsetLeft > 0) {
                    offsetLeft = 0;
                } else if (listWidth + offsetLeft < canvasWidthNew + colsGap) {
                    offsetLeft = -( listWidth - canvasWidthNew - colsGap );
                }

                $list.animate({left: offsetLeft}, animateOpts);
            }
        }


        function setVars() {
            if (o.viewportProvider) {
                var viewport = o.viewportProvider();
                viewportWidth = viewport.width;
                viewportHeight = viewport.height;
            } else {
                viewportWidth = $viewport.width();
                viewportHeight = $viewport.height();
            }

            colsVisibleCount = Math.floor( ( viewportWidth + colsGap ) / colsWidthFull );
            rowsVisibleCount = Math.floor( ( viewportHeight + rowsGap ) / rowsHeightFull );

            if (o.vertical) {
                colsCount = colsVisibleCount;
                rowsCount = Math.ceil( itemsCount / colsVisibleCount );
            } else {
                colsCount = Math.ceil( itemsCount / rowsVisibleCount );
                rowsCount = rowsVisibleCount;
            }

            if (colsVisibleCount < 1) {
                colsVisibleCount = 1;
            } else if (colsVisibleCount > colsCount) {
                colsVisibleCount = colsCount;
            }

            if (rowsVisibleCount < 1) {
                rowsVisibleCount = 1;
            } else if (rowsVisibleCount > rowsCount) {
                rowsVisibleCount = rowsCount;
            }

            canvasWidthNew = ( colsVisibleCount * colsWidthFull ) - colsGap;
            canvasHeightNew = ( rowsVisibleCount * rowsHeightFull ) - rowsGap;

            if (rowsVisibleCount < rowsCount) {
                canvasHeightNew = ( rowsVisibleCount * rowsHeightFull ) - rowsGap;
            } else {
                canvasHeightNew = ( rowsCount * rowsHeightFull ) - rowsGap;
            }

            listWidth = colsCount * colsWidthFull;
            listHeight = ( rowsCount * rowsHeightFull ) - rowsGap;

            if (o.vertical) {
                pagesCountNew = Math.ceil( rowsCount / rowsVisibleCount );
            } else {
                pagesCountNew = Math.ceil( colsCount / colsVisibleCount );
            }

            if (pageIndexCurrent < pagesCountNew) {
                pageIndexNew = pageIndexCurrent;
            } else {
                pageIndexNew = pagesCountNew - 1;
            }
        }
    });
};
})(jQuery);

