﻿// Templates

Templates = {
    currentUrl: window.location.href.split('?')[0],
    rootUrl: "http://" + window.location.hostname, 
    position: 0,
    contributorId: 0,
    occasionId: 0,
    searchString: "",
    favourites: [],
    kidsOccasions: [],
    kidsSocialUrl: "/site/kids/default.aspx",
    browseUrl: "/site/event/browse.aspx",
    theme: 0,
    
    processSelection: function(position, count, occasion, homePage) {
        
        if (homePage)
        {
            Templates.currentUrl = Templates.currentUrl = Templates.rootUrl + Templates.browseUrl;
        }    
        if ($('input_dropdown_contributor_id'))
        {
            var contributorElement = $($('input_dropdown_contributor_id').value);
            Templates.contributorId = contributorElement.options[contributorElement.selectedIndex].value;
        }
        if (occasion)
        {
            Templates.occasionId = occasion;
        }
        else
        {
            if ($('input_dropdown_occasion_id'))
            {
                var occasionElement = $($('input_dropdown_occasion_id').value); 
                Templates.occasionId = occasionElement.options[occasionElement.selectedIndex].value;
            }
        }
        if ($('input_search'))
        {
            var searchElement = $($('input_search').value);
            Templates.searchString = searchElement.value;
        }
        if ($('theme_id'))
        {
            Templates.theme = $('theme_id').value;
        }
        if (position)
        {
            Templates.position = position;
        }
        if ($('kidsOccasions'))
        {
            Templates.kidsOccasions = $('kidsOccasions').value.split(",");
            if (Templates.kidsOccasions.find(function(s){ return s == Templates.occasionId; }))
            { 
                Templates.currentUrl = Templates.rootUrl + Templates.kidsSocialUrl;
            }
        }
        
        // Create the new url
        var newLocation = Templates.currentUrl + "?invites=1"
        
        if(Templates.position != 0){
            newLocation += '&position=' + Templates.position;
        }
        if(Templates.occasionId != 0){
            newLocation += '&occasion=' + Templates.occasionId;
        }
        if(Templates.contributorId != 0){
            newLocation += '&contributor=' + Templates.contributorId;
        }
        if(count && count != 0) {
            newLocation += '&count=' + count;
        }
        if(Templates.searchString != "") {
            newLocation += '&search=' + Templates.searchString;
        }
        if(Templates.theme != 0) {
            newLocation += '&theme=' + Templates.theme;
        }
        window.location = newLocation;
    },
    
    tagItemAsFavourite : function(itemId) {
        
        if ( $('input_member_logged_in') && $('input_member_logged_in').value == 1 )
        {
            Templates.favourites = $('input_member_favourites').value.split(",");
    	    var index 		= Templates.favourites.indexOf(itemId);
		    var checkBox	= $("chkbox_" + itemId);
		    var checked		= checkBox.checked;
		    var tempId		= itemId;

		    // initialize ajax request
		    new Ajax.Request(
			    "/webservice/ajax/template.asmx/AddToFavourite",
			    {
				    method: "get",
				    parameters: { template_id: itemId },
				    onSuccess: function(transport) {

					    var response = new Ajax.Response(transport);

					    // evaluate response
					    if (!response.empty) {
					    	var sStatus = response.xml.firstChild.nodeValue;

						    // modify favourite templates array
						    if (!sStatus.empty() && sStatus != 0) {
						    	if (checked && index == -1) {
							    	Templates.favourites.push(tempId);
							    } else if (!checked && index != -1) {
								    Templates.favourites.splice(index, 1);
						    	}
						    } else {
						    	alert("You cannot tag any more invites as favourites");
						    	checkBox.checked = false;
					    	}
				    	}

			    	} // end function: onSuccess
			    }
		    ) // end object: Ajax.Request
		}
	},
	
    templateSelect: function(itemId) {
    
        var query = window.location.href.split('?')[1];
        if (query)
        {
            location.href = window.location.href + "&template_id=" + itemId;
        }
        else
        {
            location.href = window.location.href + "?template_id=" + itemId;
        }                       
	}
}


function ReflowAndScroll()
{
    // force reflow of invites to avoid mozilla display bug
    var body = document.getElementsByTagName( "body" )[0];
    var bodyClass = body.className;
    body.className = "reflow";
    body.className = bodyClass;
        
    //scroll to invites or answers
    if(window.location.href.indexOf('invites=') != -1)
    {
        var inviteDiv = $('invites');
        if (inviteDiv)
        {
            //scroll to invite
            inviteDiv.scrollTo();
        }
    } 
    else if (window.location.href.indexOf('answers=') != -1)
    {
        var answersDiv = $('myanswers');
        if (answersDiv)
        {
            answersDiv.scrollTo();
        }
    }   
}

// Scrolls to invite list if requested.  Also attempts to fix mozilla bug which distorts the invite layout.
Event.observe(window, 'load', function() {
    setTimeout("ReflowAndScroll()", 1000);
});
