﻿function HomepagePageLoad() {
    DecoPac.AjaxEntry.HomepagePageLoad(HomepagePageLoad_callback);
}
function HomepagePageLoad_callback(res) {
    if (res.error && res.error.Message != 'Unknown')
        alert(res.error.Message);
    else {
        //do something
        var xRes = $(res.value).find("Images");
        var c = "";
        c = xRes.find("FeaturedImages");
        $("#divFeaturedImages").prepend(c.text());
        c = xRes.find("NewImages");
        $("#divNewImages").prepend(c.text());
        c = xRes.find("PopularImages");
        $("#divPopularImages").prepend(c.text());

        InitHomeSliders();
        homeModuleLeftLink();
    }
}

function InitHomeSliders() {
	var container1 = $('div.sliderGalleryFeatured');
	var container2 = $('div.sliderGalleryNew');
	var container3 = $('div.sliderGalleryPopular');
	var ul1 = $('ul', container1);
	var ul2 = $('ul', container2);
	var ul3 = $('ul', container3);

	var itemsWidth1 = ul1.innerWidth() - container1.outerWidth();
	var itemsWidth2 = ul2.innerWidth() - container2.outerWidth();
	var itemsWidth3 = ul3.innerWidth() - container3.outerWidth();

	$('.sliderFeatured', container1).slider({
		min: 0,
		max: itemsWidth1,
		handle: $('.handleFeatured'),
		stop: function (event, ui) {
			ul1.animate({ 'left': ui.value * -1 }, 500);
		},
		slide: function (event, ui) {
			ul1.css('left', ui.value * -1);

			var fullWidth = itemsWidth1;
			var pct = Math.min(ui.value / fullWidth, 1);
			var handleFeatured = $(this).children("div");
			var val = pct * ($(this).outerWidth() - handleFeatured.outerWidth());
			handleFeatured.css("left", val);
			window.log(handleFeatured.outerWidth() + ", " + fullWidth + ", " + pct);
		}
	});
	$('.sliderNew', container2).slider({
		min: 0,
		max: itemsWidth2,
		handle: '.handleNew',
		stop: function (event, ui) {
			ul2.animate({ 'left': ui.value * -1 }, 500);
		},
		slide: function (event, ui) {
			ul2.css('left', ui.value * -1);

			var fullWidth = itemsWidth2;
			var pct = Math.min(ui.value / fullWidth, 1);
			var handleFeatured = $(this).children("div");
			var val = pct * ($(this).outerWidth() - handleFeatured.outerWidth());
			handleFeatured.css("left", val);
			window.log(handleFeatured.outerWidth() + ", " + fullWidth + ", " + pct);
		}
	});
	$('.sliderPopular', container3).slider({
		min: 0,
		max: itemsWidth3,
		handle: '.handlePopular',
		stop: function (event, ui) {
			ul3.animate({ 'left': ui.value * -1 }, 500);
		},
		slide: function (event, ui) {
			ul3.css('left', ui.value * -1);

			var fullWidth = itemsWidth3;
			var pct = Math.min(ui.value / fullWidth, 1);
			var handleFeatured = $(this).children("div");
			var val = pct * ($(this).outerWidth() - handleFeatured.outerWidth());
			handleFeatured.css("left", val);
			window.log(handleFeatured.outerWidth() + ", " + fullWidth + ", " + pct);
		}
	});

        //to initialize sliders, all image divs must be on when page intially loads;
        //once loaded, then call the function to display one of the divs and hide the others so they don't show through the slider
        changeHomeImages('new');
    }

    function homeModuleLeftLink() {
        var link = ($('.homeModuleLeft .homeBrowseIdeas').children('a')).attr('href');
        $('.homeModuleLeft .container').click(function () {
            window.open(link);
        });
    }
