// JavaScript Document

var isAdmin = 0;

function ChangeIsAdmin()
{
	if (isAdmin == 0)
	{
		isAdmin = 1;
		$(".mainSubContent_Articles").attr("style", "display: block");
	}
	else
	{
		isAdmin = 0;
		$(".mainSubContent_Articles").removeAttr("style");
	}
}

	function padContent() {
		if($(".preContent div.active")) {
			var filterHeight = $(".preContent div.active").height();
			$("div#pageContent.productsList").css("padding-top", filterHeight + 15);
			if($.browser.msie) {
			// IE (6 & 7) appears to like to apply the padding to child elements as well.
			// This code sets a negative top margin on child elements to offset this behavior
				var baseTopOffset = 0;
				$("#pageContent.productsList > .product").each(function() {
					var currentTopOffset = $(this).offset().top;
					if((currentTopOffset != baseTopOffset) && (baseTopOffset != 0)) {
						$(this).css("margin-top", (-1 * (filterHeight+15)));							
					} else {
						baseTopOffset = currentTopOffset;
					}
				});
			}
		}
	}


//var bannerArray = new Array("featuredPromotion","johnCharlesDesigns","featuredProducts");
var bannerArray = new Array("style1","style2","style3");
var bannerCounter = 0;
function getNewArticle(name){	
	var temp = bannerCounter;
	var tempName = name.replace("Link","");
	for(q=0 ; q < bannerArray.length; q++){
		if((bannerArray[q])== tempName){
			getArticle(q);
		}
	}
}
function getNextArticle(){
	if (isAdmin == 0)
	{
		bannerCounter = (bannerCounter + 1) % (bannerArray.length);
		getArticle(bannerCounter);
	}
}
function getArticle(number){
	$(".mainSubContent_Nav a").removeClass("selected");
	$("."+bannerArray[number]+"Link").addClass("selected");
	$(".mainSubContent_Articles").fadeTo(250,"0",function() {
		$(".mainSubContent div").removeClass("selected");
		$("."+bannerArray[number]).addClass("selected");
		$(".mainSubContent_Articles."+bannerArray[number]+".selected").fadeTo(250,"1.0", function() {});
		//alert(bannerArray[number]);
	});
	bannerCounter = number;
}
function getQueryStringParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}


jQuery(function ($) {
    var dealerId = getQueryStringParameterByName("dealerId");

    if (dealerId != "") {
        /*
        $.ajax({
            async: false,
            type: "GET",
            data: "mode=set&id=" + dealerId,
            url: "DealerDataHandler.ashx"
        });
        */

        // Hide dealer header control
        $('.locator').hide();
    }
    else {
        $.ajax({
            async: false,
            type: "GET",
            data: "mode=get",
            url: "/DealerDataHandler.ashx",
            success: function (data) {
                if (data != null && data != "")
                    $('.locator').hide();
            }
        });
    }

    $('.customizeButton').click(function () {
        track('Collection Details', 'click', 'Customize');
    });

    function getLeftPosition(object) {
        var backgroundPosition = $(object).css("background-position");
        if (!backgroundPosition) {
            var backgroundPosition = $(object).css("background-position-x");
        }
        var leftPosition = backgroundPosition.split(" ")[0];
        return (leftPosition);
    }
    $(".mainSubContent_Nav a").each(function () {
        $(this).click(function () {
            getNewArticle($(this).attr("class"));
            //clearInterval(timer);
            //timer = window.setInterval(getNextArticle, 10000);
        });
    });
    //var timer = window.setInterval(getNextArticle, 10000);

    /* Sets the backgound on the homepage for the flash content*/
    if ($("#pageContent.homepage").size() == 1) {
        $(".mainContent").removeClass("section");
    }
    /* seting the properties for the email freind window*/
    $(".emailAFriendPopUp").css({ top: '-50%' });
    $(".popUpContainer").css('opacity', 0);
    $(".emailAFriend").click(function () {
        var divHeight = 338;
        var fadeDivHeight = window.innerWidth;
        if ($.browser.msie) {
            divHeight = 360;
            fadeDivHeight = document.body.scrollHeight;
        }
        $(".popUpContainer").css('height', fadeDivHeight);
        $(".popUpContainer").fadeTo(200, ".5", function () {
            $(".emailAFriendPopUp").animate({ top: '50%', marginTop: -(.5 * divHeight) + "px" }, 'slow');
        });
    });
    $("#emailForm a").click(function () {
        $(".popUpContainer").fadeTo(200, "0", function () {
            var divHeight = 338;
            if ($.browser.msie) { divHeight = 360; }
            $(".emailAFriendPopUp").animate({ top: "200%" }, 'slow', '', function () {
                $(this).css('margin-top', -((2 * divHeight)) + "px").css('top', 0);
                $(".popUpContainer").fadeTo(200, ".0", function () {
                    $(this).css('height', 0);
                });
            });
        });
    });

    /*	$("#nav-products > li").each(function() {
    var innerLink = $(this).children("a:first");
    var leftPosition = getLeftPosition($(innerLink));
    var backgroundImage = $(innerLink).css("background-image");
    var linkWidth = $(innerLink).width();
    $(this).css("background", backgroundImage + " " + leftPosition + " 0 no-repeat").width(linkWidth);
    $(this).hover(function() {
    $(this).css("background", backgroundImage + " " + leftPosition + " -18px no-repeat");
    $(innerLink).css("margin-right", "1px").css("margin-left","1px");
    }, function() {
    $(this).css("background", backgroundImage + " " + leftPosition + " 0 no-repeat");
    $(innerLink).css("width", linkWidth +"px").css("margin-left","0");
    });
    $(this).children("a:first").css("background", "transparent none").css("border-bottom", "1px solid #fff");
    });
    */

    $(".sectionLink + ul > li > a").each(function () {
        //alert($(this).text());
        $(this).wrap("<nobr></nobr>");
    });

    $(".preContent > a").click(function () {
        $(".preContent *").each(function () {
            $(this).removeClass("active");
        });
        var targetClass = $(this).attr("class").split(" ")[0];
        $("." + targetClass).addClass("active");
        padContent();
        return false;
    });

    $("#pageContent.productDetails .information h2").each(function () {
        $(this).click(function () {
            $(this).toggleClass("expanded");
            $(this).next().toggle("fast");
        });
        $(this).next().addClass("collapsed");
    });

    var thumbs = $("#pageContent.productDetails .interaction .thumbsContainer img");
    var imgs = $("#pageContent.productDetails .interaction .imagesContainer img");

    $("#pageContent.productDetails .interaction .thumbsContainer img").each(function () {
        $(this).css("cursor", "pointer");
        $(this).click(function () {
            $(thumbs).removeClass("selected");
            $(this).addClass("selected");
            for (var i = 0; i < thumbs.length; i++) {
                if ($(thumbs).eq(i).hasClass("selected")) {
                    $(".imagesContainer img.selected").fadeOut(250, function () {
                        $(imgs).eq(i).fadeIn(250).addClass("selected");
                    }).removeClass("selected");
                    break;
                }
            }
        });
        /*$(this).css("cursor", "pointer");
        $(this).click(function() {
        var filename = $(this).attr("src").replace("_thumb","");
        $(".productShot").fadeTo(250,"0",function() {
        if(filename.match("001")) {
        $(this).attr("src", filename);
        // Remove the "else" option below - merely to offer up a placeholder for testing purposes
        } else {
        $(this).attr("src", "images/details_productPlaceholder.jpg");
        }
        $(this).fadeTo(250,"1.0", function() {});
        });
        });*/
    });
    padContent();
});

function redirect(url, idControl) {
    alert("href " + url + document.getElementById(idControl).value);
    
    window.location.href = url + document.getElementById(idControl).value;
}

function EmailLink(url) {
    //if ((url == null) || (url == ''))    
    url = document.URL;
    document.getElementById('blackScreen').style.display = 'block';
    document.getElementById('popoverBlock').style.display = 'block';
    var msg = new String;
    msg = document.getElementById('ctl00_emailThis_tbMessage').value;
    if (msg.indexOf('<url>') >= 0) msg.replace('<url>', url);
    else if (msg.indexOf(url)==-1) msg = msg + ' ' + url;
    document.getElementById('ctl00_emailThis_tbMessage').value = msg;
    document.getElementById('ctl00_emailThis_urlField').value = url;
    
}

function closeEmailForm() {
    document.getElementById('blackScreen').style.display = 'none';
    document.getElementById('popoverBlock').style.display = 'none';
}

function showAll(changeHash) {
    var i = 1;
    while (document.getElementById('productPage' + i)) 
    {
        document.getElementById('productPage' + i++).style.display="";
    }

    $(".divider bottom").attr("style", "");
    currentLink.className = '';
    currentLink = document.getElementById('all');
    currentLink.className = "selected";
    pageNum = 0;

    if (changeHash == null || changeHash)
	    document.location.hash = "#showAll";
 }

function hideAllBlocks() {
    var i = 1;
    while (document.getElementById('productPage' + i)) {
        document.getElementById('productPage' + i++).style.display = "none";
    }

    $(".divider bottom").attr("style", "display: none;");
}

var slider = null;

function scrollColor(dir) {
    var slider = document.getElementById('colorSlider');
    if (slider.offset == null) slider.offset = 0;
    slider.offset = slider.offset + dir * 10;
    if (slider.offset > 0) slider.offset = 0;
    if (slider.offset < 460 - slider.useWidth) slider.offset = 460 - slider.useWidth;
    slider.style.left = slider.offset + 'px';
}
