$(document).ready(function () {
    $("body").prepend(modal_html());


    
    $("#news_pop").jqm({
        trigger: '.newspop',
        closeClass: 'close',
        overlay: 50,
        zIndex: 9999
    });

    $("#thank_you").jqm({
        closeClass: 'close_action',
        overlay: 50
    });
    $("#thank_you_friend").jqm({
        closeClass: 'close_action',
        overlay: 50
    });

    $("#error").jqm({
        closeClass: 'close_action',
        overlay: 50
    });

    $(".get_notified").hover(
		function () {
		    this.src = this.src.replace("get_notified.png", "get_notified_hover.png");
		},
		function () {
		    this.src = this.src.replace("get_notified_hover.png", "get_notified.png");
		}
	);
    $(".cancel").hover(
		function () {
		    this.src = this.src.replace("cancel.png", "cancel_hover.png");
		},
		function () {
		    this.src = this.src.replace("cancel_hover.png", "cancel.png");
		}
	);
    $(".return_home").hover(
		function () {
		    this.src = this.src.replace("return_home.png", "return_home_hover.png");
		},
		function () {
		    this.src = this.src.replace("return_home_hover.png", "return_home.png");
		}
	);



    /* Newsletter signup */
    $("#get_notified").click(function () {
        var fullname = $(".newsletter_form").find("input[name='name']");
        var semail = $(".newsletter_form").find("input[name='email']");
        var fnamevalue = fullname.val();
        var emailvalue = semail.val();
        if (emailvalue && fnamevalue && emailvalue != "Your Email Address" && fnamevalue != "Your Name") {
            $.getJSON("http://www.illesfoods.com/illesfoods/newsletter_signup.aspx?email=" + emailvalue + "&fullname=" + fnamevalue + "&callback=?", function (myJson) {

                switch (myJson.mystatus) {
                    case "SUCCESS":
                        $('#thank_you').jqmShow();
                        $('#news_pop').jqmHide();
                        break;
                    case "INVALID_EMAIL":
                        modal_error("Invalid Email", "The email you entered is invalid, please try again.");
                        break;
                    case "INVALID_NAME":
                        modal_error("Invalid Name", "Please enter your First and Last Name with a space between them.");
                        break;
                    case "ERROR_MESSAGEAPI":
                        modal_error("Error", "An unknown error occurred while hitting the Messageing API.");
                        break;
                    case "ERROR_API":
                        modal_error("Error", "An unknown error occurred while hitting the API.");
                        break;
                    default:
                        modal_error("Error", "An unknown default error occurred.");
                        break;
                }
            });
            return false;

        } else {

            $("input#email").removeClass("newsletter_error")
            $("input#fullname").removeClass("newsletter_error")
            if (!semail.val() || semail.val() == "Your Email Address") {
                semail.val("Email Required");
                semail.addClass("newsletter_error");
            }
            if (!fullname.val() || fullname.val() == "Your Name") {
                fullname.val("Name Required");
                fullname.addClass("newsletter_error");
            }

            return false;
        }

    });






});

function modal_error(heading, paragraph) {
    $("#error").find("h2").text(heading);
    $("#error").find("p").text(paragraph);
    $('#error').jqmShow();
}

function modal_success(heading, paragraph, h2_class) {
    $("#thank_you").find("h2").text(heading);
    $("#thank_you").find("p").text(paragraph);
    if (h2_class) {
        $("#thank_you").find("h2").addClass(h2_class);
    }
    $('#thank_you').jqmShow();
}









//Mobile Redirection and Style
var mobi = ['opera', 'iemobile', 'webos', 'android', 'ipad', 'iphone', 'blackberry', 'safari'];
var midp = ['symbian', 'blackberry'];
var ua = navigator.userAgent.toLowerCase();
var desktop = '<!--[if lt IE 7]><link rel="stylesheet" type="text/css" href="' + baseurl  + 'App_Themes/illesfoods/m/ie.css" media="screen"/><![endif]-->';
if ((ua.indexOf('midp') != -1) || (ua.indexOf('mobi') != -1) || ((ua.indexOf('ppc') != -1) && (ua.indexOf('mac') == -1)) || (ua.indexOf('webos') != -1)) {
    document.write('<link rel="stylesheet" href="' + baseurl + 'App_Themes/illesfoods/m/allmobile.css" type="text/css" media="all"/>');
    if (ua.indexOf('midp') != -1) {
        for (var i = 0; i < midp.length; i++) {
            if (ua.indexOf(midp[i]) != -1) {
                document.write('<link rel="stylesheet" href="' + baseurl + 'App_Themes/illesfoods/m/' + midp[i] + '.css" type="text/css"/>');
            }
        }
    }
    else {
        if ((ua.indexOf('mobi') != -1) || (ua.indexOf('ppc') != -1) || (ua.indexOf('webos') != -1)) {
            for (var i = 0; i < mobi.length; i++) {
                if (ua.indexOf(mobi[i]) != -1) {
                    document.write('<link rel="stylesheet" href="' + baseurl + 'App_Themes/illesfoods/m/' + mobi[i] + '.css" type="text/css"/>');
                    break;
                }
            }
        }
    }
}
else {
    document.write(desktop);
}
if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
    document.write('<meta name="viewport" content="width=device-width" />');
}



function preloadImages(id) {
    var c = new Array();
    $(id + ' img').each(function (j) {
        c[j] = new Image();
        c[j].src = this.src;

        if ($.browser.msie) {
            this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='image',src='" + this.src + "')";
        }
    });

}


