﻿
$(document).ready(function()
{
    if ($("#homepage-flash").length)
    {
        // set the background image of the page.
        $("#content").css("background-image", "url(/Hard-Wood-Floor-Images/Homepage-Image.jpg)");
        
        var flashvars = { var1 : "1" };
        var params = { menu: "false", wmode: "transparent" };
        var attributes = { id: "APF", name: "APF" };

        swfobject.embedSWF("/Authentic-Pine-Floors-Flash-Movie.swf", "homepage-flash", "958", "434", "9.0.0","expressInstall.swf", flashvars, params, attributes);
    }
});


//
// Picture Viewer
//

var mainImageUrl = "";

$(document).ready(function () {

    $("#mainImage .right").click(function (e) {
        $(this).blur();
        e.preventDefault();

        $("#pictureLoading").show();

        var imgSrc = getFilename($("#mainImage img").attr("src"));

        var found = false;
        var isChanged = false;
        $("#thumbnailList img").each(function () {
            var thumb = getFilename($(this).attr("src"));

            if (found) {
                mainImageUrl = "/public/images/source/" + thumb;

                var desc = $(this).attr("Description");
                var ideaHome = $(this).attr("IdeaHome");

                preloadImage(mainImageUrl, desc, ideaHome, $(this));

                isChanged = true;

                return false;
            }

            if (thumb == imgSrc) {
                found = true;
            }
        });

        if (!isChanged && imgSrc == getFilename($("#mainImage img").attr("src"))) {
            var desc = $("#thumbnailList img:first").attr("Description");
            var ideaHome = $("#thumbnailList img:first").attr("IdeaHome");

            mainImageUrl = "/public/images/source/" + getFilename($("#thumbnailList img:first").attr("src"));

            preloadImage(mainImageUrl, desc, ideaHome, $("#thumbnailList img:first"));
        }
    });

    $("#mainImage .left").click(function (e) {
        $(this).blur();
        e.preventDefault();

        $("#pictureLoading").show();

        var lastImgSrc = getFilename($("#thumbnailList img:last").attr("src"));

        var imgSrc = getFilename($("#mainImage img").attr("src"));

        $("#thumbnailList img").each(function () {
            var thumb = getFilename($(this).attr("src"));

            if (thumb == imgSrc) {
                mainImageUrl = "/public/images/source/" + lastImgSrc;

                var desc = $(this).attr("Description");
                var ideaHome = $(this).attr("IdeaHome");

                preloadImage(mainImageUrl, desc, ideaHome, $(this));

                return false;
            }
            else {
                lastImgSrc = getFilename($(this).attr("src"));
            }

        });

    });

    $("#thumbnailList img").click(function () {
        $('html, body').animate({ scrollTop: 0 }, 'fast');

        $("#pictureLoading").show();

        mainImageUrl = "/public/images/source/" + getFilename($(this).attr("src"));

        var desc = $(this).attr("Description");
        var ideaHome = $(this).attr("IdeaHome");

        preloadImage(mainImageUrl, desc, ideaHome, $(this));
    });

    // Trigger the first image to be loaded.
    $("#thumbnailList img:first").trigger("click");
});

function preloadImage(imageUrl, desc, ideaHomeId, img)
{
    var finishId = $(img).attr("FinishId");
    var finishName = $(img).attr("FinishName");
    
    preload_image = new Image(948,400);
    //preload_image.onload = onPreload;
    preload_image.onload = function () {
        $("#mainImage img").attr("src", mainImageUrl);
        $("#imageOverlay span").html(desc);
        $("#pictureLoading").hide();

        if (ideaHomeId) {
            $("#ideaHomeLink").attr("href", "/Floors/View.aspx?FloorId=" + ideaHomeId).show();
        } else {
            $("#ideaHomeLink").hide();
        }

        if (parseInt(finishId) > 0) {


            $("#viewMoreFinishedFloors").html("View More " + finishName + " Floors &#187;").attr("href", "/Floors/View.aspx?FloorId=" + finishId).show();
            $("#viewMoreFinishedFloors2").html($("#viewMoreFinishedFloors").html()).attr("href", $("#viewMoreFinishedFloors").attr("href")).show();
        } else {
            $("#viewMoreFinishedFloors").hide();
            $("#viewMoreFinishedFloors2").hide();
        }
    };
    preload_image.src = imageUrl;
}

function getFilename(src) {
    var a = src.split('/');

    return a[a.length - 1];
}

//
// My Gallery
//

var postLoginEval = "";

function addToGallery(imageId)
{
    var url = "/ajax/AddToGallery.aspx";
    var dataUrl = "id=" + imageId;
    
    $.ajax({
       type: "GET",
       url: url,
       data: dataUrl,
       success: function(msg)
       {
            try
            {
                var r = eval(msg);
                
                if (r.HasError == "true")
                {
                    if (r.IsLoggedIn == "false")
                    {
                        postLoginEval = "addToGallery(" + imageId + ");";
                        triggerLogin();
                        
                    }
                }
                else
                {
                    $("#my-gallery-add").hide();
                    $("#my-gallery-confirm").show();
                    $("#my-gallery-confirm").click(function()
                    {
                        location.href = "/MyGallery.aspx";
                    });
                }
            }
            catch (ex)
            {
                if (confirm("Would you like to debug?"))
                {
                    alert(msg);
                    alert(url + "?" + dataUrl);
                    window.open(url + "?" + dataUrl);
                }
            }
       },
       error: function(XMLHttpRequest, textStatus, errorThrown)
       {
            if (confirm("Would you like to debug?"))
            {
                window.open(url + "?" + dataUrl);
            }
       }
     });
}

function removeImageFromGallery(imageId)
{
    var url = "/ajax/RemoveFromGallery.aspx";
    var dataUrl = "id=" + imageId;
    
    $.ajax({
       type: "GET",
       url: url,
       data: dataUrl,
       success: function(msg)
       {
            try
            {
                var r = eval(msg);
                
                if (r.HasError == "true")
                {
                    if (r.IsLoggedIn == "false")
                    {
                        postLoginEval = "removeImageFromGallery(" + imageId + ");";
                        triggerLogin();
                    }
                }
                else
                {
                    loadGallery(queryString("id"));
                }
            }
            catch (ex)
            {
                if (confirm("Would you like to debug?"))
                {
                    alert(msg);
                    alert(url + "?" + dataUrl);
                    window.open(url + "?" + dataUrl);
                }
            }
       },
       error: function(XMLHttpRequest, textStatus, errorThrown)
       {
            if (confirm("Would you like to debug?"))
            {
                window.open(url + "?" + dataUrl);
            }
       }
     });
}

function loadGallery(userId)
{
    var url = "/ajax/LoadGallery.aspx";
    var dataUrl = "id=" + userId;
    
    
    $.ajax({
       type: "GET",
       url: url,
       data: dataUrl,
       success: function(msg)
       {
            try
            {
                var html = new Array();
                var h = 0;
                
                var r = eval(msg);

                if (r.HasError == "true")
                {
                    if (r.IsLoggedIn == "false")
                    {
                        postLoginEval = "loadGallery(" + userId + ");";
                        triggerLogin();
                    }
                }
                else
                {
                    // Load Gallery
                    for (var i = 0; i < r.Products.d.length; i++)
                    {
                        html[h++] = "<h2>" + r.Products.d[i].ProductName + "</h2>";
                        
                        var product = r.Products.d[i];
                        
                        for (var j = 0; j < r.ProductImages.d.length; j++)
                        {
                            if (r.ProductImages.d[j].ProductId == product.ProductId)
                            {
                                var img = r.ProductImages.d[j];
                                html[h++] = "<div>";
                                html[h++] = "<a href=\"/Photos.aspx?CategoryID=" + img.ProductCategoryId + "&ProductID=" + img.ProductId + "&ProductImageId=" + img.ProductImageId + "\" class=\"gallery-product\"><img src=\"public/images/sized/176W/" + img.ImageUrl  + "\" alt=\"" + product.ProductName + "\" /></a>";

                                if (r.IsOwner == "true")
                                {
                                    html[h++] = "<a href=\"#\" onclick=\"removeImageFromGallery(" + img.ProductImageId + "); return false;\" class=\"note\">(x) Remove This Image</a>";
                                }
                                html[h++] = "</div>";
                            }
                        }
                        
                        html[h++] = "<br style=\"clear:both;\" />";
                    }
                }
                
                html[h++] = "<br style=\"clear:both;\" />";
                
                if (html.length > 0)
                {
                    $("#my-gallery").html(html.join(""));
                }
                else
                {
                    $("#my-gallery").html("<h3>No Samples In Your Gallery!</h3><p><a href='/OurProducts.aspx'>Click here</a> to view our products and build your gallery!</p><p>If you don’t want to build your own gallery now but need samples or additional information, please fill out the form below. Each Authentic Pine Floors’ expert has installed wide plank floors in their homes and has many years experience helping customers choose their dream floors.</p>");
                }
            }
            catch (ex)
            {
                if (confirm("Would you like to debug?"))
                {
                    alert(msg);
                    alert(url + "?" + dataUrl);
                    window.open(url + "?" + dataUrl);
                }
            }
       },
       error: function(XMLHttpRequest, textStatus, errorThrown)
       {
            if (confirm("Would you like to debug?"))
            {
                window.open(url + "?" + dataUrl);
            }
       }
     });
}



function triggerLogin()
{
    $.fn.colorbox({href:"MyGalleryLogin.htm", open:true});
}

function galleryLogin()
{
    var url = "/ajax/GalleryLogin.aspx";
    var dataUrl = "email=" + $("#login_email").val();
    
    $.ajax({
       type: "GET",
       url: url,
       data: dataUrl,
       success: function(msg)
       {
            try
            {
                var r = eval(msg);
                
                if (r.HasError == "true")
                {
                    alert(r.Error);
                }
                else
                {
                    ensureGalleryId(r.UserId);
                    
                    // Load Gallery
                    $.fn.colorbox.close();
                    
                    if (postLoginEval)
                    {
                        eval(postLoginEval);
                        postLoginEval = null;
                    }
                }
            }
            catch (ex)
            {
                if (confirm("Would you like to debug?"))
                {
                    alert(msg);
                    alert(url + "?" + dataUrl);
                    window.open(url + "?" + dataUrl);
                }
            }
       },
       error: function(XMLHttpRequest, textStatus, errorThrown)
       {
            if (confirm("Would you like to debug?"))
            {
                window.open(url + "?" + dataUrl);
            }
       }
     });
}

function galleryCreate()
{
    var url = "/ajax/GalleryCreate.aspx";
    var dataUrl = "email=" + $("#create_email").val();
    dataUrl += "&name=" + $("#create_name").val();
    
    $.ajax({
       type: "GET",
       url: url,
       data: dataUrl,
       success: function(msg)
       {
            try
            {
                var r = eval(msg);
                
                if (r.HasError == "true")
                {
                    alert(r.Error);
                }
                else
                {
                    ensureGalleryId(r.UserId);
                    
                    // Load Gallery
                    $.fn.colorbox.close();
                    
                    if (postLoginEval)
                    {
                        eval(postLoginEval);
                        postLoginEval = null;
                    }
                }
            }
            catch (ex)
            {
                if (confirm("Would you like to debug?"))
                {
                    alert(msg);
                    alert(url + "?" + dataUrl);
                    window.open(url + "?" + dataUrl);
                }
            }
       },
       error: function(XMLHttpRequest, textStatus, errorThrown)
       {
            if (confirm("Would you like to debug?"))
            {
                window.open(url + "?" + dataUrl);
            }
       }
     });
}


function showMoreInfo()
{
    $("#photo-info").toggle();
    //$("#photo-info").show("slide", { direction: "right"}, 1000);
    
    $("#photo-more-info").blur();
}

function ensureGalleryId(userId)
{
    if (location.href.indexOf("MyGallery") > -1 && (queryString("id") == null || queryString("id").length == 0))
    {
        location.href = "/MyGallery.aspx?id=" + userId;
    }
}


function showPicture()
{
    var ran = Math.floor(Math.random()*8) + 1;
    
    var imgUrl = "url(/Hard-Wood-Floor-Images/Image-0" + ran + "-Top.jpg) no-repeat";
    
    $("#picture-top").css("background", imgUrl);
    
    imgUrl = "url(/Hard-Wood-Floor-Images/Image-0" + ran + "-Bottom.jpg) no-repeat";
    
    $("#picture-bottom").css("background", imgUrl);
}


showPicture();

//
// Pull Down Navigation. Source: http://javascript-array.com/scripts/jquery_simple_drop_down_menu/
//

var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

function jsddm_open()
{	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').eq(0).show();}

function jsddm_close()
{	if(ddmenuitem) ddmenuitem.hide();}

function jsddm_timer()
{	closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{	if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}}

$(document).ready(function()
{	$('#jsddm > li').bind('mouseover', jsddm_open);
	$('#jsddm > li').bind('mouseout',  jsddm_timer);});

document.onclick = jsddm_close;

function queryString(key) 
{
    var hu = window.location.search.substring(1);
    var gy = hu.split("&");
    for (var i = 0; i < gy.length; i++) 
    {
        var ft = gy[i].split("=");
        if (ft[0].toLowerCase() == key.toLowerCase()) 
        {
            return ft[1];
        }
    }
    
    return "";
}
