/// <reference path="jquery-1.4.1-vsdoc.js" />




//domain problemm (fixed 1-12-2010) by 3laa2

var currentURL = window.location.toString().toLowerCase();
if (currentURL.indexOf("acesegypt.com") > 0 && currentURL.indexOf("www") < 0) {
    var newurl;
    if (currentURL.indexOf("http://") == 0) {
        newurl = "http://www." + currentURL.substring(7);
    } else {
        newurl = "www." + currentURL;
    }
    window.location = newurl;
}



//tzbeet el location of footer share (3laa2)

$(function() {
    $(".cont3-down .twitter-share-button", ".footer").css("margin-left", "10px");

});

// subscribe to our news letter logic
$(function() {

    $(".textinput", "#subscribeForm").focusin(function(e) {
        if ($(this).val() == "Enter YOUR E-MAIL") { $(this).val("") }
    }).focusout(function(e) {
        if ($(this).val() == "") { $(this).val("Enter YOUR E-MAIL") }
    });
    $(".submitbutton", "#subscribeForm").click(function(e) {
        var analert = $("#subHint", "#subscribeForm");
        var mytext = $(".textinput", "#subscribeForm");
        if (analert.length == 0) { $("<span id='subHint' style='font-size:13px; display none'></span>").appendTo("#subscribeForm"); }
        if (mytext.val().match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i)) {
            $.ajax({
                url: "common-engine.php?do=subscribe",
                type: "POST",
                dataType: "json",
                data: { email: mytext.val() },
                success: function(data, statusText, XHR) {
                    var analert = $("#subHint", "#subscribeForm");
                    var mytext = $(".textinput", "#subscribeForm");
                    console.log(data);
                    if (data.status == "0") {
                        analert.html("&nbsp; Error occurred.").fadeIn(100).delay(2000).fadeOut('slow');
                    }
                    else {
                        analert.html("&nbsp;<br> Thanks for subscribing.").fadeIn(100).delay(3000).fadeOut('slow');
                        mytext.val("");
                    }
                }
            });
        } else {
            analert.html("&nbsp; Invalid Email.").fadeIn(100).delay(500).fadeOut('slow');
        }
        e.preventDefault();
    });
});


// contact us logic (ALAA) !!!

var slideContactUS = function(e) {
    $(".contactform").slideToggle("normal", function() {
        if ($("#pagehide").css("display") == "none") {
            $("#pagehide").fadeIn("slow");
            $(".top-menu .the-links").css({ backgroundColor: "#7C9C2D", "opacity": "1" });
            $("#pagehide").css("height", document.body.offsetHeight);
        }
        else {
            $("#pagehide").fadeOut("slow");
            $(".top-menu .the-links").css({ backgroundColor: "#333333", "opacity": "0.5" });
            window.location.hash = "";
        }
        if (e) { e.preventDefault(); }
        return false;
    });
}


$(function() {

    //auto focus , escape to close ARE still need eno yt3mlhom add
    $(".top-menu #close").click(slideContactUS);

    $("a[href=#contactus]").click(function() {
        $("body").animate({ scrollTop: 0 }, 'slow', function() {
            slideContactUS();
        });
    });
    var myhash = window.location.hash;
    if (myhash != "") {
        myhash = myhash.toString().substring(1, myhash.length);
        if (myhash == "contactus") { slideContactUS(); }
    }


    $("<div />", {
        id: "pagehide",
        css: {
            "position": "absolute",
            "top": "0px",
            "left": "0px",
            "width": "100%",
            "height": "100%",
            "background": "black",
            "opacity": "0.7",
            "z-index": "8999",
            "display": "none"
        }
    }).appendTo("body");


    var formValidation = function(theform) {
        var formNdefa = true;
        $("input[type=text], textarea", theform).keypress(function() {
            if ($(this).attr("id") == "Email") {
                if ($(this).val().match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i)) { $(this).css({ "border": "black 1px solid" }); }
            } else {
                $(this).css({ "border": "black 1px solid" });
            }
        }).each(function() {
            if ($(this).val() == "") {
                $(this).css({ "border": "red 1px solid" });
                formNdefa = false;
            }
        });
        var emailbox = $("#Email", theform);
        if (!emailbox.val().match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i)) {
            emailbox.css({ "border": "red 1px solid" });
            formNdefa = false;
        }
        return formNdefa;
    }


    $(".contactform form").bind("submit", { alaa: "alaa" }, function(e) {
        if (formValidation(this)) {
            $.ajax({
                url: "common-engine.php?do=contactus",
                type: "POST",
                dataType: "json",
                data: {
                    name: $("#name", this).val(),
                    email: $("#Email", this).val(),
                    message: $("#Message", this).val().replace(/\n/g, "<br>")
                },
                context: this,
                success: function(data, statusText, XHR) {
                    if (data) {
                        if (data.success == "yes") {
                            $("#name", this).val("");
                            $("#Email", this).val("");
                            $("#Message", this).val("");
                            $("#messageSent", this).fadeIn().delay(2000).fadeOut(function() {
                                slideContactUS();
                            });
                        }
                    }
                }
            });
        }
        e.preventDefault();
        return false;
    });
});

// end of Contact us LOGIC



// Main menu animation  (Alaa)
$(function() {
    $(".menu-txt .mainUL > li").hover(function() {
        $(this).children(".innerUL").stop(true, true).slideDown(200);
    }, function() {
        $(this).children(".innerUL").stop(true, true).slideUp(200);
    });
});





// social buttons  (Alaa)
$(function() {
    $(".top-container #socialBtns a")
                .mouseenter(function() {
                    $(".top-container #socialBtns #title").html(this.getAttribute("text"));
                    $(this).stop(true, true).animate({ "bottom": "-0px" }, 100, function() {
                        $(".top-container #socialBtns #title").fadeIn();
                    });
                })
                .mouseout(function() {
                    $(this).stop(true, true).animate({ "bottom": "-15px" }, 100, function() {
                        $(".top-container #socialBtns #title").fadeOut();
                    });
                });

});

// left feed animation (hady)
$(function() {

    $(".feed > div > div").html("");

    var feedflag = true;
    $(".feed img").click(function() {

        if ($(".feed > div > div").html() == "") {
            $(".feed > div > div").html('<iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Facesegypt&amp;width=500&amp;colorscheme=dark&amp;connections=0&amp;stream=true&amp;header=false&amp;height=427" scrolling="no" frameborder="0px" style="border: 0px; width: 500px; height: 400px;" allowtransparency="true"></iframe>');
        }
        if (feedflag) {
            $(this).fadeTo('slow', 1);

            $(".feed").stop();
            $(".feed").animate({ "left": "0px" }, 250, "linear");
            feedflag = false;
        }
        else {
            $(this).fadeTo('slow', 0.6);
            $(".feed").stop();
            $(".feed").animate({ "left": "-495px" }, 250, "linear");
            feedflag = true;
        }
    });
});

// Foooteeeeeeeer code >>>>> (hady)

$(function() {

    $(".footer .container1  .xeek").click(function() {
        $(".footer .container1 .xeekshadow").animate({ opacity: 0.3 }, 300);
        $(".footer .container1  .xeek").animate({ bottom: '20px' }, 200, "linear", function() {
            $(".footer .container1  .xeek").animate({ bottom: '-20px' }, 200);
            $(".footer .container1 .xeekshadow").animate({ opacity: 1 }, 200);
        });
    });
});




// ACES sponsors >>>>>>> (Alaa)


$(function() {
    var intervalDiff = 0;
    $(".sponsors .image_wrapper").filter(function(index) {
        return $('a', this).length != 1;
    }).each(function() {
        intervalDiff += 2000;
        var imgs = $(this).children("a");
        var currentImg = 0;
        var imgCount = imgs.length;

        // if (imgCount == 1) return false;

        imgs.not(":eq(0)").hide();
        window.setInterval(function() {
            imgs.not(":hidden").fadeOut("slow", function() {
                if (currentImg == imgCount - 1) { currentImg = 0 } else { currentImg++; };
                imgs.eq(currentImg).fadeIn("slow");
            });
        }, intervalDiff);
    });



    //$(".sponsors").click(function(e) { alert("aa") ; console.log(e); console.log(event) });


});
// end of ACES sponosrs LOGIC
