﻿/* HashChange Event */
(function ($, i, b) { var j, k = $.event.special, c = "location", d = "hashchange", l = "href", f = $.browser, g = document.documentMode, h = f.msie && (g === b || g < 8), e = "on" + d in i && !h; function a(m) { m = m || i[c][l]; return m.replace(/^[^#]*#?(.*)$/, "$1") } $[d + "Delay"] = 100; k[d] = $.extend(k[d], { setup: function () { if (e) { return false } $(j.start) }, teardown: function () { if (e) { return false } $(j.stop) } }); j = (function () { var m = {}, r, n, o, q; function p() { o = q = function (s) { return s }; if (h) { n = $('<iframe src="javascript:0"/>').hide().insertAfter("body")[0].contentWindow; q = function () { return a(n.document[c][l]) }; o = function (u, s) { if (u !== s) { var t = n.document; t.open().close(); t[c].hash = "#" + u } }; o(a()) } } m.start = function () { if (r) { return } var t = a(); o || p(); (function s() { var v = a(), u = q(t); if (v !== t) { o(t = v, u); $(i).trigger(d) } else { if (u !== t) { i[c][l] = i[c][l].replace(/#.*/, "") + "#" + u } } r = setTimeout(s, $[d + "Delay"]) })() }; m.stop = function () { if (!n) { r && clearTimeout(r); r = 0 } }; return m })() })(jQuery, this);
/* HoverIntent Override */
(function ($) { $.fn.hoverIntent = function (f, g) { var cfg = { sensitivity: 7, interval: 100, timeout: 0 }; cfg = $.extend(cfg, g ? { over: f, out: g} : f); var cX, cY, pX, pY; var track = function (ev) { cX = ev.pageX; cY = ev.pageY; }; var compare = function (ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); if ((Math.abs(pX - cX) + Math.abs(pY - cY)) < cfg.sensitivity) { $(ob).unbind("mousemove", track); ob.hoverIntent_s = 1; return cfg.over.apply(ob, [ev]); } else { pX = cX; pY = cY; ob.hoverIntent_t = setTimeout(function () { compare(ev, ob); }, cfg.interval); } }; var delay = function (ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); ob.hoverIntent_s = 0; return cfg.out.apply(ob, [ev]); }; var handleHover = function (e) { var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget; while (p && p != this) { try { p = p.parentNode; } catch (e) { p = this; } } if (p == this) { return false; } var ev = jQuery.extend({}, e); var ob = this; if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); } if (e.type == "mouseover") { pX = ev.pageX; pY = ev.pageY; $(ob).bind("mousemove", track); if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout(function () { compare(ev, ob); }, cfg.interval); } } else { $(ob).unbind("mousemove", track); if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout(function () { delay(ev, ob); }, cfg.timeout); } } }; return this.mouseover(handleHover).mouseout(handleHover); }; })(jQuery);

var bookingsValid = false;
var registervalid = false;

$(document).ready(function () {
    navMenu();
    ajaxStateMachine();
    ieShadow();
    $('#loader').hide();
});

$(window).resize(function () {
    ieShadow();
});

$(document).resize(function () {
    ieShadow();
});

function ieShadow() {
    $('#ieshell').height($('#shell').height());
    $('#ieshell').css('left', ($(window).width() - $('#ieshell').width())/2 );
};

function loadContent(file, callback) {
    var content = $('#ajaxContent');
//    content.ajaxStart(function () { // Progress Bar
//        $('#loader').show();
//    }).ajaxStop(function () {
//        $('#loader').hide();
//    });
    content.fadeOut('fast', function () {
        content.load(file, function () {
            content.slideDown('slow', callback);
        });
    });
    
}

function navMenu() {
    $('#nav > ul li').hoverIntent(function () {
        $(this).find('ul').show('fast');
    }, function () {
        $(this).find('ul').hide();
    });
};

function rotateExhibitImage() {
    $('#whyimage img').each(function (i) {
        var current = $(this).attr('src').substr(14, 1);
        if (++current > 3) current = 0;
        $(this).attr('src', 'img/exhibit/E_' + current + '.jpg');
    });
}

function ajaxStateMachine() {
    var myinterval;
    $(window).bind('hashchange', function () {
        var state;
        if ((location.hash).substr(0, 2) == "#!") {
            state = (location.hash).substr(2, location.hash.length);
            state = state.replace(/%20/g, " ");
            switch (state) {
                case "Why Exhibit": loadContent('data/exhibit.htm #whyexhibit', function () {
                    clearInterval(myinterval);
                    myinterval = setInterval("rotateExhibitImage()", 1000);
                }); break;
                case "Exhibition Profile": loadContent('data/exhibit.htm #exhibitionprofile'); break;
                case "Book a Stand": loadContent('data/bookstand.htm', function () {
                    $('#book_stand td:first-child').css('text-align', 'left');
                    $('#book_stand td:nth-child(2)').css('text-align', 'left');
                    $.ajax({
                        url: "data/countries.ashx",
                        dataType: 'json',
                        success: function (data) {
                            for (var i = 0; i <= data.length; i++) {
                                $('#countrylist').append('<option value="' + data[i].code + '"> ' + data[i].name + ' </option>');
                            }
                        }
                    });
                    //                    $('#loader').hide();
                    $('#submit').click(function () {
                        $(this).attr('disabled', 'disabled');
                        bookingsValid = true;
                        $('#book_stand td:first-child').css('color', 'black');
                        bookings_validate();
                        if (bookingsValid) {
                            var others = $('input[name=interests]:checked').val();
                            (others == 'Other') ? others = $('#othertxt').val() : others = others;
                            $.ajax({
                                type: "POST",
                                contentType: "application/x-www-form-urlencoded",
                                data: { firstname: "" + $('#first_name').val() + "",
                                    lastname: "" + $('#last_name').val() + "",
                                    interests: "" + others + "",
                                    title: "" + $('#title option:selected').val() + "",
                                    email: "" + $('#email').val() + "",
                                    jobtitle: "" + $('#job_title').val() + "",
                                    company: "" + $('#company').val() + "",
                                    address: "" + $('#address').val() + "",
                                    pobox: "" + $('#pobox').val() + "",
                                    town: "" + $('#town').val() + "",
                                    city: "" + $('#city').val() + "",
                                    postcode: "" + $('#postcode').val() + "",
                                    country: "" + $('#countrylist').val() + "",
                                    phone: "" + $('#phone').val() + "",
                                    fax: "" + $('#fax').val() + "",
                                    standoptions: "" + $('input[name=stand_options]:checked').val() + "",
                                    size: "" + $('#size').val() + "",
                                    type: "" + $('#type').val() + "",
                                    comments: "" + $('#comments').val() + ""
                                },
                                url: "data/mail.ashx",
                                success: function (data) {
                                    if (data == 'true') {
                                        $('#book_stand').hide('slow');
                                        $('#outcome').html('<h1> Thank You </h1>');
                                    } else {
                                        $(this).removeAttr("disabled");
                                        alert('Failure to Process form..');
                                    }
                                }
                            });
                        } else {
                            $(this).removeAttr("disabled");
                        }
                    });
                }); break;
                case "Register": loadContent('data/register.htm', function () {
                    $('#reg_form td:first-child').css('text-align', 'left');
                    $('#reg_form td:nth-child(2)').css('text-align', 'left');
                    $.ajax({
                        url: "data/countries.ashx",
                        dataType: 'json',
                        success: function (data) {
                            for (var i = 0; i <= data.length; i++) {
                                $('#countrylist').append('<option value="' + data[i].code + '"> ' + data[i].name + ' </option>');
                            }
                        }
                    });
                    //                    $('#loader').hide();
                    $('#submit').click(function () {
                        $(this).attr('disabled', 'disabled');
                        registervalid = true;
                        var busiact; var inter; var constring;
                        $('#busyactivity input:checked').each(function () {
                            if ($(this).val() == 'Other1') {
                                busiact += $('#other1').val() + " <br /> ";
                            } else {
                                busiact += $(this).val() + " <br /> ";
                            }
                        });
                        $('#interests input:checked').each(function () {
                            inter += $(this).val() + " <br /> ";
                        });
                        $('#construct input:checked').each(function () {
                            if ($(this).val() == 'Other2') {
                                constring += $('#other2').val() + " <br /> ";
                            } else {
                                constring += $(this).val() + " <br /> ";
                            }
                        });

                        $('#reg_form td:first-child').css('color', 'black');
                        reg_validate();
                        if (registervalid) {
                            $.ajax({
                                type: "POST",
                                contentType: "application/x-www-form-urlencoded",
                                data: { firstname: "" + $('#first_name').val() + "",
                                    lastname: "" + $('#last_name').val() + "",
                                    title: "" + $('#title option:selected').val() + "",
                                    email: "" + $('#email').val() + "",
                                    jobtitle: "" + $('#job_title').val() + "",
                                    company: "" + $('#company').val() + "",
                                    address: "" + $('#address').val() + "",
                                    address2: "" + $('#address2').val() + "",
                                    pobox: "" + $('#pobox').val() + "",
                                    town: "" + $('#town').val() + "",
                                    country: "" + $('#countrylist').val() + "",
                                    phone: "" + $('#phone').val() + "",
                                    fax: "" + $('#fax').val() + "",
                                    jobfunction: "" + $('#jobfunction option:selected').val() + "",
                                    busyact: "" + busiact.substr(9) + "",
                                    interests: "" + inter.substr(9) + "",
                                    construct: "" + constring.substr(9) + "",
                                    spending: "" + $('#project_spend option:selected').val() + "",
                                    hear: "" + $('#hear option:selected').val() + ""
                                },
                                url: "data/registration.ashx",
                                success: function (data) {
                                    if (data == 'true') {
                                        $('#reg_form').hide('slow');
                                        $('#outcome').html('<h1> Thank You </h1>');
                                    } else {
                                        $(this).removeAttr("disabled");
                                        alert('Failure to Process form..');
                                    }
                                }
                            });
                        } else {
                            $(this).removeAttr("disabled");
                        }
                    });
                }); break;
                case "Sponsorship Opportunities": loadContent('data/exhibit.htm #sponsorshipopportunities'); break;
                case "Venue & Opening Times": loadContent('data/exhibit.htm #venue_openingtimes'); break;
                case "Exhibitor manual": loadContent('data/manual.htm', function () {
                    $('table td').css('text-align', 'left');
                }); break;
                case "Download Centre": loadContent('data/exhibit.htm #downloadcentre'); break;
                case "Video 2010": loadContent('data/video.htm', function () {
                    $('#vid1').slideDown('fast');
                }); break;
                case "Why Visit?": loadContent('data/visit.htm #whyvisit'); break;
                case "Venue & Opening Times": loadContent('data/visit.htm #venue_openingtimes'); break;
                case "Press Information": loadContent('data/press.htm #pressinformation'); break;
                case "Press Releases": loadContent('data/press.htm #pressreleases', function () {
                    $('#switch').click(function () {
                        if ($('#arabic').css('display') == 'none') {
                            $('#english').toggle('fast');
                            $('#arabic').toggle('fast');
                            $('#switch').html('Convert to English');
                        } else {
                            $('#english').toggle('fast');
                            $('#arabic').toggle('fast');
                            $('#switch').html('حول للعربية');
                        }
                    });
                }); break;
                case "Online Picture Gallery": loadContent('data/gallery.htm', function () {
                    $('#gallery img').click(function () {
                        var tmp = $(this).attr('src').substr($(this).attr('src').lastIndexOf('_') + 1); var img = tmp.substr(0, tmp.lastIndexOf('S'));
                        var src = 'img/gallery/L_' + img + '.jpg';
                        makeBig(src);
                    });
                }); break;
                case "Logos and Banners": loadContent('data/press.htm #logosandbanners'); break;
                case "Media Partners": loadContent('data/press.htm #mediapartners'); break;
                case "Hilal Events and Publications": loadContent('data/press.htm #hilaleventsandpublications'); break;
                case "Contact Us": loadContent('data/press.htm #contactus'); break;
                case "Travel Centre": loadContent('data/destinationb.htm #travelcentre'); break;
                case "Travel Air": loadContent('data/travel.htm #air'); break;
                case "Travel Road": loadContent('data/travel.htm #road'); break;
                case "Travel Water": loadContent('data/travel.htm #water'); break;
                case "Visa": loadContent('data/travel.htm #visa'); break;
                case "After Hours": loadContent('data/destinationb.htm #afterhours', function () {
                    $('#afterhours ul li').click(function () {
                        var tmp = 'ah' + $('#afterhours ul li').index($(this));
                        $('#afterhours div').hide();
                        $('#' + tmp).slideDown('slow');
                    });
                }); break;
                case "Vital Markets": loadContent('data/marketfeatures.htm #vitalmarkets'); break;
                case "Current Industry Newsfeeds":
                    $('#ajaxContent').html('<table><tr><td rowspan="4"><marquee direction=up height="300" scrollamount="1" scrolldelay="0" width="350"><iframe src="data/feedreader.asp" height="300" width="480" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe><br /><center></center></marquee></td><td> <img src="img/nf1.jpg" /> </td></tr><tr><td> <img src="img/nf2.jpg" /> </td></tr><tr><td> <img src="img/nf3.jpg" /> </td></tr><tr><td> <img src="img/nf4.jpg" /> </td></tr></table>');
                    break;
                case "Green Buildings Forum": loadContent('data/marketfeatures.htm #greenbuildingsforum'); break;
                case "Gala Dinner": loadContent('data/marketfeatures.htm #galadinner'); break;
                case "Networking Lunches": loadContent('data/marketfeatures.htm #networkinglunches'); break;
                case "Organisers": loadContent('data/contact.htm #organisers'); break;
                case "International Agents": loadContent('data/contact.htm #internationalagents'); break;
                default:
                    loadContent('data/exhibit.htm #home');
            }
        } else { } // Special Case if empty url is handled improperly
    });
    $(window).trigger('hashchange');
};

function makeGone() {
    $('#overlay').slideUp('slow', function () {
        $('#overlay').remove();
    });
}

function makeBig(mysrc) {
    var winY = $(window).height();
    var winX = $(window).width();
    $('#slider').append('<img id="overlay" style="position:relative; z-index: 100; display:none;" onclick="makeGone();" />');
    $('#overlay').attr('src', mysrc);
    $('#overlay').width('980px');
    $('html,body').animate({ scrollTop: 200 }, 1000);
    $('#overlay').css('left', 0);
    $('#overlay').slideDown('slow');
}

$('#regButton').click(function () {
    window.location.hash = '#!Register';
});

$('#bookButton').click(function () {
    window.location.hash = '#!Book a Stand';
});

function bookings_validate() {
    isEmpty($('#first_name')); isEmpty($('#last_name'));
    isEmpty($('#job_title')); isEmpty($('#company'));
    isEmpty($('#address')); isEmpty($('#pobox'));
    isEmpty($('#town')); isEmpty($('#city'));
    isEmpty($('#postcode')); isEmpty($('#phone'));
    isEmpty($('#fax')); isEmpty($('#size'));
    isEmpty($('#type'));
    if ($('#countrylist').val() == "no") {
        bookingsValid = false;
        $('#countrylist').parent().siblings().css('color', 'red');
    }
    if (echeck($('#email').val())) {
        bookingsValid = false;
        $('#email').parent().siblings().css('color', 'red');
    }
}

function reg_validate() {
    isEmpty($('#first_name')); isEmpty($('#last_name')); isEmpty($('#job_title'));
    isEmpty($('#company')); isEmpty($('#address')); isEmpty($('#address2')); isEmpty($('#pobox'));
    isEmpty($('#town')); isEmpty($('#phone')); isEmpty($('#fax'));
    if ($('#countrylist').val() == "no") {
        registervalid = false;
        $('#countrylist').parent().siblings().css('color', 'red');
    }
    if (echeck($('#email').val())) {
        registervalid = false;
        $('#email').parent().siblings().css('color', 'red');
    }
}

function isEmpty(brain) {
    if (brain.val() == "") {
        brain.parent().siblings().css('color', 'red');
        bookingsValid = false;
        registervalid = false;
    }
}

/* Email Validation */
function echeck(str) {

    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        alert("Invalid E-mail ID")
        return true
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        alert("Invalid E-mail ID")
        return true
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        alert("Invalid E-mail ID")
        return true
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        alert("Invalid E-mail ID")
        return true
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        alert("Invalid E-mail ID")
        return true
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        alert("Invalid E-mail ID")
        return true
    }

    if (str.indexOf(" ") != -1) {
        alert("Invalid E-mail ID")
        return true
    }

    return false
}
