$(document).ready(function(){
      if($('.con_quadia iframe').length) {
          var src = $('.con_quadia iframe').attr('src');
          var scrCut = src.replace(/&autoplay=false/,"");
          if(!$.cookie(scrCut))
          {
              $.cookie(scrCut, 'quadia', { expires: 1, path: '/'});
              $('.con_quadia iframe').attr('src', scrCut + "&autoplay=true")
          }
      }      
      //*****Slider
      //check if slider exists
      if($('#slider').length) {
        var slider = $('#slider');
         //set the hight of the slider
        var setSliderHeight = 491; 
        //slider mover right, checks for animating + appends the last image
        function moveRight(imgWidth, lengthShows) {
            if(lengthShows > 6) {
                    var mover = $('#mover', slider);
                    if(!mover.is(":animated")) {
                    mover.animate( { "left": "-=" + imgWidth + "px" }, {
                        duration: "slow",
                        complete: function() {
                            mover
                                .append($('#mover > a:first', slider))
                                .append($('#mover > div.clear', slider))
                                .css('left', "+=" + imgWidth + "px" );
                        }
                    }); 
                }
            }
        }
        //slider move left. checks if the slider is animating + appends the first image
        function moveLeft(imgWidth, lengthShows) {
           if(lengthShows > 6) {
               var mover = $('#mover', slider); 
                if(!mover.is(":animated")) {
                    mover.prepend($('#mover > a:last', slider)); 
                    newLeft = parseInt(mover.css('left')) - imgWidth;
                    mover.css('left', newLeft);
                    mover.animate({"left": "+="+ imgWidth + "px"}, "slow");            
            }
           }
        }
        //slider moveup
        function moveUp(setSliderHeight, imgHeight, projWidth, wrapWidth, projHeight) {
            //check if the slider is animating
            if(!$('#mover').is(':animated')) {
                if($('#projects').height() == projHeight) {
                //close projects menu
                projectUp = 10 - $('#projectMenu').height();
                $('#projectMenu').animate({
                                top: projectUp
                               }, 500);
                    
                    $('#mover').append("<div class='slider-musicals'>").append("<div class='slider-theatre'>");
                    $('div.slider-musicals').append('<h3>Musicals</h3>');
                    $('div.slider-theatre').append('<h3>Theatershows</h3>');
                    $('#mover > a[rel=musical]', slider).each(function(index) {
                        if (index < 12) {
                            $('div.slider-musicals').append($(this));
                        } else {
                            $(this).remove();        
                        }
                    });
                    
                    $('#mover > a[rel=theatre]', slider).each(function(index) {
                        if (index < 6) {
                            $('div.slider-theatre').append($(this));
                        } else {
                            $(this).remove();    
                        }
                    });
                    
                    $('#moveLeft').css('display', 'none');
                    $('#moveRight').css('display', 'none');
                    var top = parseInt($('#moveWrapper').css('top'));
                    var newTop = (top - setSliderHeight) + imgHeight + 20;
                    leftMover = $('#mover').css('left');
                    $('#mover')
                        .css('left', 0)
                        .css('width',parseInt($('#projects').css('width')));
                    $('#mover, #moveWrapper, #projects').css('height',setSliderHeight);
                    $('#projects')
                        .css('top',newTop + parseInt($('#projects').css('top')))
                        .css('width',projWidth);
                    $('#moveWrapper').css('width',770);     
                    
                    $('#up').addClass('upDown');           
                    $('#projects').addClass('projectUp');           
                }
            }
            
            return leftMover;   
        }
        //slider movedown
        function moveDown(widthMover, projHeight, leftMover, projects, moveWrapper ) {
            $('#moveWrapper').css('width', moveWrapper);
            $('#mover').html(projects);
            $('#up').removeClass('upDown');
            $('#projects').removeClass('projectUp');
            $('#moveLeft').css('display', 'block');
            $('#moveRight').css('display', 'block');
            
            $('#projects, #moveWrapper').css('height',projHeight);
            if(leftMover != undefined) {
                 $('#mover').css('left', leftMover);
            }
            $('#projects').css('top',0);
            $('#mover').css('width', widthMover);
        }
        $(window).load(function() {    
            //save some settings
            var projWidth = $('#projects').width();
            var projHeight = $('#projects').height();
            var wrapWidth = $('#moveWrapper').width();
            var wrapHeight = $('#moveWrapper').height();                                                              
            var imgWidth = parseInt($('#mover a ').width() + parseInt($('#mover a').css('padding-left')) + parseInt($('#mover a').css('padding-right')));
            var imgHeight= parseInt($('#mover a ').height() + parseInt($('#mover a').css('padding-top')) + parseInt($('#mover a').css('padding-bottom')));
            var widthMover = imgWidth * $('#mover > a').length
            var lengthShows = $('#mover > a').length;
            var projects = $('#mover').html();
            var moveWrapper = $('#moveWrapper').width();
            var projectEl = $('#projects');
            
            //this is for the loop. You can stop de animation by change de class in the moveWrapper div
            function moveMe() {
                if(projectEl.height() == projHeight ) {
                    $('#moveRight').trigger('click');
                }
            }
            
            //set some init's, for IE
            $('#mover')
                .css('left', 0)
                .css('width', widthMover);
            $('#moveWrapper').css('top', 0);    
            
            //set the animation loop
            if($('#moveWrapper').attr('class') == "startLoop") {
            setInterval(moveMe, 3000);
            }
            
            //mover left button
            $('#moveLeft').click(function(s) {
                moveLeft(imgWidth, lengthShows);
                
                return false;
            });
            
            //mover right button
            $('#moveRight').click(function() {  
                moveRight(imgWidth, lengthShows);
                
                return false;
            });    
            
            //you can use the the left, right and up keys with the slider
            $(document).keydown(function(e){    
                if(e.keyCode == 39) {
                    if(projectEl.height() == projHeight) {
                        moveRight(imgWidth, lengthShows);
                    }
                } else if(e.keyCode == 37) {
                    if(projectEl.height() == projHeight) {
                        moveLeft(imgWidth, lengthShows);
                    }
                }  else if(e.keyCode == 38) {
                    leftMover = moveUp(setSliderHeight, imgHeight, projWidth, wrapWidth, projHeight);
                } else if(e.keyCode == 40) {
                    moveDown(widthMover, projHeight, leftMover, projects, moveWrapper ); 
                } else if(e.keyCode == 32) {
                     $('#moveLeft').trigger('click');                           
                }
            });
                                                 
            //button for show all images
            $('#up').click(function() {  
                if(projectEl.height() == projHeight) {
                      leftMover = moveUp(setSliderHeight, imgHeight, projWidth, wrapWidth, projHeight);
                } else {
                       moveDown(widthMover, projHeight, leftMover, projects, moveWrapper );
                }
                
                return false;
            });
            
            
        });
    }
    
    //*****topmenu 
    if($('#getProjects').length) {
        $(window).load(function() {    
            //click outside some the slider and show shows to close them
            $(document.body).click(function() {
                if(parseInt($('#projectMenu').css('top')) > 0) {
                    $('#projectMenu').animate({
                        top: projectMenuUp
                    }, 500);
                }
            });
        });
        //slider Project Menu   
        projectTop = 20;
        projectMenuHeight = $('#projectMenu').height();
        //This is for css loading problems, so it will it will show on the correct height;
        if(parseInt($('#projectMenu').css('padding-top')) == 9) {
            projectMenuHeight += 9;
        }
        projectMenuUp = projectTop - projectMenuHeight;
        projectMenuDown = projectTop + projectMenuHeight;  
        // move the menu to the right spot
        $('#projectMenu').css({
            top: projectMenuUp    
        });
        $('#getProjects').click(function(e) {
            if(parseInt($('#projectMenu').css('top')) > 0) {
                $('#projectMenu').animate({
                    top: projectMenuUp
                }, 500);
            } else {
            if($('#projects') && $('#projects').height() > 200) {
                            $('#up').trigger('click');
                        }
                $('#projectMenu').animate({
                    top: projectTop
                }, 500);
            }
            
            return false;
        });
    }
    
    $(document.body).mouseup(function() {
        $('.form_order_now-submit').removeClass('hover');
    });
    
    $('.form_order_now-submit').mousedown(function() {
        $('.form_order_now-submit').toggleClass('hover', 'addOrRemove');
        return false;
    });
    $('.form_order_now-submit').mouseup(function() {
        $(location).attr('href',$('#form_order_now-shows').val());
    });
    
    $('#form_order_now').submit(function() {
        return false;    
    });
    
    //footer
    $('#footer_select_projects').change(function() {
        var lnk = $(this).val().split('@!');
        if(lnk[0] == "_blank") {
                window.open(lnk[1]);
        } else {
                $(location).attr('href', lnk[1]);
        }
    });
    
    //cast
    if($('#con_cast').length) {
        //set current page underline
        function ajustPaging(thisPage) {
            $('.news_nav > .pageNumber').each(function(i) {
                
                
                if(thisPage > castPages) {
                    thisPage = 1;
                }
                
                if(thisPage == 0) {
                    thisPage = castPages;    
                }
                
                if((i+1) == thisPage) {
                    c = i+2;
                    $('.news_nav .pageNumber').removeClass('selected');
                    $('.news_nav .pageNumber:nth-child('+c+')').addClass('selected');
                } 
            });
        }
        //save these 2 for later use
        castWidth = $('.con_cast_page').width();
        castPages = $('.con_cast_page').length;
    if(castPages == 1) {
        $('#moveLeft').css('visibility', 'hidden');
        $('#moveRight').css('visibility', 'hidden');
    } else {
        $('#moveLeft').css('visibility', 'hidden');
    }
        
        //start page number
        thisPage = 1;
        
        //set page number
        $('#con_cast_wrapper').css('width',parseInt(castWidth) * castPages);
        for(i=1; i<=castPages; i++) {
            if($('.pageNumber').text() !== i) {
                $('.news_nav > .pageNumber:first').clone().appendTo('.news_nav').text(i);
            }
        }
        
        
        //rollover of the images
        img = [];
        $('.cast > a[name=cast]').each( function(i) {
            img[i] = $(this).children().attr('src');
            $(this).mouseout(function() {
                $(this).children().attr('src', img[i]);
            }).mouseover(function() {
                $(this).children().attr('src', $(this).attr('rel'));
            });
        });
        
        
        $('.news_nav').append($('#moveRight'));
        
        //remove the original page link + set selected page number
        $('.news_nav > .pageNumber:first').remove();
        ajustPaging(thisPage);
        //jump to page
        $('.pageNumber').click(function(i) {
            if(!$('#con_cast_wrapper').is(":animated")) {
                goTo = $(this).text();
                
                //if the page where you want to go to is bigger then the current page
                if(goTo > thisPage) {
                    number = (goTo - thisPage);
                
                    $('#con_cast_wrapper').animate(
                        {"left": "-="+ (castWidth * number) + "px"}, {
                            duration:"slow",
                            complete: function() {
                                for(i=0; i<number; i++) {
                                    $('#con_cast_wrapper').append($('#con_cast_wrapper > .con_cast_page:first'));
                                }
                                $('#con_cast_wrapper').css('left', 0);
                            }
                        }
                    ); 
                    ajustPaging(thisPage+=number);
                    
                //if the page where you want to go to is bigger then the current page    
                } else if(goTo < thisPage) {
                    number = (thisPage - goTo);
                    for(i=0; i<number; i++) {
                        $('#con_cast_wrapper').prepend($('#con_cast_wrapper > .con_cast_page:last')); 
                    }
                    newLeft = parseInt($('#con_cast_wrapper').css('left')) - castWidth * number;
                    $('#con_cast_wrapper').css('left', newLeft).animate({"left": "+="+ (castWidth * number) + "px"}, "slow");  
                    ajustPaging(thisPage-=number);
                }
        hideArrows(thisPage, castPages);
            }
            return false;
        });
        
        $('#moveRight').click(function() {
            if(!$('#con_cast_wrapper').is(":animated")) {
                $('#con_cast_wrapper').animate( { "left": "-=" + castWidth + "px" }, {
                    duration: "slow",
                    complete: function() {
                        $('#con_cast_wrapper')
                            .append($('#con_cast_wrapper > .con_cast_page:first'))
                            .css('left', "+=" + castWidth + "px" );
                    }
                }); 
                ajustPaging(thisPage+=1);
                if(castPages < thisPage) {
                    thisPage = 1;
                }
        hideArrows(thisPage, castPages);
            }
            
            return false;
        });

    function hideArrows(thisPage, castPages) {
         if(thisPage == 1) {
                        $('#moveLeft').css('visibility', 'hidden');
                } else {
                        $('#moveLeft').css('visibility', 'visible');
                }
        if(thisPage == castPages) {
            $('#moveRight').css('visibility', 'hidden');
        } else {
            $('#moveRight').css('visibility', 'visible');
        }
    }
        
        $('#moveLeft').click(function() {
            if(!$('#con_cast_wrapper').is(":animated")) {
                $('#con_cast_wrapper').prepend($('#con_cast_wrapper > .con_cast_page:last')); 
                newLeft = parseInt($('#con_cast_wrapper').css('left')) - castWidth;
                $('#con_cast_wrapper').css('left', newLeft).animate({"left": "+="+ castWidth + "px"}, "slow");  
                if(thisPage == 1) {
                    thisPage = castPages +1;
                }
                ajustPaging(thisPage-=1);                
        hideArrows(thisPage, castPages);
            }
                        
            return false;
        });
        
        
        $('#con_cast_wrapper').css('width',parseInt(castWidth) * $('.con_cast_page').length);
        
        
        //sets the position of the pages based on number of pages
        newW = 0;
        $('.news_nav > a').each(function(){
            newW += parseInt($(this).width())+4;
        });
        
        $('.news_nav').css('width', newW+84)
    }
});
