﻿

    function pageBottomAdminCheck() {
        $.ajax(
        {
            url: '/admin/admincheck',
            success: function (data) {
                if (jQuery.trim(data) == "True") window.location.href = '/admin';
                else $('#admininput').show();
            },
            error: function (a, b, c) {
                alert(b);
            }
        }
    );
}

function openAdmin(id) {

    $('.adminsection').hide();
    $('#' + id).fadeIn();
}

function validEmail(email) {

    var regex = new RegExp(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/);
    return regex.test(email);
}

/*
function addcomment() {
    var blogid = $('#blogcommentid').val();
    var name = $('#commentname').val();
    var ctext = $('#commenttext').val();
    $('#addcommentbutton').html('<img src="../Content/images/loading.gif" />');
    $('#addcommentbutton').removeAttr("onclick");
    $.ajax({
        url: 'blog/addcomment',
        type: 'POST',
        data: { 'blogid': blogid, 'name': name, 'ctext': ctext },
        success: function (data) {
            var newcomment = document.createElement('div');
            $(newcomment).css({ 'display': 'none' });
            $('#commentname').attr('disabled', 'true');
            $('#commenttext').attr('disabled', 'true');
            $(newcomment).html(data);
            $('#currentblogcomments').append(newcomment);
            $('#addcommentbutton').html('Comment Added');
            $(newcomment).fadeIn()
        }
    });
}

*/

$(document).ready(function(){

  $(".rounded-img, .rounded-large").load(function() {
    $(this).wrap(function(){
      return '<span class="' + $(this).attr('class') + '" style="background:url(' + $(this).attr('src') + ') no-repeat center center; width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px; display: block;" />';
    });
    $(this).css("opacity","0");
  });

});

$(function () {
    $(window).scroll(function () {
        var top = document.body.scrollTop
                || window.pageYOffset
                || (document.body.parentElement
                    ? document.body.parentElement.scrollTop
                    : 0
                    );
        if (top > 310) {
            $('#listheading2').css({ 'opacity':'1', 'display':'block' });
        }
        else {
            $('#listheading2').css({ 'opacity': '0', 'display': 'none' });
        }
    });
});
var fnum = 0;
var flickp = new Array();
$(function () {
    if ($('.flickr').length > 0) {
        $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=75561519@N07&lang=en-us&extras=url_s&per_page=20&format=json&jsoncallback=?",
                    function (data) {
                        $.each(data.items, function (i, item) {
                            flickp.push(item.media.m + "^" + item.link);
                        });
                        showFlickr();
                    });
    }
});

function showFlickr() {
    var c = 0;
    if (fnum + 3 > flickp.length) fnum = 0;
    var nthree = fnum + 3;
    for (var i = fnum; i < nthree; i++) {
        var d = flickp[i].split("^");
            $('#fp' + c).html('').attr('href', d[1]).html('<img src="'+d[0]+'" />');
        c++;
    }
    fnum += 3;
    setTimeout('showFlickr()', 10000);
}


function adminAuth(page) {
    var newpage = "";
    var atext = $('#atext').val();
    $.ajax(
            {
                url: '/admin/admincheck',
                data: { 'key': atext },
                type: 'post',
                success: function (data) {
                    if (jQuery.trim(data) == 'True') {
                        if (page == true) window.location.href = "/admin";
                        else window.location.reload(true);
                    }
                    else {

                    }
                },
                error: function (a, b, c) {
                }
            }
        );

 }

 $(function () {
     $('#atext').keyup(function (e) {
         if (e.keyCode == 13) {
             adminAuth(true)
         }
     });
 });
 function clearInput(input) {
     var yo = $(input).val().indexOf("Your");
     if (yo == 0) {
         $(input).val("");
         $(input).css('color', '#000');
     }
}

function sendMail() {
    $('#emailreturn').html("");
    var name = escapeHtml($('#contactname').val());
    var email = escapeHtml($('#contactemail').val());
    var subject = escapeHtml($('#contactsubject').val());
    var message = escapeHtml($('#contactmessage').val());
    var regex = new RegExp(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/);
    if (name.length > 0 && name != "Your Name") {
        if (regex.test(email)) {
            if (subject.length > 0) {
                if (message.length > 0 && name != "Your Message") {
                    if (message.length < 3000) {
                        $('#contactsend').hide();
                        $('#emailreturn').html('<img src="/Content/images/loading.gif" />');
                        $.ajax(
                            {
                                url: '/contact/sendemail',
                                type: 'post',
                                data: { 'name': name, 'email': email, 'subject': subject, 'message': message },
                                success: function (data) {
                                    $('#emailreturn').css("color", "green");
                                    $('#emailreturn').html('<img src="/Content/images/icons/mail.png" /> Email sent');
                                    $('.contactright input').attr("disabled", "true");
                                    $('.contactright textarea').attr("disabled", "true");
                                }

                            }
                        );
                    }
                    else {

                        $('#emailreturn').append("Message too long");
                    }
                }
                else {
                    $('#emailreturn').append("Message too short");
                }
            }
            else {
                $('#emailreturn').append("Subject too short");
            }
        }
        else {
            $('#emailreturn').append("Please provide a valid email address");
        }
    }
    else { $('#emailreturn').append("Name too short"); }
}



function sendMailAnimal() {
    $('#emailreturn').html("");
    var name = escapeHtml($('#contactname').val());
    var email = escapeHtml($('#contactemail').val());
    var sinfo = $('#emailsub').val().split("~");
    var subject = sinfo[1] + " -  " + sinfo[2] + " - " + sinfo[3];
    var message = escapeHtml($('#contactmessage').val());
    var regex = new RegExp(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/);
    if (name.length > 0 && name != "Your Name") {
        if (regex.test(email)) {
            if (message.length > 0 && message != "Your Message") {
                if (message.length < 3000) {
                    $('#contactsend').hide();
                    $('#emailreturn').html('<img src="/Content/images/loading.gif" />');
                    $.ajax(
                        {
                            url: '/contact/sendemail',
                            type: 'post',
                            data: { 'name': name, 'email': email, 'subject': subject, 'message': message },
                            success: function (data) {
                                $('#emailreturn').css("color", "green");
                                $('#emailreturn').html('<img src="/Content/images/icons/mail.png" /> Email sent');
                                $('.contactright input').attr("disabled", "true");
                                $('.contactright textarea').attr("disabled", "true");
                            }

                        }
                    );
                }
                else {

                    $('#emailreturn').append("Message too long");
                }
            }
            else {
                $('#emailreturn').append("Message too short");
            }
        }
        else {
            $('#emailreturn').append("Please provide a valid email address");
        }
    }
    else { $('#emailreturn').append("Name too short"); }
}


$(function () {
    $('#searchgo').click(function () {
        var st = $('#search').val();
        search(st);
    });
});
$(function () {
    $('#search').keyup(function (e) {
        if (e.keyCode == 13) {
            var st = $('#search').val();
            search(st);
        }
    });
});

function search(searchterm) {
    window.location.href='/search/'+searchterm

}


function flickrpics(animal) {
    var images = Array();
    var url = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=b9b21192c400f22fe542f3e782c85436&tags=" + animal + "&safe_search=1&per_page=6&license=4,6,5,7";
    var src;
    return {
        getPics: function () {
            $.getJSON(url + "&format=json&jsoncallback=?", function (data) {
                var ic = 1;
                var cc = 1;
                $.each(data.photos.photo, function (i, item) {
                    if (ic == 1) {
                        var h = document.createElement("h1");
                        $(h).addClass("lowertitle flickrtitle");
                        $(h).html("Additional photos from <span>flick<span>r</span></span>.");
                        $('#flickrpics').append(h);
                        var p = document.createElement("p");
                        $(p).css("float", "left");
                        $(p).html('Below are some photos from flickr of this animal.' +
                        ' Please keep in mind these photos are for <strong>identification purposes only</strong>. The actual animals in stock may vary in size and appearance.<div class="clear"></div>');

                        $('#flickrpics').append(p);
                        var cl = document.createElement("div");
                        var clb = document.createElement("div");
                        $(cl).addClass("fcol");
                        $(cl).attr("id", "fc2");
                        $(clb).addClass("fcol");
                        $(clb).attr("id", "fc1");
                        $('#flickrpics').append(clb).append(cl);
                    }
                    var imgurl = "http://farm" + item.farm + ".static.flickr.com/" + item.server + "/" + item.id + "_" + item.secret + "_m.jpg";
                    var link = document.createElement("a");
                    $(link).attr({ 'href': 'http://www.flickr.com/photos/' + item.owner + '/' + item.id, "target": "_blank" });
                    var img = document.createElement("img");
                    $(img).attr({ 'src': imgurl }); 

                    link.appendChild(img);
                    if (ic%2==0) $('#fc2').append(link); else $('#fc1').append(link);
                    ic++;
                });
            });
        }
    }

}

function escapeHtml(unsafe) {
    return unsafe
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#039;")
}

$(function () {
    $('.thumbimage').click(function () {
        var tid = $(this).attr('id');
        var tc = thumbClick(tid);
        tc.swapDisplay();
        tc.swapThumb();
    });
});

$(function () {
    $('#displayimage').click(function () {
        var pop = displayImagePop(this);
        pop.showImage();
    });
});

function thumbClick(tid) {
    var url = $('#' + tid + ' > img').attr('src');
    url = url.replace("_t_", "_d_");
    var alt = $('#' + tid + ' > img').attr('alt');
    var durl = $('#displayimage').attr('src');
    var nurl = durl.replace("_d_", "_t_");
    var nalt = $('#displayimage').attr('alt');
    return {
        swapDisplay: function () {
            $('#miloading').fadeIn(function () {




                var img = new Image();

                $(img).load(function () {
                    $('#mainimage').fadeOut(function () {
                        //$('#mainimage').html("");
                        $('#displayimage').attr({ 'src': url, 'alt': alt });
                        $('#mainimage').fadeIn();
                        $('#miloading').fadeOut();
                    });
                }).attr({ 'src': url, 'id': 'displayimage', 'alt': alt });
            });
        },
        swapThumb: function () {
            var timg = new Image();

            $(timg).load(function () {
                $('#' + tid).fadeOut(function () {
                    $('#' + tid).html(timg);
                    $('#' + tid).fadeIn();
                });
            }).attr({ 'src': nurl, 'alt': nalt });
        }
    }
}

function displayImagePop(dimg) {
    var src = $(dimg).attr('src');
    var alt = jQuery.trim($(dimg).attr('alt'));
    var orig = src.replace("_d_", "_o_");
    return {
        showImage: function () {
            var img = new Image();
            $('#backgrounddark').fadeIn();
            var loading = document.createElement("div");
            $(loading).attr("id", "poploading");
            $(loading).html('<img src="/Content/images/loading.gif" />');
            $('#backgrounddark').append(loading);
            $(loading).fadeIn();

            $(img).load(function () {
                var owidth = img.width;
                var oheight = img.height;
                var width = img.width;
                var height = img.height;
                if (oheight > (Math.floor(document.body.clientHeight * 0.8))) {
                    height = Number(Math.floor(document.body.clientHeight * 0.8));
                    var scale = height / oheight;
                    width = Math.floor(owidth * scale);
                }
                if (owidth > (Math.floor(document.body.clientWidth * 0.8))) {
                    width = Number(Math.floor(document.body.clientWidth * 0.8));
                    var scale = width / owidth;
                    height = Math.floor(oheight * scale);
                }
                var mleft = width / 2;
                var mtop = height / 2;
                var imgin = document.createElement("div");
                $(imgin).css({ 'background': 'url(' + orig + ') center center no-repeat', 'width': '100%', 'height': '100%', 'backgroundSize': '100%' });
                $(loading).html("");
                var close = document.createElement("a");
                $(close).html("x");
                $(close).attr('id', 'closeimagepop');
                $(loading).append(close);
                $(loading).css({ 'height': height + 'px', 'width': width + 'px', 'marginLeft': '-' + (mleft + 15) + 'px', 'marginTop': '-' + mtop + 'px', 'padding': '10px' });
                $(loading).append(imgin);
                if (alt != undefined && alt.length > 3) {
                    var desc = document.createElement("div");
                    $(desc).html(alt);
                    $(desc).css({ width: '100%', padding: '5px 0', textAlign: 'center',
                        background: '#fff', background: 'rgba(255,255,255,0.7)',
                        color: '#484848', bottom: '30px', fontWeight: 'bold', height: '20px', position: 'relative'
                    });
                    $(loading).append(desc);
                }


            }).attr({ 'src': orig });
        }
    }
}

function paypalLoading() {
    $('#backgrounddark').fadeIn();
    var loading = document.createElement("div");
    $(loading).attr("id", "poploading");
    $(loading).css({ 'width': '180px', 'height': '150px', 'background':'#fff', 'padding':'10px' });
    $(loading).html('<img src="/Content/images/paypal.gif" /><br /><br /><center><img src="/Content/images/loading.gif" /></center>');
    $('#backgrounddark').append(loading);
    $(loading).fadeIn();
}

$(function () {
    $('#closeimagepop').click(function () {
        $('#backgrounddark').fadeOut(
            function () {
                $('#backgrounddark').html("");
            }
        );
    });
});

$(function () {
    $('#backgrounddark').click(function () {
        $(this).fadeOut(
            function () {
                $(this).html("");
            }
        );
    });
});

/*--------------------FRONT PAGE-------------------*/

function specials () {
    var spheights = new Array();


    return {

        getSpecialHeights: function () {
            $('.special').each(function () {
                spheights.push($(this).outerHeight(true));
            });
        },
        showSpecial: function (arrpos) {
            var scheight = 0;
            for (var i = 0; i < arrpos; i++) {
                scheight += Number(spheights[i]-10);
            }
            $('#scroller').css('marginBottom', '-' + scheight + 'px');
        }
    }
}


function showSpecial(scrollto) {
    var special = specials();
    special.getSpecialHeights();
    special.showSpecial(scrollto);

}

function scrollerTop(scam) {
    var newleft = Number(scam * 560);
    $('#mtrwrap').css('marginLeft', '-'+newleft + 'px');

}

function addToNewsletter() {
    $('#newsletterreturn').html('');
    var email = $('#newsletteremail').val();
    if (validEmail(email)) {
        $.ajax({
            url: '/newsletter/add/' + email,
            success: function (data) {
                if (data.indexOf("true") != -1) $('#newsletterreturn').html('E-mail added.');
                else $('#newsletterreturn').html('Unable to add email to mailing list.');
            }
        });
    }
    else {
        $('#newsletterreturn').html('Invalid email.');
    }
}

/*~~~~~~~~~~~~~~~~~~~~~ GALLERY ~~~~~~~~~~~~~~~~~~~~*/

function showGalPic(url) {

    var img = new Image();

    $(img).load(function () {
        $('#mainimage').fadeOut(function () {
            //$('#mainimage').html("");
            $('#mainimage').attr({ 'src': '/Content/aimages/'+url });
            $('#mainimage').fadeIn();
            $('#miloading').fadeOut();
        });
    }).attr({ 'src': '/Content/aimages/' + url, 'id': 'mainimage' });
}

function galLeft() {
    var clen = $('#length').css('marginLeft').length;
    var curml = $('#length').css('marginLeft').substr(0, clen - 2);
    if(curml <= -1) $('#length').css('marginLeft', Number(curml) + 720 + "px");
}
function galRight() {
    var clen = $('#length').css('marginLeft').length;
    var curml = $('#length').css('marginLeft').substr(0, clen - 2);
    if(curml >= -6800) $('#length').css('marginLeft', Number(curml) - 720 + "px");
}
