this.imagePreview = function(){
    /* CONFIG */

        xOffset = 10;
        yOffset = 30;

        // these 2 variable determine popup's distance from the cursor
        // you might want to adjust to get the right result

    /* END CONFIG */
    jQuery("a.preview").hover(function(e){
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");
        $("#preview")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px")
            .fadeIn("fast");
    },
    function(){
        this.title = this.t;
        $("#preview").remove();
    });
    jQuery("a.preview").mousemove(function(e){
        $("#preview")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px");
    });
}

jQuery(document).ready(function($) {
    imagePreview();
    /**
    * Search form hooks: clear search form fields (main-search.php) on click
    * search submit is disabled until a who/wheer is enetered, place only clears field, not enable button
    */
    $("#keyword").click(function(){ $(this).attr({ value: '' }); $("#place").attr({ value: '' }); $("#searchSubmit").removeAttr("disabled"); });
    $("#place").click(function(){ $(this).attr({ value: '' }); });

    /* ad type1 = online click */
    $("#advertType1").click(function(){
        $(".advertise_adSize").css({'visibility': 'hidden', 'height': '0px'});
        $(".advertise_adType").css({'visibility': 'visible', 'height': 'auto'});
        $(".prevNext_page1").css({'visibility': 'visible', 'height': 'auto'});
    })

    /* ad type3 = in the mag click */
    $("#advertType2").click(function(){
        $(".advertise_adSize").css({'visibility': 'visible', 'height': 'auto'});
        $(".advertise_adType").css({'visibility': 'hidden', 'height': '0px'});
        $(".prevNext_page1").css({'visibility': 'visible', 'height': 'auto'});
    })

    /* ad type3 = both click */
    $("#advertType3").click(function(){
        $(".advertise_adSize").css({'visibility': 'visible', 'height': 'auto'});
        $(".advertise_adType").css({'visibility': 'visible', 'height': 'auto'});
        $(".prevNext_page1").css({'visibility': 'visible', 'height': 'auto'});
    })

    /* premium ads click - enable description area */
    $("#advertOnline2").click(function(){
        $(".advertise_premium_only").css({'visibility': 'visible', 'height': 'auto'});
    })

    /* free ads click - disable description area */
    $("#advertOnline1").click(function(){
        $(".advertise_premium_only").css({'visibility': 'hidden', 'height': '0px'});
    })

    /* print size dropdown click */
    $("#advertPrint").change(function(){
        var src = $("#printSize").attr("src");
        var newid = $("#advertPrint").attr("value");
        $("#printSize").attr('src','/wp-content/themes/arrow-advertiser/images/advertise/magpreview'+newid+'.png ');
    })

    /* show page 1 - hide page 2 */
    $("#toPage1").click(function(){
        /* show / hide elements based on options */
        $(".advertise_adWhere").css({'visibility': 'visible', 'height': 'auto'});
        $(".advertise_adType").css({'visibility': 'visible', 'height': 'auto'});
        $(".advertise_adSize").css({'visibility': 'visible', 'height': 'auto'});
        $(".prevNext_page1").css({'visibility': 'visible', 'height': 'auto'});
        $('[name=advertType]').each(function(i,v){if(v.checked) $(v).click();});
        /* show page 1, hide page 2 */
        $("#advertise_page1").css({'visibility': 'visible', 'height': 'auto'});
        $("#advertise_page2").css({'visibility': 'hidden', 'height': '0px'});
        $("#advertise_page3").css({'visibility': 'hidden', 'height': '0px'});
    })

    /* hide page 1 - showw page 2 */
    $("#page1to2").click(function(){
        /* hide all elemets for page 1 */
        $("#advertise_page1").css({'visibility': 'hidden', 'height': '0px'});
        $(".advertise_adWhere").css({'visibility': 'hidden', 'height': '0px'});
        $(".advertise_adType").css({'visibility': 'hidden', 'height': '0px'});
        $(".advertise_adSize").css({'visibility': 'hidden', 'height': '0px'});
        $(".prevNext_page1").css({'visibility': 'hidden', 'height': '0px'});
        /* show page 2 */
        $("#advertise_page2").css({'visibility': 'visible', 'height': 'auto'});
    })

    /* jump from page 1 to 3 (existing advetiser placing another ad) hide page 1 - showw page 3 */
    $("#page1to3").click(function(){
        /* hide all elemets for page 1 */
        $("#advertise_page1").css({'visibility': 'hidden', 'height': '0px'});
        $(".advertise_adWhere").css({'visibility': 'hidden', 'height': '0px'});
        $(".advertise_adType").css({'visibility': 'hidden', 'height': '0px'});
        $(".advertise_adSize").css({'visibility': 'hidden', 'height': '0px'});
        $(".prevNext_page1").css({'visibility': 'hidden', 'height': '0px'});
        /* show page 2 */
        $("#advertise_page3").css({'visibility': 'visible', 'height': 'auto'});
    })

    /* hide page 2 - showw page 3 */
    $("#toPage3").click(function(){
        /* show page 2 */
        $("#advertise_page1").css({'visibility': 'hidden', 'height': '0px'});
        $("#advertise_page2").css({'visibility': 'hidden', 'height': '0px'});
        $("#advertise_page3").css({'visibility': 'visible', 'height': 'auto'});
    })

    /* hide page 2 - showw page 3 */
    $("#page3to2").click(function(){
        /* show page 2 */
        $("#advertise_page2").css({'visibility': 'visible', 'height': 'auto'});
        $("#advertise_page3").css({'visibility': 'hidden', 'height': '0px'});
    })

    /* page 3 finish - submit form */
    $("#toFinish").click(function(){ jQuery('#advertiseForm').submit(); })



    $('label.required').append('&nbsp;<strong>*</strong>&nbsp;');

    /*
    * Listing forms Validation functions
    */
    var vlistingForm = $("#listingForm").validate({
        rules: {
            adTitle: { required: true, minlength: 4, maxlength: 28 },
            adSalary: { required: true, minlength: 2, maxlength: 14 },
            adPrice: { required: true, minlength: 2, maxlength: 8 },
            adTelephone: { required: true, minlength: 6 },
            adDescription: { required: true, minlength: 8, maxlength: 250 },
            adEmail: { required: true, email: true },
            adDate: { required: true, date: true },
            adImage: { accept: "jpg|jpeg" },
            terms: { required: true }

        },
        messages: {
            adTitle: {
                required: "Please enter a title for your Listing",
                minlength: "Listing title must be at least 4 characters",
                maxlength: "Listing title must be less then 29 characters"
            },
            adPrice: {
                required: "Please the price for this item",
                minlength: "Item price must be at least 2 digits",
                maxlength: "Item price too high",
                digits: "Please enter numbers only"
            },
            adSalary: {
                required: "Please enter the salary for this job",
                minlength: "Salary price must be at least 2 digits",
                maxlength: "Salary entry is too long",
                digits: "Please enter numbers only"
            },
            adTelephone: {
                required: "Please enter your telephone number",
                minlength: "Telephone number must be at least 6 digits",
                digits: "Please only enter numbers"
            },
            adDescription: {
                required: "Please enter some description text for your listing",
                minlength: "The description must be at least 8 characters long",
                maxlength: "The description cannot be more than 250 characters long",
            },
            adEmail: {
                required: "Please enter an email address for this listing",
                email: "Please enter a valid email address"
            },
            adDate: {
                required: "Please enter a date for this event",
                date: "Please enter a date in the format dd/mm/yyyy"
            },
            adImage: { accept: "Images must be jpg format only" },
            // alert
            //terms: { required: function() { alert("You must accept the terms and conditions"); }}
            terms: { required: "You must accept the terms and conditions" }
        }
    });


    /*
    * Advertise form Validation functions
    */
    var vadForm = $("#advertiseForm").validate({
        rules: {
            // advertise page 2
            emailAddress: { required: true, minlength: 3, email: true },
            password: { required: true, minlength: 5 },
            passwordVer: { required: true, minlength: 5, equalTo: "#password" },
            fullName: { required: true, minlength: 3 },
            companyName: { required: true, minlength: 3 },
            telephone: { required: true, minlength: 6 },
            // advertise page 3
            adTitle: { required: true, minlength: 8, maxlength: 28 },
            adCategory: { required: true, minlength: 1 },
            adTelephone: { required: true, minlength: 6 },
            adAddress1: { required: true, minlength: 3 },
            adAddress2: { required: true },
            adAddress3: { required: true },
            adAddress4: { required: true },
            adAddress5: { required: true, maxlength: 8 },
            adDescription: { required: true, minlength: 8, maxlength: 349 },
            adURL: { required: true, url: true },
            adEmail: { required: true, email: true },
            terms: { required: true }
        },
        messages: {
            emailAddress: "Please enter a valid email address",
            password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            passwordVer: {
                required: "Please provide a password",
                equalTo: "Please enter the same password as above"
            },
            fullName: {
                required: "Please enter your full name"
            },
            companyName: {
                required: "Please enter your company name"
            },
            telephone: {
                required: "Please enter your telephone number",
                minlength: "Telephone number must be at least 6 digits",
                digits: "Please only enter numbers"
            },
            // advertise Page 2
            adTitle: {
                required: "Please enter a title for your advertisement",
                minlength: "Ad titlle must be at least 8 characters",
                maxlength: "Ad titlle must be less then 29 characters"
            },
            adCategory: {
                required: "Please select a category"
            },
            adTelephone: {
                required: "Please enter a telephone number to be displayed on the ad",
                minlength: "Telephone number must be at least 6 digits",
                required: "Please enter your telephone number"
            },
            adAddress1: {
                required: "Please enter the first line of your address"
            },
            adAddress2: {
                required: "Please enter the second line of your address"
            },
            adAddress3: {
                required: "Please enter your town"
            },
            adAddress4: {
                required: "Please enter your county"
            },
            adAddress5: {
                required: "Please enter your postcode",
                maxlength: "Postcode is too long"
            },
            adDescription: {
                required: "Please enter some description text for your advertisement",
                minlength: "The description must be at least 8 characters long",
                maxlength: "The description cannot be more than 350 characters long",
            },
            adURL: {
                required: "Please enter your website URL",
                url: "Please enter a URL beginning with http;//"
            },
            adEmail: {
                required: "Please enter an email address for this advertisement",
                email: "Please enter a valid email address"
            },

            terms: { required: "You must accept the terms and conditions" }
            // alert
            //terms: { required: function() { alert("You must accept the terms and conditions"); }}
        }
    });

    /*
    * Tell a friend form Validation functions
    */
    var vadForm = $("#tellFriendForm").validate({
        rules: {
            // advertise page 2
            yourEmail: { required: true, minlength: 3, email: true },
            friendsEmail: { required: true, minlength: 3, email: true },
            yourName: { required: true, minlength: 3 },
            friendsName: { required: true, minlength: 3 },
            addMessage: { required: true, minlength: 8, maxlength: 349 },
        },
        messages: {
            yourEmail: "Please enter a valid email address",
            friendsEmail: "Please enter a valid email address",
            yourName: "Please enter your name",
            friendsName: "Please enter your friends name",
            addMessage: "Please enter a message to your friend"
        }
    });

});
