﻿
// Check that the page has loaded
$(document).ready(function() {

    // Preload menu images 
    menuTopImage = new Image();
    menuTopImage.src = "/Best4PlantsDesignerPlants/i/dropDown_top.png";
    menuMiddleImage = new Image();
    menuMiddleImage.src = "/Best4PlantsDesignerPlants/i/dropDown_middle.png";
    menuMiddleOverImage = new Image();
    menuMiddleOverImage.src = "/Best4PlantsDesignerPlants/i/dropDown_middle_over.png";
    menuBottomImage = new Image();
    menuBottomImage.src = "/Best4PlantsDesignerPlants/i/dropDown_bottom.png";

    // MAIN NAV DROPDOWN  
    $('#mainNav LI A').hover(
        function() {
            // Show dropdown
            $(this).parent('LI').find('.dropdown').show();
        },

        function() {
            // Hide dropdown
            $(this).parent('LI').find('.dropdown').hide();
        });

    $('#mainNav .dropdown').hover(
        function() {
            // Show dropdown
            $(this).show();
        },

        function() {
            // Hide dropdown
            $(this).hide();
        });

    // Item rollover
    $('#mainNav .dropdown .item').hover(
        function() {
            if (!$('BODY').hasClass('browserIE6')) {
                $(this).css({ backgroundImage: 'url(i/dropDown_middle_over.png)' });
            } else {
                $(this).css({ backgroundImage: 'url(i/dropDown_middle_over.gif)' });
            }
        },

        function() {
            if (!$('BODY').hasClass('browserIE6')) {
                $(this).css({ backgroundImage: 'url(i/dropDown_middle.png)' });
            } else {
                $(this).css({ backgroundImage: 'url(i/dropDown_middle.gif)' });
            }
        }
    );

    // case study link
    $('a.caseStudies, a.information').click(function() {
        return false;
    });

    // Big target item
    $("#mainNav .dropdown .item A").bigTarget({
        clickZone: '.item' // jQuery parent selector
    });
    $('.item').css({ 'cursor': 'pointer' });


    // CASE STUDY GALLERY

    // Remove margin for last Thumbnail
    $('.caseStudyGallery .thumbnails .thumbnail:last').css({ margin: '0' });

    // Load correct image on click
    $('.caseStudyGallery .thumbnails .thumbnail A').click(function() {
        var imgURL = $(this).attr('href');
        var img = new Image();
        $('.caseStudyGallery .thumbnails .thumbnail A .overlay').remove();
        $(this).append('<span class="overlay"></span>');
        $(img).load(function() {

            $('.caseStudyGallery .mainImg IMG').attr('src', imgURL);


            Reflection.remove(document.getElementById("studyImage"));
            Reflection.add(document.getElementById("studyImage"));
        }).attr('src', imgURL);

        return false;

    });

    // Add reflection to page load if on case studies 
    if (window.location.toString().indexOf("caseStudy") > 0) {
        $('body').attr('OnLoad', "Reflection.add(document.getElementById('studyImage'))");
    }

    // Select the first image
    $('.caseStudyGallery .thumbnails .thumbnail A:first').append('<span class="overlay"></span>');
    $('.caseStudyGallery .mainImg IMG').attr('id', 'studyImage');

    // Quote marks for testimonials  
    $('.quote').prepend('<img src="i/testimonials/quote_left.gif" />');
    $('.quote').append('<img src="i/testimonials/quote_right.gif" />');


    // TRADE LOGIN
    $('#mainNav LI .tradeLogin.loggedOut').click(function() {
        $('#tradeLogin').show();
    });

    if (document.location.search.indexOf("login=true") > 0) {
        $('#tradeLogin').show();
    }

    $('.cancel a').click(function() {
        $('#tradeLogin').hide();
        return false;
    });
    
      // Logout
    $('.logOut A').live('click', function() {
        $.ajax({
            type: "POST",
            url: '/Best4PlantsDesignerPlants/methods/methods.aspx/logOut',
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                window.location = "/Best4PlantsDesignerPlants/";
            }

        });

        return false;

    });
    
    // Forgot password
    $('.forgotPassword A').live('click', function() {
          if ($('#txt_loginEmail').val() == "") {
            $('#loginError').html("<span class=\"error\">Please enter your email address</span>");
        }
        else if (!isValidEmail($('#txt_loginEmail').val())) {
            $('#loginError').html("<span class=\"error\">Please enter a valid email address</span>");
        }
        else {
            $.ajax({
                type: "POST",
                url: 'methods/methods.aspx/passwordReminder',
                data: "{'email': '" + $('#txt_loginEmail').val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    result = JSON.parse(data.d);
                    if (result.success) {
                        $('#loginError').html('Successfully sent reminder');
                    }
                    else {
                        if (result.errorMessage != "Server Error") {
                            $('#loginError').html("<span class=\"error\">" + result.errorMessage + "</span>");
                        }
                        else {
                            $('#loginError').html("<span class=\"error\">There was an error, please try again later</span>");
                        }
                    }
                }
            });
        }
        return false;
    });

    // TRADE CAT NAVIGATION/SKINNING    
    $('#tradeCatNav LI A').click(function() {
        $('.loading').show();
        // Nav colouring
        $('#tradeCatNav LI').removeClass('selected');
        $(this).parent('LI').addClass('selected');

        // Body skinning
        var skin = $(this).attr('class');
        $('#tradeCatSkin').removeClass().addClass(skin);

        $('#plants').empty();
        var letter = $('.index li.selected a').html();
        var category = $(this).attr('title');
        var JSONData = "{'letter': '" + letter + "', 'category': '" + category + "', 'page' : '1'}";
        $.ajax({
            type: "POST",
            url: '/Best4PlantsDesignerPlants/tradeCat.aspx/returnPlants',
            data: JSONData,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                data = JSON.parse(data.d);
                if (data.plantCount > 1) {
                    // Setup paging
                    $('.pageFrom').html('1');
                    $('.pageTo').html(data.pages);
                    $('.prev').hide();
                    if (data.pages > 1) {
                        $('.next').show();
                    }
                    else {
                        $('.next').hide();
                    }
                    // Render the plants
                    $('#plants').html(data.plants);
                    setDropdowns();
                }
                else {
                    $('#plants').html("<p>No Plants Found In This Category/Letter</p>");
                }
                $('.loading').hide();

            }

        });

        return false;

    });

    var page = parseInt($('.pageTo').html());
    if (page > 1) {
        $('.next').show();
    }

});

function isValidEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return (reg.test(email))
}
