/**
 * MAIN MENU
 */
$(document).ready(function(){
    var mItem = $('ul#mainMenu li');
    //Check for submenus
    $(mItem).each(function(i, el){
        if($(el).children('div.submenu').hasClass('submenu')) $(el).children('a').first().addClass('drop');
    });

	
	//Add Hover event
	$(mItem).hover(function(){
		$(this).addClass('on');
		$(this).next('li').addClass('noBorder');
		$(this).children('div.submenu')
			   .delay(200)
			   .slideDown(500, "easeOutBack");
	},
	function(){
		$(this).removeClass('on');
		$(this).next('li').removeClass('noBorder');
		$(this).children('div.submenu')
			   .stop(true)
			   .slideUp(0);
	});
	
});


/**
 * SUBMENU 1
 */
$(document).ready(function(){
    var sm1 = $('.submenu1');

    if($(sm1)){
        $(sm1).each(function(i, el){
            $(el).children('div').each(function(i2, el2){
                if(!$(el2).hasClass('clear')) $(el2).css('height', $(el).height()+'px');
            });
        });
    }
});

/**
 * SUBMENU 3
 */
$(document).ready(function(){
    var sm3a = $('.submenu3 .inner a');
    var sm3ul = $('.submenu3 .gallery ul');
    var sm3li = $(sm3ul).children('li');
    var lastActive = $(sm3a).first();
    if($(sm3a)){
        $(sm3a).each(function(i, el){
            $(el).click(function(e){
                e.preventDefault();
                $(el).addClass('active');
                $(lastActive).removeClass('active');
                lastActive = el;
                $(sm3ul).animate({top: -i * sm3li.first().height()+'px'}, {easing: "easeInOutExpo", duration:500});
                sm3li.eq(i).children('a').each(function(i2, a){
                    $(a).children('img').first().attr('src', $(a).attr('rel'));
                });
            });
        });
    }
});

/**
 * HORIZONTAL ACCORDION
 */
$(document).ready(function(){
    var accordion = $('#accordion').children('div.frame').first();

    divs = $(accordion).children('div');
    l = $(divs).length-1;
    lastBlock = $(divs).first();

    minWidth = 49;
    maxWidth = 960 - l*(minWidth+1) - 1;

     $(lastBlock).css('width', maxWidth).addClass('active');

    $(divs).click(function(){
        if($(lastBlock).attr('id') !=  $(this).attr('id')){
            $(lastBlock).animate({width: minWidth+"px"}, {easing: "easeOutSine", duration:500}).removeClass('active');
            $(this).animate({width: maxWidth+"px"}, {easing: "easeOutSine", duration:450}).addClass('active');
            lastBlock = this;
        }
    });
});

/**
 * MAILING SIGN UP
 */
$(document).ready(function(){
    mailSignUp($('.newsletter-signup'));
});

function mailSignUp(el){
    $(el).submit(function(e){
        e.preventDefault();
        //setup variables
        var form = $(this),
        formData = form.serialize(),
        formUrl = form.attr('action'),
        formMethod = form.attr('method'),
        responseMsg = $(form).children('.signup-response').first();

        //show response message - waiting
        responseMsg.hide()
                   .text('Please Wait...')
                   .fadeIn(200);

        //send data to server for validation
        $.ajax({
            url: formUrl,
            type: formMethod,
            data: formData,
            success:function(data){
                //setup variables
                var responseData = jQuery.parseJSON(data);
                //show reponse message
                responseMsg.fadeOut(200,function(){
                    $(this).text(responseData.msg)
                           .fadeIn(200);
                 });
              }
        });

        //prevent form from submitting
        return false;
    });
}

/**
 * PLACEHOLDERS
 */
$(document).ready(function(){
    $('input').each(function(i, el){
        if($(el).attr('placeholder')){
            $(el).attr('value', $(el).attr('placeholder'))
                .click(function(){
                 if($(this).attr('value') == $(this).attr('placeholder')) $(this).attr('value', '');
             });
        }
    });
});

/**
 * 1. NADSTROPJE
 */
$(document).ready(function(){
    var fFloor = $('#fFloor');
    var open = 'down';

    $('#up').click(function(){
        $(fFloor).slideToggle(800);
        if(open == 'down'){
            $(this).children('div').children('img.down').css('display', 'none');
            $(this).children('div').children('img.up').css('display', 'block');
			$('a#firstpagesejem').trigger('click');
            open = 'up';
			
        }else{
            $(this).children('div').children('img.down').css('display', 'block');
            $(this).children('div').children('img.up').css('display', 'none');
            open = 'down';
        }
    });

    //GLOWING ARROW
    var glow =  $('.glow');
    setInterval(function(){
       $(glow).each(function(i, el){
           if($(el).hasClass(open)){
               $(el).fadeIn(500, function(){
                    $(this).fadeOut(500);
               });
           }
       });
    }, 1000);

    //AJAX LOADER
    var ff = $('#fFloor .inner');
    $('#topmenu a').click(function(e){
        e.preventDefault();
        ajaxLoader($(this));
    });
    $('a.ajax').click(function(e){
        e.preventDefault();
        ajaxLoader($(this));
    });

    function ajaxLoader(el){
        $.get($(el).attr('href'), function(data){
            //Remove And Append
            //alert($('#fFloor .inner .inner2').length)
            $('#fFloor .inner .inner2').remove();
            $('#fFloor .inner').append(data);
            //Recheck elements
            loadGallery($('#ffGallery'));
            $('.chBoxSet').buttonset();
            sendPrizeForm($('#prizeContest'));
            liveFair($('#player'));
            $('a.ajax').click(function(e){
                e.preventDefault();
                ajaxLoader($(this));
            });
            mailSignUp($('.newsletter-signup'));
            
        });
    }
    /**
     * IZDELKI NA SEJMU
     */
    var ffGallery, frame, pages, page;

    function loadGallery(el){
        frame = $(el).children('.frame').first();
        pages = $(frame).children('a').length-3;
        $(frame).children('a').children('span').fadeOut(0);
        page = 0;
        //$(ffGallery).prevAll('.left').hide();

        $(el).prevAll('.arrow').click(function(){
            if($(this).hasClass('right')) moveGallery(1);
            if($(this).hasClass('left')) moveGallery(-1);
        });

        $(frame).children('a').hover(function(){
            $(this).children('span').fadeIn(200);
        }, function(){
            $(this).children('span').fadeOut(200);
        });
    }

    function moveGallery(step){
        page += step;

        if(page < 0) page = pages;
        if(page > pages) page = 0;

        $(frame).animate({left: page * -249 + 'px'}, {duration:500});

        $(ffGallery).prevAll('.left').fadeIn(200);
        $(ffGallery).prevAll('.right').fadeIn(200);
        if(page == 0) $(ffGallery).prevAll('.left').fadeOut(200);
        if(page == pages) $(ffGallery).prevAll('.right').fadeOut(200);

        if(page == 1){
            $(frame).children('a').children('img').each(function(i, el){
                if($(el).attr('rel') && $(el).attr('src') != $(el).attr('rel')) $(el).attr('src', $(el).attr('rel'));
            });
        }
    }

    //LIVE FROM FAIR
    function liveFair(el){
        var img = $(el).children('img');
        var last = $(img).first();
        $(last).fadeIn(200);
        
        setInterval(function(){
            $(last).fadeOut(400, function(){
                last = $(this).next('img');
                if(!$(last).length) last = $(img).first();
                $(last).fadeIn(400);
            });
        }, 3000);
    }

});

/**
 * POSAMEZNI PRIKAZ
 */
$(document).ready(function(){
    var ptInner = $('#ptInner');
    var ptImg = $('#ptImg');
    var ptFrame = $('#ptFrame');
    var ptFrameY = ptImgY = tmpH = 350;//$(ptFrame).height();
    var ptNote = $(ptFrame).children('.note');
    var ptArrows = $('#ptInner .arrow');
    var aImg = $('a.img');
    var slide = true;
    var way = 'down';
    var enlarge = false;
    var opened = false;

    $(ptImg).load(function(){
        checkSize();
    })

    $(ptFrame).click(function(){
        h = $(ptImg).height();
        if(tmpH == ptFrameY){
            resizeFrame(h, 0);
            //$(this).stop(true).css('top', '0px');
            tmpH = h;
        }else{
            if(h > ptFrameY) resizeFrame(ptFrameY, Math.round(-(ptImgY-ptFrameY)/2));
            //slideImg();
            tmpH = ptFrameY;
        }
    }).hover(function(){
        //if(enlarge) $(ptNote).fadeIn(200);
    }, function(){
        //$(ptNote).fadeOut(200);
    }).mousemove(function(e){
        /*
        offset = $(ptInner).offset();
        $(ptNote).css({'top': e.pageY - offset.top + 'px', 'left': e.pageX - offset.left +5 + 'px'});
        */
    });

    //change image
    $(aImg).click(function(e){
        e.preventDefault();
        changeImg($(this).attr('href'));
    });

    function changeImg(src){
        if($(ptImg).attr('src') == src) return false;
        $(ptImg).stop(true).fadeOut(450, function(){
                $(this).attr('src', src)
                       .css('top', '0px')
                       .load(function(){
                            $(this).fadeIn(450);
                        });
            });
            return true;
    }

    function checkSize(){
        x = $(ptImg).width();
        y = ptImgY = $(ptImg).height();
        enlarge = false;
        $(ptImg).removeClass('ch');
		$(ptNote).css('display', 'none');

        if(y > ptFrameY && !opened){
            $(ptImg).css('top', Math.round(-(y-ptFrameY)/2)+'px');
            y = ptFrameY;
            enlarge = true;
            $(ptImg).addClass('ch');
			$(ptNote).css('display', 'block');
        }

        if(opened){
            $(ptImg).addClass('ch');
            $(ptNote).css('display', 'block');
        }

        nextPrevArrowsPos(y);
        $(ptFrame).css({'width': x+'px', 'height': y+'px'});
        //$(ptFrame).animate({'width': x+'px', 'height': y+'px'}, {easing: "easeOutSine", duration:450});
        //$(ptInner).animate({'width':  x+2+'px'}, {easing: "easeOutSine", duration:450});
        $(ptInner).css('width', x+2+'px');
        
    }

    function resizeFrame(y, top){
        $(ptFrame).animate({'height': y+"px"}, {easing: "easeOutSine", duration:450});
        $(ptImg).animate({'top': top+'px'}, {easing: "easeOutSine", duration:450});
        nextPrevArrowsPos(y);
        opened = opened ? false : true;
        if($.browser.msie && $.browser.version == '6.0') $('#footer').css('position', 'relative');
    }

    leftAndRight($('#referencaMid'), 5);
    function leftAndRight(el, i){
        i = i ? i : 1;
        var frame = $(el).children('.frame').first();
        var slides = $(frame).children('.slides').first();
        var pages = $(slides).children('a');
        var w = $(pages).outerWidth(true);
        var p = 0;
        var arrows = $(el).children('img.arrow');
        arrows.click(function(){
            if($(this).hasClass('left')) move(-1);
            if($(this).hasClass('right')) move(1);
        });

        function move(x){
            p += x;
            if(p<0) p = $(pages).length-i;
            if(p>$(pages).length-i) p = 0;
            $(slides).animate({'left': -p*w+'px'}, 400);
        }
    }

    //NEXT AND PREVIOUS BIG IMAGE
    nextPrevImg($(aImg));
    function nextPrevImg(a){
        $(ptArrows).click(function(){
            src = $(ptImg).attr('src');
            var self = $(this);
            $(a).each(function(i, el){
                if(src == $(el).attr('href')){
                    //next
                    if($(self).hasClass('nextImg')){
                        next = $(el).next('a');
                        if($(next).hasClass('file')) next = $(next).next('a');
                        if(!$(next).length) next = $(a).first();
                        newSrc = $(next).attr('href');
                    }
                    //Previous
                    if($(self).hasClass('prevImg')){
                        prev = $(el).prev('a');
                        if($(prev).hasClass('file')) prev = $(prev).prev('a');
                        if(!$(prev).length) prev = $(a).last();
                        newSrc = $(prev).attr('href');
                    }
                }
            });
            changeImg(newSrc);
        });
    }

    //NEXT AND PREVIOUS ARROWS POSITION
    function nextPrevArrowsPos(h){
        var top = h/2 - 24;
        $(ptArrows).animate({'top': top+'px'}, {easing: "easeOutSine", duration:450});
    }
});

/*
 * ARTICLE IMAGE
 */
$(document).ready(function(){
    var prThumb = $('.prThumb');
    var prBig = $('#prBig');

    $(prThumb).click(function(e){
        e.preventDefault();
        if($(prBig).attr('href') != $(this).attr('href')){
        var self = this;
            $(prBig).fadeOut(400, function(){
                $(prBig).attr('href', $(self).attr('href'))
                        .children('.large')
                        .first()
                        .attr('src', $(self).attr('href'))
            }).fadeIn(400);
        }
    });

});

/*
 * PRINT AND BACK LINK
 */
$(document).ready(function(){
    $('a.print').click(function(e){
        e.preventDefault();
        window.print();
    });

    $('a.back').click(function(e){
        e.preventDefault();
        history.go(-1);
    });
});

/*
 * LOAD ABOUT US
 */
$(document).ready(function(){
    var ic = $('#introCites');
    if($(ic).length){
        setInterval(function() {
            $.get(
                $(ic).children('form').attr('action'),
                $(ic).children('form').serialize(),
                function(data){
                    $(ic).fadeOut(200, function(){
                        $(this).empty()
                               .append(data)
                               .fadeIn(200);
                    });
                }
            );
        }, 5000);
    }
});

/*
 * BING MAP
 */
$(document).ready(function(){
    /*
    if($('#contactMap').length){
        var latLon = new VELatLong(13.645059204101562,45.968221028645836);
        map = new VEMap('contactMap');
        map.LoadMap(latLon, 12);

        pinPoint = map.GetCenter();
        pinPixel = map.LatLongToPixel(pinPoint);
        map.AddPushpin(pinPoint);
    }
    */
});

/**
 * CATALOG ORDER FORM
 */
$(document).ready(function(){
    $('#fSendBy').buttonset();
    $('#fCatalogs').buttonset();
});

/**
 * STRIP
 */
$(document).ready(function(){
    var stripImg = $('#strip div img');
    $('#stripLink').click(function(){
        $('#strip').slideToggle(400);
    });
    $(stripImg).draggable({axis: "x"});

    $('#stripMenu .stripVer').click(function(e){
        e.preventDefault();
        $(stripImg).attr('src', $(this).attr('href'));
    });
});

/*
 * PRIZE COMPETITION FORM
 */
function sendPrizeForm(form){
    $(form).submit(function(e){
        e.preventDefault();
        msg = $(this).next('#prizeMsg');
        $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            data: $(this).serialize(),
            success:function(res){
                var data = jQuery.parseJSON(res);
                if(data.status == 'success') $(form).remove();
                $(msg).fadeOut(200, function(){
                    $(this).text(data.msg).fadeIn(200);
                });
              }
        });
    });
}

/**
 * REFERENCE
 */
$(document).ready(function(){
    
});

/**
 * COMBOBOX
 */
$(document).ready(function(){
    var combo = $('.combobox');
    if(combo.length) $(combo).combobox();
});
