/* DOM Ready
----------------------------------------------- */
fixPNG();
$(function(){
    $('.offers').jOffers();
    $('.offers .list1 div').hoverable();
    $('.bn-swap').bnSwap();
    $('.pages span').hoverable();
    $('.icon').hoverable('icon-hover');
    $('button').hoverable();
    $('label.placeholder').placeholder();

    $('.form select').jSelect({maxItems: 5});
    $('.form form').formSubmit();

    $('.drop-menu').dropMenu();

    $('.blog-entry-list').ajaxPages({contentID: 'ajaxBlogEntryListContent'});
    $('.blog-comment-list').ajaxPages({contentID: 'ajaxBlogCommentListContent'});
    $('.apartment-list').ajaxPages({contentID: 'ajaxApartmentListContent'});
    

    $('.certif-list .list a').imageFlow();
    $('.gallery .i a, .gallery .link-all a').imageFlow();

    $('.certif-images a').imageFlow();


    $('.country-filter').jCountries({speed: 350});
    $('.country-comment-list').ajaxPages({contentID: 'ajaxCountryCommentListContent'});

    $('.event-filter select').jSelect();

    $('.event-filter .input-date').datepick({
        rangeSelect: true,
        monthsToShow: 2,
        showTrigger: '#eventFilterCalendar',
        rangeSeparator: '-',
        onSelect: function(dates) { 
            var dateFrom = dates[0],
                dateTill = dates[1];

            dateFrom = $.datepick.formatDate(dateFrom);
            dateTill = $.datepick.formatDate(dateTill);

            $('.event-filter .date-from').html(dateFrom || '&mdash;');
            $('.event-filter .date-till').html(dateTill || '&mdash;');
        }
    });
    $('#inputDatepicker').datepick({
        rangeSelect: true,
        monthsToShow: 2
    });
    
    $('.jswap').jswap();

    $('.blog-year-list').blogYears({easing: 'easeOutExpo'});

    $('.multiform').multiform();
});


/* multiform
----------------------------------------------- */
(function($){
var $doc = $(document);

$.fn.multiform = function(o){
    o = $.extend({
        speed: 'normal'
    }, o || {});

    return this.each(function(){
        var $root = $(this),
            $pages = $('.pages-i', $root),
            $navPrev = $('.pages-nav-prev', $root),
            $navNext = $('.pages-nav-next', $root),
            $persons = $('.person-list .i', $root),
            $inputTo = $('input[name="to"]', $root),
            currentIndex = 0,
            busy = false;


        $pages.click(function(){
            var index = $pages.index(this);
            go(index);
        });


        $navPrev.click(function(){
            goPrev();
        });


        $navNext.click(function(){
            goNext();
        });
        
        
        function goPrev() {
            var index = currentIndex - 1;
            go(index);
        }


        function goNext() {
            var index = currentIndex + 1;
            go(index);
        }
        

        function go(index) {
            if (busy || index == currentIndex || index < 0 || index >= $pages.length) {
                return;
            }
            
            busy = true;
            
            $inputTo.val($pages.eq(index).text());
            
            $pages.removeClass('pages-i-act').eq(index).addClass('pages-i-act');

            if (index > 0) {
                $navPrev.removeClass('pages-nav-prev-disabled');
            } else {
                $navPrev.addClass('pages-nav-prev-disabled');
            }

            if (index < $pages.length - 1) {
                $navNext.removeClass('pages-nav-next-disabled');
            } else {
                $navNext.addClass('pages-nav-next-disabled');
            }

            $persons.eq(currentIndex).fadeOut(o.speed, function(){
                $persons.eq(index).fadeIn(o.speed, function(){
                    currentIndex = index;
                    busy = false;
                });
            });
        }
    });
};
})(jQuery);


/* jswap
----------------------------------------------- */
(function($){
$.fn.jswap = function(o){
    o = $.extend({
        speed: 'normal',
        easing: 'swing'
    }, o || {});

    return this.each(function(){
        var $root = $(this),

            $content = $('.jswap-content', $root),
            $items = $('.jswap-content-i', $root),
            $controls = $('.jswap-controls', $root),

            $pages = $('<div class="jswap-pages" />'),
            $pagesItems,
            
            $navPrev = $('<div class="jswap-nav-prev jswap-nav" />'),
            $navNext = $('<div class="jswap-nav-next jswap-nav" />'),

            currentIndex = 0,

            busy = false;


        if ($items.length < 2) {
            return;
        }


        $navPrev.click(function(){
            goPrev();
        }).hover(
            function(){
                $(this).addClass('jswap-nav-hover');
            },
            function(){
                $(this).removeClass('jswap-nav-hover');
            }
        );

        if (currentIndex == 0) {
            $navPrev.addClass('jswap-nav-disabled');
        }


        $navNext.click(function(){
            goNext();
        }).hover(
            function(){
                $(this).addClass('jswap-nav-hover');
            },
            function(){
                $(this).removeClass('jswap-nav-hover');
            }
        );

        if (currentIndex == $items.length - 1) {
            $navPrev.addClass('jswap-nav-disabled');
        }


        var pagesItemsHTML = '';
        for (i = 1; i <= $items.length; i++) {
            pagesItemsHTML += '<div class="jswap-pages-i" />';
        }
        $pagesItems = $(pagesItemsHTML);
        $pagesItems.click(function(){
            go($pagesItems.index(this));
        }).hover(
            function(){
                $(this).addClass('jswap-pages-i-hover');
            },
            function(){
                $(this).removeClass('jswap-pages-i-hover');
            }
        ).eq(currentIndex).addClass('jswap-pages-i-act');


        $pages.append($pagesItems);
        $controls.append($navPrev, $pages, $navNext);


        function goPrev() {
            go(currentIndex - 1);
        }


        function goNext() {
            go(currentIndex + 1);
        }


        function go(index) {
            if (busy) {
                return;
            }

            busy = true;

            if (index === currentIndex || index < 0 || index >= $items.length) {
                busy = false;
                return;
            }

            if (index == 0) {
                $navPrev.addClass('jswap-nav-disabled');
            } else {
                $navPrev.removeClass('jswap-nav-disabled');
            }

            if (index == $items.length - 1) {
                $navNext.addClass('jswap-nav-disabled');
            } else {
                $navNext.removeClass('jswap-nav-disabled');
            }

            $pagesItems.removeClass('jswap-pages-i-act').eq(index).addClass('jswap-pages-i-act');

            $content.height($content.height());

            $items.eq(index).addClass('jswap-content-i-tmp');

            var height = $items.eq(index).outerHeight();

            $items.eq(index).removeClass('jswap-content-i-tmp').addClass('jswap-content-i-new');

            $items.eq(currentIndex).fadeOut(o.speed, function(){
                $content.animate({
                    height: height
                }, {
                    queue: false,
                    easing: o.easing,
                    duration: o.speed,
                    complete: function(){
                        $items.eq(index).fadeIn(o.speed, function(){
                            $items.eq(index).removeClass('jswap-content-i-new');
                            $content.height('auto');
                            currentIndex = index;
                            busy = false;
                        });
                    }
                });
            });
        }
    });
};
})(jQuery);



/* jCountries
----------------------------------------------- */
(function($){
$.fn.jCountries = function(o){
    o = $.extend({
        speed: 'normal',
        contentID: 'ajaxCountryFilterContent'
    }, o || {});


    return this.each(function(){
        var $root = $(this),

            $filter = $('.continent-list', $root),
            $filterItems = $('.icon', $filter),
            $filterLinks = $('a', $filter),

            $wrap = $('.ajax-country-filter-wrap', $root),
            $content = $('.ajax-country-filter-content', $root),
            $loading = $('.ajax-country-filter-loading', $root),
            $temp = $('.ajax-country-filter-temp', $root),

            currentIndex = -1,

            busy = false;


        if ($filterItems.filter('.act').length) {
            currentIndex = $filterItems.index($filterItems.filter('.act'));
        }


        $filterLinks.click(function(e){
            e.preventDefault();
        });


        $filterItems.click(function(e){
            var index = $filterItems.index(this);
            go(index);
        });

        
        function go(index) {
            if (busy) {
                return;
            }

            busy = true;

            if (index == currentIndex || index < 0 || index >= $filterItems.length) {
                busy = false;
                return;
            }

            $filterItems.eq(currentIndex).removeClass('act');
            $filterItems.eq(index).addClass('act');

            var loadURL = $filterItems.eq(index).find('a').attr('href');

            $wrap.height($wrap.height());
            $content.fadeOut(o.speed, function(){
                $loading.fadeIn(o.speed, function(){
                    $temp.load(loadURL + ' #' + o.contentID, function(response, status, xhr) {

                        var wrapHeight = $temp.height(),
                            contentHTML = $(' #' + o.contentID, response).html();

                        $temp.empty();

                        $wrap.animate({
                            height: wrapHeight
                        }, {
                            queue: false,
                            easing: 'easeOutExpo',
                            duration: o.speed,
                            complete: function(){
                                $loading.fadeOut(o.speed, function(){
                                    $content.html(contentHTML).fadeIn(o.speed, function(){
                                        $wrap.height('auto');

                                        currentIndex = index;
                                        busy = false;
                                    });
                                });
                            }
                        });
                    });
                });
            });
        }
    });
};
})(jQuery);



/* blogYears
----------------------------------------------- */
(function($){
$.fn.blogYears = function(o){
    o = $.extend({
        speed: 'normal',
        easing: 'swing',
        contentID: 'ajaxBlogYearContent'
    }, o || {});

    return this.each(function(){
        var $list = $(this),
            $links = $('a', $list),
            $items = $('.blog-year-list-i', $list),

            $wrap = $list.next('.blog-year-wrap'),
            $content = $('.blog-year-content', $wrap),
            $temp = $('.blog-year-temp', $wrap),
            $loading = $('.blog-year-loading', $wrap),

            currentIndex,
            busy = false;

        if ($items.length < 2) {
            return;
        }
        
        currentIndex = $items.index($items.filter('.blog-year-list-i-act'));
        
        $links.click(function(e){
            e.preventDefault();
        });
        
        $items.click(function(){
            var index = $items.index(this);
            go(index);
        });
        
        function go(index) {
            if (busy) {
                return;
            }
            
            busy = true;
            
            if (index == currentIndex) {
                busy = false;
                return;
            }

            $items.removeClass('blog-year-list-i-act').eq(index).addClass('blog-year-list-i-act');
            
            $wrap.height($wrap.height());

            var loadURL = $links.eq(index).attr('href');

            $content.fadeOut(o.speed, function(){
                
                $content.empty();
                
                $loading.fadeIn(o.speed, function(){
                    $temp.load(loadURL + ' #' + o.contentID, function(response) {

                        var height = $temp.height(),
                            html = $temp.html();

                        $temp.empty();

                        $wrap.animate({
                            height: height
                        }, {
                            queue: false,
                            easing: o.easing,
                            duration: o.speed,
                            complete: function(){
                                $loading.fadeOut(o.speed, function(){
                                    $content.html(html).fadeIn(o.speed);
                                    $wrap.height('auto');

                                    $('.blog-entry-list', $content).ajaxPages({contentID: 'ajaxBlogEntryListContent'});

                                    currentIndex = index;
                                    busy = false;
                                });
                            }
                        });
                    });
                });
            });
        }
    });
};
})(jQuery);



/* ajaxPages
----------------------------------------------- */
(function($){
$.fn.ajaxPages = function(o){
    o = $.extend({
        speed: 'normal',
        contentID: 'ajaxContent'
    }, o || {});

    return this.each(function(){
        var $root = $(this),

            $wrap = $('.ajax-pages-wrap', $root),
            $content = $('.ajax-pages-content', $root),
            $loading = $('.ajax-pages-loading', $root),
            $temp = $('.ajax-pages-temp', $root),

            $navPrev = $('.pages-nav-prev', $root),
            $navNext = $('.pages-nav-next', $root),

            $pages = $('.pages', $root),
            $links = $('a', $pages),
            $list = $('.pages-list', $root),
            $items = $('.pages-i', $list),
            $sepPrev = $('.pages-sep-prev', $list),
            $sepNext = $('.pages-sep-next', $list),

            currentIndex = 0,
            busy = false;


        $links.click(function(e){
            e.preventDefault();
        });


        $items.click(function(){
            var index = $items.index(this);
            go(index);
        });


        $navPrev.click(function(){
            var index = currentIndex - 1;
            go(index);
        });


        $navNext.click(function(){
            var index = currentIndex + 1;
            go(index);
        });


        function go(index) {
            if (busy) {
                return;
            }

            busy = true;

            if (index == currentIndex || index < 0 || index >= $items.length) {
                busy = false;
                return;
            }

            $items.removeClass('pages-i-act').eq(index).addClass('pages-i-act');

            if (index == 0) {
                $navPrev.addClass('pages-nav-prev-disabled');
            } else {
                $navPrev.removeClass('pages-nav-prev-disabled');
            }

            if (index == $items.length - 1) {
                $navNext.addClass('pages-nav-next-disabled');
            } else {
                $navNext.removeClass('pages-nav-next-disabled');
            }

            var loadURL = $links.eq(index).attr('href');

            $wrap.height($wrap.height());

            $content.fadeOut(o.speed, function(){
                
                $content.empty();

                $loading.fadeIn(o.speed, function(){
                    $temp.load(loadURL + ' #' + o.contentID, function(response, status, xhr) {

                        var wrapHeight = $temp.height(),
                            contentHTML = $(' #' + o.contentID, response).html();

                        $temp.empty();

                        $wrap.animate({
                            height: wrapHeight
                        }, {
                            queue: false,
                            easing: 'easeOutExpo',
                            duration: o.speed,
                            complete: function(){
                                $loading.fadeOut(o.speed, function(){
                                    $content.html(contentHTML).fadeIn(o.speed);
                                    $wrap.height('auto');

                                    $items.each(function(i){
                                        if (i == 0 || i == $items.length - 1 || $items.length <= 9 || i >= index - 2 && i <= index + 2 || i <= 6 && index <= 4 || i >= $items.length - 7 && index >= $items.length - 5) {
                                            $(this).show();
                                        } else {
                                            $(this).hide();
                                        }
                                    });
                                    
                                    if ( index > 4 && $items.length > 9 ) {
                                        $items.eq(index < $items.length - 5 ? ( index - 2 ) : $items.length - 7 ).before($sepPrev.show());
                                    } else {
                                        $sepPrev.remove();
                                    }
                                    
                                    if (index < $items.length - 5) {
                                        $items.eq(index >= 4 ? ( index + 2 ) : 6 ).after($sepNext.show());
                                    } else {
                                        $sepNext.remove();
                                    }

                                    currentIndex = index;
                                    busy = false;
                                });
                            }
                        });
                    });
                });
            });
        }
    });
};
})(jQuery);



/* bnSwap
----------------------------------------------- */
(function($){
var IE = $.browser.msie;


$.fn.bnSwap = function(o){
    o = $.extend({
        speed: 'normal',
        interval: 5000
    }, o || {});

    return this.each(function(){
        var $root = $(this),
            $items = $('.i', $root),
            index = 0,
            currentIndex = 0,
            t;

        go();

        function go() {
            clearTimeout(t);

            var index = currentIndex + 1;

            if (index > $items.length - 1) {
                index = 0;
            }

            setTimeout(function(){
                if (IE) {
                    $items.eq(currentIndex).css({'z-index': 2}).hide();
                    $items.eq(index).css({'z-index': 1}).show();
                    currentIndex = index;
                    go();
                } else {
                    $items.eq(currentIndex).css({'z-index': 2}).fadeOut(o.speed);
                    $items.eq(index).css({'z-index': 1}).fadeIn(o.speed, function(){
                        currentIndex = index;
                        go();
                    });
                }
            }, o.interval);
        }
    });
};
})(jQuery);


/* dropMenu
----------------------------------------------- */
(function($){
$.fn.dropMenu = function(o){
    o = $.extend({
        timeout: 300
    }, o || {});

    var $dropMenus = $('.drop-menu');

    return this.each(function(){
        var $root = $(this),
            $title = $('.drop-menu-title', $root),
            $content = $('.drop-menu-content', $root),
            t;

        $root.hover(
            function(){
                clearTimeout(t);
                $dropMenus.not(this).removeClass('drop-menu-hover');
                $root.addClass('drop-menu-hover');


                if ($content.outerWidth() < $title.outerWidth()) {
                    $content.width($title.width() - 2);
                }
            },
            function(){
                t = setTimeout(function(){
                    $root.removeClass('drop-menu-hover');
                }, o.timeout);
            }
        );
    });
};
})(jQuery);









/* jSelect
----------------------------------------------- */
(function($){
var $doc = $(document),
    $jSelectArray;


$.fn.jSelect = function(o){
    o = $.extend({
        speed: 'normal',
        maxItems: 10
    }, o || {});


    return this.each(function(){
        var $select = $(this),
            $options = $('option', $select),
            
            $arr,

            $title,
            $titleText,
            
            $list,
            $listContent,
            $listItemArray = $(),
            listItemHeight = 0,
            
            $scrollUp,
            $scrollDown,
            
            $jSelect = $('<div class="jselect" />').append(
                $title = $('<div class="jselect-title" />').append(
                    $('<div class="jselect-title-bg-l jselect-title-bg" />').append(
                        $('<div class="jselect-title-bg-r jselect-title-bg" />').append(
                            $titleText = $('<div class="jselect-title-text" />')
                        )
                    )
                ),
                $arr = $('<div class="jselect-arr" />'),
                $list = $('<div class="jselect-list" />').append(
                    $('<div class="jselect-list-bg-top-l jselect-list-bg"><div class="jselect-list-bg-top-r jselect-list-bg"></div></div>'),
                    $('<div class="jselect-list-bg-l jselect-list-bg" />').append(
                        $('<div class="jselect-list-bg-r jselect-list-bg" />').append(
                            $('<div class="jselect-list-wrap" />').append(
                                $scrollUp = $('<div class="jselect-scroll-up" />'),
                                $listContent = $('<div class="jselect-list-content" />'),
                                $scrollDown = $('<div class="jselect-scroll-down" />')
                            )
                        )
                    )
                )
            ),

            t;


        $titleText.text($options.eq(this.selectedIndex).text());


        $options.each(function(index){
            var $listItem = $('<div />', {
                'class': 'jselect-list-i',
                'html': this.innerHTML || '&nbsp;',
                'click': function(){
                    set(index);
                }
            });
            $listItemArray = $listItemArray.add($listItem);
        });


        $listItemArray.hover(
            function(){
                $(this).addClass('jselect-list-i-hover');
            },
            function(){
                $(this).removeClass('jselect-list-i-hover');
            }
        );


        $listContent.html($listItemArray);


        $jSelect.width($select.width()).attr('unselectable', 'on').bind('selectstart', function(e){
            e.preventDefault();
        });


        $select.hide().after($jSelect);


        $jSelectArray = $('.jselect');


        $arr.click(function(e){
            e.stopPropagation();
            show();
        });


        $title.click(function(e){
            e.stopPropagation();
            show();
        });


        $list.click(function(e){
            e.stopPropagation();
            hide();
        });


        $doc.click(function(){
            hide();
        });


        $scrollUp.hover(
            function(){
                t = setInterval(function(){
                    scrollUp();
                }, 100);
            },
            function(){
                clearInterval(t);
            }
        ).click(function(e){
            e.stopPropagation();
        });


        $scrollDown.hover(
            function(){
                t = setInterval(function(){
                    scrollDown();
                }, 100);
            },
            function(){
                clearInterval(t);
            }
        ).click(function(e){
            e.stopPropagation();
        });


        function show() {
            $jSelectArray.removeClass('jselect-expanded');
            $jSelect.addClass('jselect-expanded');
            if ($listItemArray.length > o.maxItems) {
                $scrollUp.show();
                $scrollDown.show();
                $list.css({'top': -9999});
                listItemHeight = $listItemArray[0].clientHeight;
                $listContent.height(listItemHeight * o.maxItems).css({'overflow': 'hidden'});
                $list.css({'top': 0});
            } else {
                $scrollUp.hide();
                $scrollDown.hide();
            }
        }


        function hide() {
            $jSelect.removeClass('jselect-expanded');
        }


        function set(index) {
            $select.attr('selectedIndex', index).change();
            $titleText.text($options.eq(index).text());
        }


        function scrollUp() {
            $listContent.scrollTop($listContent.scrollTop() - listItemHeight);
        }


        function scrollDown() {
            $listContent.scrollTop($listContent.scrollTop() + listItemHeight);
        }
    });
};
})(jQuery);



/* jOffers
----------------------------------------------- */
(function($){
$.fn.jOffers = function(o){
    o = $.extend({
        speed: 'normal',
        timeout1: 200,
        timeout2: 50,
        flashClass: 'sideflash',
        list2Width: 220
    }, o || {});

    return this.each(function(){
        var $root = $(this),
            $list1Items = $('.list1 li', $root),
            $list2 = $('.list2', $root),
            $items = $('.items .i', $root),

            $inputs = $('input', $root),
            $flashItems = $('.' + o.flashClass),

            currentIndex = 0,
            t1,
            t2,
            busy = false;


        offerChange(currentIndex);


        $list1Items.hover(
            function(){
                clearTimeout(t1);
                clearTimeout(t2);

                $list1Items.removeClass('act');

                var index = $list1Items.index(this);

                t1 = setTimeout(function(){
                    offerChange(index);
                }, o.timeout1);
            },
            function(){
                clearTimeout(t1);

                $list1Items.removeClass('act');
                $list1Items.eq(currentIndex).addClass('act');
            }
        );
        

        function offerChange(index) {
            clearTimeout(t2);

            $list1Items.removeClass('act');
            $list1Items.eq(index).addClass('act');

            var flashID = $inputs.eq(index).val();

            if (index == currentIndex) {

                t2 = setTimeout(function(){
                    list2Show(index);
                }, o.timeout2);

            } else {

                $flashItems.hide();

                $items.eq(currentIndex).css({'z-index': 2}).fadeOut(o.speed);
                $items.eq(index).css({'z-index': 1}).fadeIn(o.speed, function(){
                    t2 = setTimeout(function(){
                        list2Show(index);
                    }, o.timeout2);
                });

            }

            if (flashID) {
                $flashItems.filter('#' + flashID).fadeIn(1).sideflash();
            }

            currentIndex = index;
        }


        function list2Show(index) {
            $list2.width(0).eq(index).animate({
                width: o.list2Width
            }, {
                queue: false,
                easing: 'easeOutExpo',
                duration: o.speed
            });
        }
    });
};
})(jQuery);






/* sideflash
----------------------------------------------- */
(function($){
var $doc = $(document),
    viewport,
    getVieport = function(){
        return {
            width: document.documentElement.clientWidth,
            margin: (document.documentElement.clientWidth - 940) / 2
        };
    };

$.fn.sideflash = function(o){
    o = $.extend({
        speed: 'normal'
    }, o || {});

    viewport = getVieport();

    return this.each(function(){
        var $root = $(this),
            hidden = false;

        $doc.unbind('.sideflash').bind('mousemove.sideflash', function(e){
            if ($root[0].offsetWidth > viewport.margin - 20) {
                if (!hidden && e.pageX > viewport.width - $root[0].offsetWidth - 20) {
                    hide();
                } else if (hidden && e.pageX < viewport.width - $root[0].offsetWidth - 20) {
                    show();
                }
            }
        }).bind('mouseleave.sideflash', function(){
            show();
        });
        
        function hide() {
            hidden = true;
            go(viewport.margin - $root[0].offsetWidth - 20);
        }

        function show() {
            hidden = false;
            go(0);
        }

        function go(marginRight) {
            $root.stop().animate({
                marginRight: marginRight
            }, {
                queue: false,
                duration: o.speed
            });
        }
    });
};

$(function(){
    viewport = getVieport();
    $(window).resize(function(){
        viewport = getVieport();
    });
});
})(jQuery);




/* formSubmit
----------------------------------------------- */
(function($){
$.fn.formSubmit = function(o){
    o = $.extend({
        submitURL: '',
        capchaURL: '',
        type: 'post',
        speed: 'normal',
        timeout: 3000
    }, o || {});

    return this.each(function(){
        var $form = $(this),
            $fields = $('.fields', $form),
            $overlay = $('.overlay', $form),
            $inputArray = $('input, textarea, select, button', $form),
            $textareaArray = $('textarea', $form),
            $submit = $('.submit .button', $form),
            $loading = $('.loading', $form),
            $status = $('.status', $form),
            submitURL = o.submitURL || $form.attr('action'),
            capchaURL = o.capchaURL || $('.capcha-url', $form).val(),
            $capcha = $('.capcha', $form),
            $capchaAnswer = $('.capcha-answer', $form),
            $capchaID = $('.capcha-id', $form),
            $capchaReload = $('.capcha-reload', $form),
            t;

        $capcha.data('capcha-answer', $capchaAnswer.val());

        $capchaReload.click(function(){
            capchaReload();
        });

        $.validator.addMethod('capcha', function(value, element) { 
            return $.trim(value.toLowerCase()) === $(element).data('capcha-answer');
        });

        $form.validate({
            errorPlacement: function(error, el) {
            },
            highlight: function(el, errorClass) {
                $(el).closest('.field').addClass('field-error');
            },
            unhighlight: function(el, errorClass) {
                $(el).closest('.field').removeClass('field-error');
            },
            submitHandler: function() {
                $submit.fadeOut(o.speed);
                $overlay.height($fields.height()).fadeIn(o.speed);
                $loading.fadeIn(o.speed, function(){
                    $form.ajaxSubmit({
                        url: submitURL,
                        type: o.type,
                        resetForm: true,
                        beforeSubmit: function(){
                            $inputArray.blur();
                        },
                        success: function(response, status, xhr, $form){
                            capchaReload();

                            $textareaArray.each(function(){
                                $(this).text($(this).data('placeholder'));
                            });

                            $loading.fadeOut(o.speed, function(){
                                $status.fadeIn(o.speed);
                                t = setTimeout(
                                    function(){
                                        $overlay.fadeOut(o.speed, function(){
                                            $status.hide();
                                        });
                                        $submit.fadeIn(o.speed);
                                    },
                                    o.timeout
                                );
                            });
                        }
                    });
                });
             }
        });

        function capchaReload() {
            $.get(capchaURL, {id: $capchaID.val()}, function(response){
                $capcha.data('capcha-answer', response.answer).data('placeholder', response.question).attr('title', response.question).val(response.question);
                $capchaID.val(response.id);
                $capchaAnswer.val(response.answer);
            }, 'json');
        }
    });
};
})(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() == $input.data('placeholder')) {
            if (isTextarea) $input.text($input.data('placeholder'));
            $input.val($input.data('placeholder')).addClass(className);
        }

        $input.focus(function(){
            if ($input.val() == $input.data('placeholder')) {
                if (isTextarea) $input.text('');
                $input.val('').removeClass(className);
            }
        }).blur(function(){
            if ($input.val() == '') {
                if (isTextarea) $input.text($input.data('placeholder'));
                $input.val($input.data('placeholder')).addClass(className);
            }
        });
    });
};
})(jQuery);



/* imageFlow
----------------------------------------------- */
(function($){
var o,
    viewPort,
    $wrap, $store,
/*     $close, */
    $navPrev, $navNext,
    $title,
    $currentImage, $prevImage, $nextImage,
    currentImage = new Image(), prevImage = new Image(), nextImage = new Image(),
    selectedArray = [], selectedIndex = 0,
    currentArray = [], currentIndex = 0,
    prevIndex = -1, nextIndex = -1,
    busy = false,


/* private methods */
		imageFlowAbort = function(){
        $store.empty();
        currentImage.onload = null;
        prevImage.onload = null;
        nextImage.onload = null;
		},


    imageFlowGetViewPort = function(){
        var doc = document.documentElement,
            viewport = {},
            width = 0,
            height = 0,
            docWidth = 0,
            docHeight = 0,
            scrollLeft = 0,
            scrollTop = 0;

        width = doc.clientWidth;
        height = doc.clientHeight;
        
        docWidth = doc.scrollWidth;
        docHeight = doc.scrollHeight;

        if (typeof(window.pageXOffset) == 'number') {
            scrollLeft = window.pageXOffset;
            scrollTop = window.pageYOffset;
        } else {
            scrollLeft = doc.scrollLeft;
            scrollTop = doc.scrollTop;
        }

        viewPort = {
            width: width,
            height: height,
            docWidth: docWidth,
            docHeight: docHeight,
            scrollLeft: scrollLeft,
            scrollTop: scrollTop
        };

        return viewPort;
    },


    imageFlowProcessImage = function(image, index){
        var $img = $('<img />').attr({
            'id'	: 'imageFlowIMG' + index,
            'src'	: image.src
        }).css({
            'border-width': o.borderWidth,
            'border-style': o.borderStyle,
            'border-color': o.borderColor
        }).data('size', [image.width, image.height]).data('title', currentArray[index].title);
        $store.append($img);
        return $img;
    },


    imageFlowInit = function(){
        if ($('#imageFlowWrap').length) {
            return;
        }

        $('body').append(
            $overlay = $('<div id="imageFlowOverlay"></div>'),
            $wrap = $('<div id="imageFlowWrap"></div>').append(
                $navPrev = $('<div id="imageFlowPrev"><div class="png"></div></div>'),
                $navNext = $('<div id="imageFlowNext"><div class="png"></div></div>'),
/*                 $close = $('<div id="imageFlowClose"></div>'), */
                $title = $('<div id="imageFlowTitle"></div>'),
                $store = $('<div id="imageFlowStore" />')
            )
        );

        $wrap.click(function(){
            $wrap.blur();
            imageFlowHide();
        }).dblclick(function(){
            $wrap.blur();
        });

/*
        $close.click(function(e){
            e.stopPropagation();
            imageFlowHide();
        });
*/

        $title.click(function(e){
            e.stopPropagation();
        });


        $navPrev.click(function(e) {
            e.stopPropagation();
            imageFlowPrev();
        });

        $navNext.click(function(e) {
            e.stopPropagation();
            imageFlowNext();
        });
    },


    imageFlowStart = function(){
        imageFlowAbort();

        currentArray = selectedArray;
        currentIndex = selectedIndex;
        prevIndex = currentIndex > 0 ? currentIndex - 1 : -1;
        nextIndex = currentArray.length > currentIndex + 1 ? currentIndex + 1 : -1;

        if (currentArray.length) {
            imageFlowShow();
        }
    },


    imageFlowShow = function(){
        viewPort = imageFlowGetViewPort();

        $overlay.css({
            'width': ( viewPort.width < viewPort.docWidth ? viewPort.docWidth : '100%'),
            'top': viewPort.scrollTop,
            'opacity': 0
        });
        $overlay.show().fadeTo(o.fadeInSpeed, o.overlayOpacity);

        $wrap.css({
            'width': ( viewPort.width < viewPort.docWidth ? viewPort.docWidth : '100%'),
            'top': viewPort.scrollTop
        });
        $wrap.fadeIn(o.fadeInSpeed);

/*         $close.fadeIn(o.fadeInSpeed); */
        
        if (prevIndex > -1) {
            $navPrev.fadeIn(o.fadeInSpeed);
        }

        if (nextIndex > -1) {
            $navNext.fadeIn(o.fadeInSpeed);
        }

        currentImage = new Image();
        currentImage.onload = function(){
            currentImage.onload = null;
            $currentImage = imageFlowProcessImage(currentImage, currentIndex);
            $currentImage
                .imageFlowScale()
                .css({
                    'left': viewPort.docWidth / 2 - $currentImage.data('resize')[0] / 2 - o.borderWidth,
                    'top': viewPort.height / 2 - $currentImage.data('resize')[1] / 2 - o.borderWidth
                })
                .fadeIn(o.fadeInSpeed, function(){
                    $title.html($currentImage.data('title')).css({
                        'width': $currentImage.width() + ( o.borderWidth * 2 ),
                        'left': $currentImage.position().left,
                        'top': $currentImage.position().top + $currentImage.height() + ( o.borderWidth * 2 )
                    }).fadeIn(o.fadeInSpeed);
                });

            if (prevIndex > -1) {
                prevImage = new Image();
                prevImage.onload = function(){
                    prevImage.onload = null;
                    $prevImage = imageFlowProcessImage(prevImage, prevIndex);
                    $prevImage
                        .imageFlowScale()
                        .css({
                            'left': -$prevImage.data('resize')[0] + o.sides,
                            'top': viewPort.height / 2 - $prevImage.data('resize')[1] / 2 - o.borderWidth
                        })
                        .fadeIn(o.fadeInSpeed);
                };
                prevImage.src = currentArray[prevIndex].href;
            }

            if (nextIndex > -1) {
                nextImage = new Image();
                nextImage.onload = function(){
                    nextImage.onload = null;
                    $nextImage = imageFlowProcessImage(nextImage, nextIndex);
                    $nextImage
                        .imageFlowScale()
                        .css({
                            'left': viewPort.docWidth - o.sides,
                            'top': viewPort.height / 2 - $nextImage.data('resize')[1] / 2 - o.borderWidth
                        })
                        .fadeIn(o.fadeInSpeed);
                };
                nextImage.src = currentArray[nextIndex].href;
            }
        };
        currentImage.src = currentArray[currentIndex].href;

        $(window).resize(function(){
            viewPort = imageFlowGetViewPort();


            $overlay.css({
                'width': ( viewPort.width < viewPort.docWidth ? viewPort.docWidth : '100%'),
                'top': viewPort.scrollTop
            });
            $wrap.css({
                'width': ( viewPort.width < viewPort.docWidth ? viewPort.docWidth : '100%'),
                'top': viewPort.scrollTop
            });

            $currentImage
                .imageFlowScale()
                .css({
                    'left': viewPort.docWidth / 2 - $currentImage.data('resize')[0] / 2 - o.borderWidth,
                    'top': viewPort.height / 2 - $currentImage.data('resize')[1] / 2 - o.borderWidth
                });

            $title.css({
                'width': $currentImage.width() + ( o.borderWidth * 2 ),
                'left': $currentImage.position().left,
                'top': $currentImage.position().top + $currentImage.height() + ( o.borderWidth * 2 )
            });

            if (prevIndex > -1) {
                $prevImage
                    .imageFlowScale()
                    .css({
                        'left': -$prevImage.data('resize')[0] + o.sides,
                        'top': viewPort.height / 2 - $prevImage.data('resize')[1] / 2 - o.borderWidth
                    });
            }

            if (nextIndex > -1) {
                $nextImage
                    .imageFlowScale()
                    .css({
                        'left': viewPort.docWidth - o.sides,
                        'top': viewPort.height / 2 - $nextImage.data('resize')[1] / 2 - o.borderWidth
                    });
            }
        }).scroll(function(){
            viewPort = imageFlowGetViewPort();
            $overlay.css({'top': viewPort.scrollTop});
            $wrap.css({'top': viewPort.scrollTop});
        });

    },


    imageFlowHide = function(){
        $overlay.fadeOut(o.fadeOutSpeed, function(){
            imageFlowAbort();
        });
        $wrap.fadeOut(o.fadeOutSpeed);
        $navPrev.fadeOut(o.fadeOutSpeed);
        $navNext.fadeOut(o.fadeOutSpeed);
/*         $close.fadeOut(o.fadeOutSpeed); */
    },


    imageFlowPrev = function(){
        if (!busy && prevIndex > -1) {
            busy = true;

            $title.hide();

            $navNext.fadeIn(o.fadeInSpeed);

            if (nextIndex > -1) {
                $nextImage.fadeOut(o.fadeOutSpeed);
            }

            $currentImage.animate({
                'left': viewPort.docWidth - o.sides
            }, {queue: false, duration: o.moveSpeed, easing: 'easeOutExpo'});

            currentIndex--;
            prevIndex = currentIndex > 0 ? currentIndex - 1 : -1;
            nextIndex = currentArray.length > currentIndex + 1 ? currentIndex + 1 : -1;

            if (prevIndex > -1) {
                $navPrev.fadeIn(o.fadeInSpeed);
            } else {
                $navPrev.fadeOut(o.fadeOutSpeed);
            }

            $prevImage.animate({
                'left': viewPort.docWidth / 2 - $prevImage.data('resize')[0] / 2 - o.borderWidth
            }, {queue: false, duration: o.moveSpeed, easing: 'easeOutExpo',
                complete: function(){
                    $nextImage = $currentImage;
                    $currentImage = $prevImage;

                    $title.html($currentImage.data('title')).css({
                        'width': $currentImage.width() + ( o.borderWidth * 2 ),
                        'left': $currentImage.position().left,
                        'top': $currentImage.position().top + $currentImage.height() + ( o.borderWidth * 2 )
                    }).fadeIn(o.fadeInSpeed);

                    if (prevIndex > -1) {
                        prevImage = new Image();
                        prevImage.onload = function(){
                            prevImage.onload = null;
                            $prevImage = imageFlowProcessImage(prevImage, prevIndex);
                            $prevImage
                                .imageFlowScale()
                                .css({
                                    'left': -$prevImage.data('resize')[0] + o.sides,
                                    'top': viewPort.height / 2 - $prevImage.data('resize')[1] / 2 - o.borderWidth
                                })
                                .fadeIn(o.fadeInSpeed, function(){
                                    busy = false;
                                });
                        }
                        prevImage.src = currentArray[prevIndex].href;
                    } else {
                        busy = false;
                    }
                }
            });
        }
    },


    imageFlowNext = function() {
        if (!busy && nextIndex > -1) {
            busy = true;

            $title.hide();

            $navPrev.fadeIn(o.fadeInSpeed);

            if (prevIndex > -1) {
                $prevImage.fadeOut(o.fadeOutSpeed);
            }

            $currentImage.animate({
                'left': -$currentImage.data('resize')[0] - o.borderWidth * 2 + o.sides
            }, {queue: false, duration: o.moveSpeed, easing: 'easeOutExpo'});

            currentIndex++;
            prevIndex = currentIndex > 0 ? currentIndex - 1 : -1;
            nextIndex = currentArray.length > currentIndex + 1 ? currentIndex + 1 : -1;

            if (nextIndex > -1) {
                $navNext.fadeIn(o.fadeInSpeed);
            } else {
                $navNext.fadeOut(o.fadeOutSpeed);
            }

            $nextImage.animate({
                'left': viewPort.docWidth / 2 - $nextImage.data('resize')[0] / 2 - o.borderWidth
            }, {queue: false, duration: o.moveSpeed, easing: 'easeOutExpo',
                complete: function(){
                    $prevImage = $currentImage;
                    $currentImage = $nextImage;

                    $title.html($currentImage.data('title')).css({
                        'width': $currentImage.width() + ( o.borderWidth * 2 ),
                        'left': $currentImage.position().left,
                        'top': $currentImage.position().top + $currentImage.height() + ( o.borderWidth * 2 )
                    }).fadeIn(o.fadeInSpeed);

                    if (nextIndex > -1) {
                        nextImage = new Image();
                        nextImage.onload = function(){
                            nextImage.onload = null;
                            $nextImage = imageFlowProcessImage(nextImage, nextIndex);;
                            $nextImage
                                .imageFlowScale()
                                .css({
                                    'left': viewPort.docWidth - o.sides,
                                    'top': viewPort.height / 2 - $nextImage.data('resize')[1] / 2 - o.borderWidth
                                })
                                .fadeIn(o.fadeInSpeed, function(){
                                    busy = false;
                                });
                        }
                        nextImage.src = currentArray[nextIndex].href;
                    } else {
                        busy = false;
                    }
                }
            });
        }
    };


/* public methods */
    $.fn.imageFlow = function(options){
        o = $.extend({}, $.fn.imageFlow.defaults, options);

        imageFlowInit();

        this.unbind('click.if').bind('click.if', function(e){
            e.preventDefault();

            $(this).blur();

            selectedArray	= [];
            selectedIndex	= 0;

            var rel = $(this).attr('rel') || '';

            if (!rel || rel == '' || rel === 'nofollow') {
                selectedArray.push(this);
            } else {
                selectedArray	= $('a[rel=' + rel + '], area[rel=' + rel + ']');
                selectedIndex	= selectedArray.index(this);
            }

            imageFlowStart();
        });

    		return this;
    };


    $.fn.imageFlowScale = function(){
        var $img = $(this),
            size = $img.data('size'),
            width = size[0],
            height = size[1],
            resize = [width, height],
            scale = 1,
            hScale = (viewPort.docWidth - o.hPadding * 2 - o.borderWidth) / width,
            vScale = (viewPort.height - o.vPadding * 2 - o.borderWidth) / height;



        if (hScale < 1 && hScale <= vScale) {
            scale = hScale;
        } else if (vScale < 1 && vScale <= hScale) {
            scale = vScale;
        }

        resize[0] = width * scale;
        resize[1] = height * scale;

        $img.css({
            'width': resize[0],
            'height': resize[1]
        }).data('resize', resize);

        return $img;
    };


    $.fn.imageFlow.defaults = {
        'onLoad': false,
        'moveSpeed': 500,
        'fadeInSpeed': 300,
        'fadeOutSpeed': 300,
        'overlayOpacity': 0.8,
        'hPadding': 100,
        'vPadding': 100,
        'sides': 50,
        'borderWidth': 10,
        'borderStyle': 'solid',
        'borderColor': '#fff'
    };

})(jQuery);



/* hoverable
----------------------------------------------- */
(function($){
$.fn.hoverable = function(hoverClass){
    hoverClass = hoverClass || 'hover';

    return this.each(function(){
        var $el = $(this);

        $el.hover(
            function(){
                $el.addClass(hoverClass).refreshPNG();
            },
            function(){
                $el.removeClass(hoverClass).refreshPNG();
            }
        );
    });
};
})(jQuery);



/* fixPNG
----------------------------------------------- */
function fixPNG() {
    if (typeof DD_belatedPNG == 'object') {
        var hasPng = '.png';
        hasPng += ', ul li';
        hasPng += ', .logo img';
        hasPng += ', .footnote-logo img';
        hasPng += ', .icon i, .ico i, .icol i, .icor i';
        hasPng += ', .teasers li';
        hasPng += ', .tabs i, .tabs em';
        hasPng += ', .offers .corner, .offers .list1 i, .offers .list2';
        hasPng += ', .bn i, .bn img';
        hasPng += ', .site-links img';
        hasPng += ', .footnote .google-maps a';
        hasPng += ', #footerGlow, #footerRose';
        hasPng += ', .contacts-location .google-maps a';
        hasPng += ', .image-frame i';
        DD_belatedPNG.fix(hasPng);
    }
}



/* refreshPNG
----------------------------------------------- */
(function($){
$.fn.refreshPNG = function(){
    if (typeof(DD_belatedPNG) == 'object') {
        this.find('*').andSelf().each(function(){
            this.fireEvent('onblur');
        });
    }    
    return this;
};
})(jQuery);

