$(document).ready(function() {

    var $menu = $('#frame-menu li');
    var $menuText = $('#frame-menu li span');
    var $homeContent = $('#frame-content div');
    var $page = $('#frame-content');
    var $pageContent = $('#frame-content-page');
    var today = new Date();

    /*  Check for a cookie and valid date stuff */
    var cookiename = "emma-janethommen";
    var exp_days = 1;
    var val = "Emma Jane Thommen intro first run check.";

    var locURL = location.href;

    $('input[type=text]#email').live('focusin focusout', function(evt) {
        var origText = "Enter your email here...";
        if (evt.type == "focusin") {
            $(this).val('');
        } else {
            if ($(this).val().search('@') < "0") {
                $(this).val(origText);
            }
        }
    });

    if (checkCookie(cookiename)) {
        if (locURL.search('.htm') <= -1 ) {
            window.open('emma-janethommen/musicplayer.html','player','width=640, height=350, scrollbars=no, location=no, status=no, resizable=no, toolbar=no, menubar=no, copyhistory=no');
        }
        $('#loading-intro').hide(function() {
            $.each($page.find('div'), function() {
                if ($(this).attr('class')) {
                    if ($(this).css('display') == "none") {
                        $(this).fadeOut('500');
                    }
                }
            });
            $('#loading-intro').slideUp('500');
        });
    } else {
        setCookie(cookiename, val, exp_days);
        /*  Show Intro and hide the other elements  */
        $.each($page.find('div'), function() {
            /*  Hide the fancy hover stuff and make sure the keys stay visible  */
            if ($(this).attr('class') && $(this).attr('class').indexOf('-key') < 1) {
                $(this).hide();
            }
        });
    }
    
    /*  Handle Skip Intro   */
    /*$('#skip-intro').live('click', function() {
        skipIntro($page);
    });*/

    /*  DatePicker  */
    $('#perfomance-date').datepicker({
        minDate: "+1D",
        maxDate: "+365D",
        changeMonth: true,
        changeYear: true,
        dateFormat: "d/mm/yy",
        showAnim: "fold"
    });

    /*  Time Picker */
    $('#start-perfomance-time').ptTimeSelect();
    $('#end-perfomance-time').ptTimeSelect();

    /*  Adds a class attr to the menu to allow the other 2 functions to run   */
    $.each($menuText,function(){
        var name = $(this).html()
        name = name.toLowerCase()
        $(this).closest('li').addClass(name+'-menu');
    });

    /*  Adds the hover effect to the homepage image if the menu items are hovered over  */
    $($menu).live('mouseover mouseout',function(event){
        var divhover = $(this).attr('class').split('-');
        divhover = divhover[0];
        if (event.type == 'mouseover') {
            $('.'+divhover).addClass(divhover+'-hover');
        } else {
            $('.'+divhover).removeClass(divhover+'-hover');
        }
    });

    /*
     *  Adds the hover effect to the menu if the homepage image items are hovered over and add an onclick event to allow you to navigate.
     *  Stuck in this If statement to stopp JS errors on other pages
     */
    if ($($homeContent).attr('class') != "") {
        $($homeContent).live('mouseover mouseout',function(event){
            var menuhover = $(this).attr('class');
            var link = $('.'+menuhover+'-menu').find('a');
            if (event.type == 'mouseover') {
                if (menuhover == "" || menuhover.indexOf("-key") > 1) {

                } else {
                    $('.'+menuhover+'-menu').addClass('menu-hover');
                    $('.'+menuhover).live('click', function(){
                        location.href = $(link).attr('href');
                    });
                }
            } else {
                $('.'+menuhover+'-menu').removeClass('menu-hover');
            }
        });
    }

    /*  Live Event Month List */
    $('#live-event-month-list li').live('click', function() {
        var $eventsContent = $(this).closest('div#frame-content-page').find('div#live-event-list');
        var dates = $(this).attr('id');
        dates = dates.split('-');
        var month = dates[1];
        var year = dates[2];
        
        $.each($('#live-event-month-list li'), function(){
            if($(this).hasClass('activemonth')) {
                $(this).removeClass('activemonth')
            }
        });
        
        $(this).addClass('activemonth');
        $eventsContent.fadeOut('500', function(){
            $.post('emma-janethommen/ajax/getEvents.php',{'month':month, 'year':year,'requested':'listings'},function(data) {
                $eventsContent.html(data);
                $eventsContent.fadeIn('500');
                /*  Live Event List */
                $('#live-event-list li:nth-child(odd)').addClass('odd');
            });
        });
    });

    /*  Get Past Events*/
    $('#past-gigs').live('click', function(){
        var $eventsContent = $(this).closest('div#frame-content-page').find('div#live-event-list');
        var date = $('#live-event-month-list li:first-child').attr('id');
        date = date.split('-');
        var month = date[1];
        var year = date[2];
        $.post('emma-janethommen/ajax/getEvents.php',{'month':month, 'year':year, 'requested':'pastevents'},function(data) {
            $eventsContent.html(data);
            $eventsContent.fadeIn('500');
            /*  Live Event List */
            $('#live-event-list li:nth-child(odd)').addClass('odd');
        });
    });

    /*  Live Event Details  */
    $('#live-event-list li').live('click', function(){
        var event = $(this).attr('id');
        var $parent = $(this).parent();
        event = event.split('-');
        event = event[1];
        $parent.fadeOut('500', function() {
            $.post('emma-janethommen/ajax/getEvents.php', {'requested':'viewevent', 'event':event}, function(data){
                $parent.html(data);
                $parent.fadeIn('500');
            });
        });
    });

    /*  Handle Hover effect */
    $('#form-sumbit').live('mouseover mouseout',function(event){
        if (event.type == 'mouseover') {
            $(this).attr({'src':'emma-janethommen/images/submit_active.png'});
        } else {
            $(this).attr({'src':'emma-janethommen/images/submit_passive.png'});
        }
    });

    /*  Handle Enquiry Form Submission  */
    $('#form-sumbit').live('click', function(){
        var $form = $(this).parent();
        var send = true;
        $.each($form.children('input[type=text]'), function(){
            if($(this).hasClass('req')){
                if($(this).val() == "" || $(this).val() == null) {
                    $(this).css({'background':'fireBrick', 'border':'2px solid red'});
                    send = false;
                } else {
                    $(this).css({'background': '#1F1F1F',
                                    'border-top': '2px solid #515151',
                                    'border-left': '2px solid #515151',
                                    'border-right': '2px solid #2D2D2D',
                                    'border-bottom': '2px solid #2D2D2D'
                                });
                }
            }
        });
        if (send) {
            $form.fadeOut('500', function() {
                $.post('emma-janethommen/ajax/processEnquiry.php', {'form':$form.serializeArray()}, function(data) {
                    $('#form-submission').html(data);
                    $('#form-submission').fadeIn('500');
                });
            });
        }
        return false;
    });
    
    $('#newsletter-signup input[type=image]').live('mouseover mouseout', function(evt){
        var src = $(this).attr('src')
        if (evt.type == "mouseover") {
            src = src.replace("passive","active");
            $(this).attr({'src': src});
        } else {
            src = src.replace("active","passive");
            $(this).attr({'src': src});
        }
    });

    /*  Gallery Image Handling  */
    $('#frame-content-page a.galleryImage').fancybox({
        speedIn : '500',
        speedOut : '500',
        transitionIn: 'elastic',
        transitionOut: 'elastic',
        overlayShow: false
    });

    $('.openMusicPlayer').live('click', function() {
        window.open('emma-janethommen/musicplayer.html','popup','width=640, height=350, scrollbars=no, location=no, status=no, resizable=no, toolbar=no, menubar=no, copyhistory=no');
        return false;
    });

    $('#play-music').live('click', function() {
        skipIntro($page);
        window.open('emma-janethommen/musicplayer.html','popup','width=640, height=350, scrollbars=no, location=no, status=no, resizable=no, toolbar=no, menubar=no, copyhistory=no');
        return false;
    });
    
    $('#no-music').live('click', function() {
        skipIntro($page);
        return false;
    });

});

function skipIntro($page) {
    $('#loading-intro').fadeOut('500', function(){
        /*  Safari Fix  */
        $.each($page.find('div'), function() {
            if ($(this).attr('class')) {
                if ($(this).css('display') == "none") {
                    $(this).show();
                }
            }
        });
        $('#loading-intro').slideUp('500');
    });
}

function checkCookie(cookie_n) {
    return true;
    if (document.cookie.length > 0) {
        var cookie_start = document.cookie.indexOf(cookie_n+"=");
        if(cookie_start != -1) {
            cookie_start = cookie_start+cookie_n.length+1;
            //return false; /*Testing and design of the into section  */
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

function setCookie(cookie_n, value ,date_expr) {
    var cookie_expr = new Date();
    cookie_expr.setDate(cookie_expr.getDate()+date_expr);
    document.cookie=cookie_n+"="+escape(value)+((date_expr==null) ? "" : ";expires="+cookie_expr.toUTCString());
}
