﻿
//
// 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;
    }
}


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);

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 "";
}