

var siteActions = {};
siteActions.jq= jQuery.noConflict();

(function($) {
    siteActions.jq(document).ready(function() {


    var mt = $('meta[name=SKYPE_TOOLBAR]');
    mt = mt.length ? mt : $('<meta name="SKYPE_TOOLBAR" />').appendTo('head');
    mt.attr('content', 'SKYPE_TOOLBAR_PARSER_COMPATIBLE');


    $('.redirectThis').live('click', function(event) { redirectThis(event, $(this) ) });
    $('.contactsForm, .showMapBlock').live('submit', function(event){sendFormData(event, $(this))});

    $('.reservationPage').live('click', function(event) { showReservationPage(event ) });
    $('.couponPage').live('click', function(event) { showCouponPage(event) });

    $('a.gallery').colorbox({rel:'gallery'});

    $('.partnersContainer').live('mousemove', function(event){moveSlider(event,$(this))});


        function moveSlider(event, obj){

            var width = 0;

            obj.children().children('a').each(function() {
                width += $(this).outerWidth( true ) + parseInt($(this).css('marginRight'));
            });


            var left = (event.pageX - obj.offset().left) * (width-obj.width()) / obj.width();


            obj.scrollLeft(left);

    }

    var blockCategoryParams = {};

    $('.leftArrowBtn').live('click', function() {
        var sliderObj  = $(this).parent().parent().find('.titleSlider');
        topCategorySliderPhotoToSelectPrevious(sliderObj);
    });

    $('.rightArrowBtn').live('click', function() {
        var sliderObj  = $(this).parent().parent().find('.titleSlider');
        topCategorySliderPhotoToSelectNext(sliderObj);
    });

    $('.specialOfferSlideButtons span').live('click', function() {
        if( !$(this).hasClass('active') ){
            var selectBlock  = $(this).parent();
            var sliderObj  = selectBlock.parent().find('ul');
            var photo = $(this).index()+1;
            
            topCategorySliderPhotoSelect(sliderObj, photo);
        }
    });


    defaultActions();


    function onBlur() {
        document.body.className = 'blurred';
    }

    function onFocus(){
        document.body.className = 'focused';
    }





    function defaultActions(){
        
        siteSliders();
        setForms();

        if (/*@cc_on!@*/false) { // check for Internet Explorer
            document.onfocusin = onFocus;
            document.onfocusout = onBlur;
        } else {
            window.onfocus = onFocus;
            window.onblur = onBlur;
        }

        //$('.shareBoxItemFB').html(fbHTML);
    }


    function setForms(){
        setContactsForm();
    }

    function showReservationPage( event){

        event.preventDefault();
        showSplash(' ', '<iframe class="reservationIframe" src="'+reservationPage+'"></iframe>');


    }

    function showCouponPage( event){

        event.preventDefault();
        showSplash(' ', '<iframe class="couponIframe" src="'+couponPage+'"></iframe>');


    }


    function setContactsForm(){

        if( $('.contactsForm').length!=0 ){
           
            $('.contactsForm').validate({
                
                invalidHandler:function(e, validator) {

                },
                showErrors: function(){
                    if (this.settings.highlight) {
                        for (var i = 0; this.errorList[i]; ++i) {
                            this.settings.highlight.call(this, this.errorList[i].element,
                                this.settings.errorClass, this.settings.validClass);
                        }
                    }
                    if (this.settings.unhighlight) {
                        for (var i = 0, elements = this.validElements(); elements[i]; ++i) {
                            this.settings.unhighlight.call(this, elements[i],
                                this.settings.errorClass, this.settings.validClass);
                        }
                    }
                },
                onkeyup: false,
                onfocusin: false,
                onfocusout: false,
                onclick: false,
                rules: {
                        name: {
                                required:true,
                                minlength:2
                        },
                        surname: {
                                required:true,
                                minlength:2
                        },
                        email: {
                                required: true,
                                email: true
                        },
                        phone: {
                                required: true

                        },
                        message: {
                                required: true

                        }                       

                }
            });
        }

    }

    function siteSliders(){

        var total = $('.titleSlider img').length;
        var rand = Math.floor(Math.random()*total); 
        $('.titleBannerAreaInner').nivoSlider({
            pauseTime: 5000, // default: 3000. How long each slide will show
            startSlide: rand, // default: 0. Set starting Slide (0 index)
            directionNav: true, // default: true. Next & Prev navigation
            controlNav: false,
            directionNavHide: false, // default: true. Only show on hover
            keyboardNav: false, // default: true. Use left & right arrows
            effect:'sliceUpDownLeft',
            prevText: '',
            nextText: ''
        });

        var sliderListObjs = $('.specialOffersList, .bottomBlockContentList');

        if( sliderListObjs.length!=0 ){

            sliderListObjs.each(

                function(){
                    if( $(this).length != 0 && $(this).children('li').size()>1 ){
                        blockCategoryParams[$(this).attr('class')+'_processed'] = false;
                        topCategorySliderWork($(this));
                    }
                }

            )
        }
    }

    function sendFormData(event, form){


        event.preventDefault();
        var formData = form.serialize();
        
         $.ajax({
            url:ajaxURI+'?lang='+siteLANG+'&'+new Date().getTime(),//prevent cache,
            type:"POST",
            async: false,
            data:formData,
            datatype:"json",
            success: function(response){
                var data = $.parseJSON(response);
                if(data.title && data.text){
                    showSplash(data.title, data.text);
                    if( !data.no_reset ){
                        form[0].reset();
                    }

                    if(  !data.error && data.remove_on_success ){
                        form.parent().remove();
                    }
                }else{
                    if(data.link){
                       window.location=data.link;
                       $.die();
                    }

                }
            }
        });

    }

    function topCategorySliderWork(sliderObj){

        blockCategoryParams[sliderObj.attr('class')] = setInterval(
        function() {
            var bClass = document.body.className;
            
            if( bClass!='blurred'){
                topCategorySliderPhotoToSelectNext(sliderObj);
            }
        }, 4000);

    }


    function topCategorySliderPhotoToSelectNext(sliderObj){

        var  categorySliderImg = sliderObj.find('li.activeSliderCategoryItem').index()+1;
        var photo=parseInt(categorySliderImg)+1>parseInt(sliderObj.find('li').size())?1:parseInt(categorySliderImg)+1;
        topCategorySliderPhotoSelect(sliderObj, photo);
    }

    function topCategorySliderPhotoToSelectPrevious(sliderObj){
        var  categorySliderImg = sliderObj.find('li.activeSliderCategoryItem').index()+1;
        var photo=parseInt(categorySliderImg)-1<parseInt(sliderObj.find('li').size()) && parseInt(parseInt(categorySliderImg)-1)>0?parseInt(categorySliderImg)-1:sliderObj.find('li').size();
        topCategorySliderPhotoSelect(sliderObj, photo);
    }

    function topCategorySliderPhotoSelect( sliderObj , photo )
    {
          
            topCategorySliderStop(sliderObj.attr('class'));
            topCategorySliderWalk(sliderObj, photo);            
            topCategorySliderWork(sliderObj);
    }

    function topCategorySliderWalk(sliderObj, photo){


        if( !blockCategoryParams[sliderObj.attr('class')+'_processed'] ){
            
            blockCategoryParams[sliderObj.attr('class')+'_processed'] = true;
            var photoIndex = photo-1;
            var loopIndex = 0;
             sliderObj.children('li').each(function() {
                 if( $(this).hasClass('activeSliderCategoryItem')){

                    var  currentSliderObj = $(this);
                    var  nextSliderObj = sliderObj.children('li:eq(' +photoIndex+ ')');

                    currentSliderObj.removeClass('activeSliderCategoryItem');


                    if( ( loopIndex>photoIndex && !( photoIndex==0 && loopIndex==(sliderObj.children('li').size()-1) ) )
                            || ( loopIndex==0 && (sliderObj.children('li').size()-1)==photoIndex ) ){

                        $(this).stop(true,true).animate( {left:$(this).width()+'px'},1200); // from left

                    }else{

                        $(this).stop(true,true).animate( {left:'-'+$(this).width()+'px'},1200); // from right

                    }

                  

                    nextSliderObj.addClass('activeSliderCategoryItem');


                    if( (loopIndex>photoIndex  && !( photoIndex==0 && loopIndex==(sliderObj.children('li').size()-1) ) )
                        || ( loopIndex==0 && (sliderObj.children('li').size()-1)==photoIndex ) ){

                        sliderObj.children('li:eq(' +photoIndex+ ')').css('left', '-' + sliderObj.children('li:eq(' +photoIndex+ ')').width()+'px');// from right

                    }else{

                        sliderObj.children('li:eq(' +photoIndex+ ')').css('left', sliderObj.children('li:eq(' +photoIndex+ ')').width()+'px');// from left

                    }

                    sliderObj.children('li:eq(' +photoIndex+ ')').stop(true,true).animate( {left:0},1200);






                    sliderObj.children('li:eq(' +photoIndex+ ')').css('zIndex',3);
                    $(this).css('zIndex',2);

                    return false;

                 }
                 loopIndex++;
              });

            sliderObj.children('li:eq(' +photoIndex+ ')').css('display','block');

            //if(sliderObj.hasClass('specialOffersList')){ //additional actions
                setSliderActiveOption(sliderObj, photoIndex);
            //}

            setTimeout( function(){  blockCategoryParams[sliderObj.attr('class')+'_processed'] = false; }, 1300);

        }


    }


    function topCategorySliderStop(sliderObjClass){
        if( blockCategoryParams[sliderObjClass] ){
            clearInterval(blockCategoryParams[sliderObjClass]);
        }
    }

    function setSliderActiveOption(sliderObj, photoIndex){
        var sliderOptionsBlock  = sliderObj.parent().parent().find('.sliderOptions');
        var currentOption = sliderOptionsBlock.find('.active');
        var activeOption = sliderOptionsBlock.children('span:eq('+photoIndex+')');
        currentOption.removeClass('active');
        activeOption.addClass('active');
    }







    function redirectThis( event, obj){

        event.preventDefault();

        var link = obj.find('input[name="link"]');

        if( link.length != 0 && $.trim(link.val()) ){

            window.location  = link.val();
            $.die();
        }


    }



        /* SPLASH / MAP SCREEN BEGIN*/

        function showSplash( title, text){

            var textContainer = $(".splashScreen").find(".splashScreenText");

            textContainer.children('h2').html(title);
            textContainer.children('div').html(text);
            $(".splashScreen").attr('style','');
            $(".splashScreen").overlay({
                top: 0,
                mask: {
                    color: '#000000',
                    loadSpeed: 1000,
                    opacity: 0.7,
                    zIndex: 9998
                },
                closeOnClick: true,
                effect: 'drop',
                load: true
            }).load();

        }




        $.easing.drop = function (x, t, b, c, d) {
            return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
        };

        $.tools.overlay.addEffect("drop", function(css, done) {

            var conf = this.getConf(),
            overlay = this.getOverlay();
            if (conf.fixed)  {
                css.position = 'fixed';
            } else {
                css.top += $(window).scrollTop();
                css.left += $(window).scrollLeft();
                css.position = 'absolute';
            }

            overlay.css(css).show();

            overlay.animate({
                top: '+=70',
                opacity: 1,
                width: '+=10'
            }, 800, 'drop', done);

        }, function(done) {
            this.getOverlay().animate({
                top:'-=70',
                opacity:0,
                width:'-=10'
            },100, 'drop', function() {
                $(this).hide();
                done.call();
            });
        }
        );

    /* SPLASH SCREEN END*/




    function isStrJSON(str) {


         if ($.trim(str) == '') return false;
         str = str.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
         return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
    }

    });


})(jQuery);
