﻿addNamespace("Aspacts.Websites.Terstal.ContentControls.Store")

Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class = function()
{
    this.htmlElements = new Object()
    this.htmlElements.SearchForm = null
    this.htmlElements.ShopDetails = null
    this.htmlElements.SearchResults = null
    this.htmlElements.NavigationNext = null
    this.htmlElements.NavigationPrevious = null
    this.htmlElements.NavigationSeparator = null
    this.htmlElements.ShopSearchMessage = null
    
    this.htmlElements.tbxLocation = null
    this.htmlElements.lnkSearchShop = null

    this.currentSearchPoint = null
    this.intNumPages = 0
    this.intCurrentPage = 1
    this.intPageSize = 9
    this.arrShops = new Array()
    
    this.intInitialShopId = -1
    
}

Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.Initialize = function()
{


    this.htmlElements.ShopDetails.style.display="none"
    this.htmlElements.NavigationNext.style.display="none"
    this.htmlElements.NavigationPrevious.style.display="none"
    this.htmlElements.NavigationSeparator.style.display="none"   
    this.htmlElements.ShopSearchMessage.style.display="none"

     Aspacts.Script.Utils.Core.ImplementEvent(this.htmlElements.NavigationNext)
     this.htmlElements.NavigationNext.refCallBack = this
	 this.htmlElements.NavigationNext.addEventListener('click', function(evt){ Aspacts.Idios3.I_CAP.Scripting.Core.CancelEvent(evt||window.event);this.refCallBack.ShowStoresNext()}, true)
	 
	 Aspacts.Script.Utils.Core.ImplementEvent(this.htmlElements.NavigationPrevious)
     this.htmlElements.NavigationPrevious.refCallBack = this
	 this.htmlElements.NavigationPrevious.addEventListener('click', function(evt){ Aspacts.Idios3.I_CAP.Scripting.Core.CancelEvent(evt||window.event);this.refCallBack.ShowStoresPrevious()}, true)
	 
	 Aspacts.Script.Utils.Core.ImplementEvent(this.htmlElements.lnkSearchShop)
     this.htmlElements.lnkSearchShop.refCallBack = this
	 this.htmlElements.lnkSearchShop.addEventListener('click', function(evt){ Aspacts.Idios3.I_CAP.Scripting.Core.CancelEvent(evt||window.event);this.refCallBack.FindShops()}, true)
	 
	 Aspacts.Script.Utils.Core.ImplementEvent(this.htmlElements.tbxLocation)
     this.htmlElements.tbxLocation.refCallBack = this
	 this.htmlElements.tbxLocation.addEventListener('keydown', function(evt){this.refCallBack.HandleKeyDown(evt||window.event)}, true)
	   
	   
    this.LoadShops()
}


Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.HandleKeyDown = function(evt)
{
	window.status = evt.keyCode
	if (evt.keyCode==14 || evt.keyCode==13)
	{
		Aspacts.Idios3.I_CAP.Scripting.Core.CancelEvent(evt);
		this.FindShops()
		return false
	}
}

Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.FindShops = function()
{
	if(this.htmlElements.tbxLocation.value.trim() == "")
	{
		alert('Voer uw postcode in')
		return
	}
	
	
	
		var geocoder = new GClientGeocoder();
	 
	
	geocoder.getLatLng( this.htmlElements.tbxLocation.value + " Nederland " , this.FindShopsCallBack.bind(this) )  
		
	
	
}

Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.FindShopsCallBack = function(point)
{
	if (!point) 
	{        
		alert("De door u ingevoerde locatie werd niet herkend. Pas uw invoer aan en probeer het nogmaals.");      
	} 
	else 
	{        
		this.currentSearchPoint = point
		this.SortShops()
		this.RenderShops()
    		    
        // display message
        this.htmlElements.ShopSearchMessage.style.display=""  
	}    
}


Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.SortShops = function()
{
	 for (var i = 0;i<this.arrShops.length;i++)
     {
		this.arrShops[i].dblDistance = this.currentSearchPoint.distanceFrom(new GLatLng(this.arrShops[i].dblCoordLat, this.arrShops[i].dblCoordLong))
     }
     this.arrShops.sort(this.CompareShops)          
}

Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.CompareShops = function(Shop1, Shop2)
{
	var x = Shop1.dblDistance;
    var y = Shop2.dblDistance;
   return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.LoadShops = function()
{
    Aspacts.Websites.Terstal.Data.Store.Client.GetStores(this.LoadShopsCallBack.bind(this))
}

// Simple browser sniffing needed here
var isNav = (navigator.appName == "Netscape")
// Output list of properties for the object
function dumpProps(objName) {
var obj = eval(objName)
var msg = ""
var count = 0
var maxProps = 10
// Loop through properties of the object
for (var i in obj) {
if (i != "outerHTML" && i != "outerText" && i != "innerHTML" && i
!= "innerText" && i != "domain") {
msg += objName + "." + i + "=" + obj[i] + "\n"
if (count > maxProps) {
// Output a batch
if (isNav) {
java.lang.System.out.println(msg)
}
else {
alert(msg)
}
msg = ""
count = 0
continue
}
count++
}
}
// Output any leftovers
if (isNav) {
java.lang.System.out.println(msg)
} else {
alert(msg)
}
}

Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.LoadShopsCallBack = function(result)
{

    if (result && result.error)
	{
		alert(result.error.Type)
		return false
	}
	
    if (result && result.error && result.error.Message)
	{
		alert(result.error.Message)
		return false
	}
	else
	{
	    this.arrShops = result.value
	    
	   
	    this.RenderShops()  
	    //this.FindCurrentIndex()
	    //this.UpdateNavigation()
	 
	}
}


Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.ClearShops = function()
{

    while(this.htmlElements.SearchResults.childNodes.length > 0)
    {
        this.htmlElements.SearchResults.removeChild(this.htmlElements.SearchResults.childNodes[0])
    }
}

Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.RenderShops = function()
{
    this.ClearShops()
    
    var refUl = null
    var intPageNum = 0
     this.intCurrentPage = 1   
    for (var i = 0;i<this.arrShops.length;i++)
    {
        if((i%this.intPageSize)==0)
        {
            refUl = this.htmlElements.SearchResults.appendChild(document.createElement("ul"))   
            intPageNum++
        }
        
        if(this.arrShops[i].intId==this.intInitialShopId)
        {
			this.intInitialShopId=-1
			this.intCurrentPage = intPageNum
			this.ShowStoreDetails(this.arrShops[i])
        }
        
        //<li><a href="#" class="linkStyle" title="">Beverwijk</a></li>
        var storeElement = refUl.appendChild(document.createElement("li"))
        var storeLink = storeElement.appendChild(document.createElement("a"))
        storeLink.className="linkStyle"
        storeLink.href="#"
        storeLink.title=this.arrShops[i].strAddressCity
        storeLink.appendChild(document.createTextNode(this.arrShops[i].strAddressCity))
        if (this.currentSearchPoint)
			if (Math.floor(this.arrShops[i].dblDistance/1000)>10)
				storeLink.appendChild(document.createTextNode(" (" + Math.floor(this.arrShops[i].dblDistance/1000) + " km)"))
			else
				storeLink.appendChild(document.createTextNode(" (" + Math.floor(this.arrShops[i].dblDistance/100)/10 + " km)"))
		storeLink.refCallBack = this
        storeLink.shopItem = this.arrShops[i]
        
        Aspacts.Script.Utils.Core.ImplementEvent(storeLink)
	   storeLink.addEventListener('click', function(evt){ Aspacts.Idios3.I_CAP.Scripting.Core.CancelEvent(evt||window.event);this.refCallBack.ShowStoreDetails(this.shopItem)}, true)
	   
	   var storeAddress = storeElement.appendChild(document.createElement("p"))
	   storeAddress.appendChild(document.createTextNode(this.arrShops[i].strAddressStreet))
    }
    
    this.intNumPages = Math.ceil(this.arrShops.length/this.intPageSize)
    
   
    
    
    this.UpdateNavigation()
    
}

Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.UpdateNavigation = function()
{
    this.htmlElements.NavigationPrevious.style.display=this.intCurrentPage > 1 ? "": "none"
    this.htmlElements.NavigationNext.style.display=this.intCurrentPage < this.intNumPages ? "": "none"
    
    if (this.intCurrentPage > 1 && this.intCurrentPage < this.intNumPages )
    {
        this.htmlElements.NavigationSeparator.style.display =  ""
    }
    else
    {
        this.htmlElements.NavigationSeparator.style.display =  "none"
    }
    
    var intMarginLeft = 0-((this.intCurrentPage-1) * 200)
    this.htmlElements.SearchResults.style.marginLeft = intMarginLeft + "px"
       
}

Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.ShowStoresNext = function()
{
    if(this.intCurrentPage < this.intNumPages)
    {
        this.intCurrentPage++
        this.UpdateNavigation()
    }
}

Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.ShowStoresPrevious = function()
{
     if(this.intCurrentPage > 1)
    {
        this.intCurrentPage--
        this.UpdateNavigation()
    }
}


Aspacts.Websites.Terstal.ContentControls.Store.ShopSearch_class.prototype.ShowStoreDetails = function(storeItem)
{
    this.htmlElements.SearchForm.style.display="none"
    this.htmlElements.ShopDetails.style.display=""
    
    
    
    //clear existing items
    while(this.htmlElements.ShopDetails.childNodes.length > 0)
    {
        this.htmlElements.ShopDetails.removeChild(this.htmlElements.ShopDetails.childNodes[0])
    }
    
     //<h1 class="filiaal">TerStal Duiven</h1>
	var title =  this.htmlElements.ShopDetails.appendChild(document.createElement("h1"))   
    title.className = "filiaal"
    title.appendChild(document.createTextNode(storeItem.strAddressCity))
     
	if (storeItem.strEditUrl != "")
    {
		var linkEdit = this.htmlElements.ShopDetails.appendChild(document.createElement("a"))
		linkEdit.href = storeItem.strEditUrl
		linkEdit.appendChild(document.createTextNode("wijzigen"))
	}
	
	if (storeItem.strDeleteUrl != "")
    {
		if (storeItem.strEditUrl != "")
		{
			this.htmlElements.ShopDetails.appendChild(document.createTextNode(" | "))      
		}
		
		var linkDelete = this.htmlElements.ShopDetails.appendChild(document.createElement("a"))
		linkDelete.href = storeItem.strDeleteUrl
		linkDelete.appendChild(document.createTextNode("verwijderen"))
		linkDelete.onclick = function(){return confirm('Weet u zeker dat u dit filiaal wilt verwijderen?')}
	}
	
 
	//   <p>
	//       Dorpsplein 3 | 3456 KL | Tel: 032 12 34 567 
	//   </p>
	var address =  this.htmlElements.ShopDetails.appendChild(document.createElement("p"))   
	address.appendChild(document.createTextNode(storeItem.strAddressStreet + " | " + storeItem.strAddressZipCode + " | Tel: " + storeItem.strPhoneNumber))
	 

	//   <div class="searchParameters">
	//       <label class="lbl">Openingstijden:</label>ma t/m vr: 09.00 - 18.00<br />
	//       <label class="lbl">&nbsp;</label>di: gesloten<br />
	//       <label class="lbl">&nbsp;</label>za: 09.00 - 17.00<br />
	//       <br />
	//       <div class="shopSlogan">Elke donderdag en vrijdag koopavond!</div>
	//       <img src="images/shopPhoto.jpg" alt="" /> <!-- width:250px; height: proportioneel -->
	//   </div>

	var searchParams =  this.htmlElements.ShopDetails.appendChild(document.createElement("div"))   
	searchParams.className = "searchParameters"

	var lblOpeningHours1 = searchParams.appendChild(document.createElement("label"))   
	lblOpeningHours1.className="lbl"
	lblOpeningHours1.appendChild(document.createTextNode("Openingstijden"))      
	if (storeItem.strOpeningHours1 != "")
	{
		searchParams.appendChild(document.createTextNode(storeItem.strOpeningHours1))   
	}
	searchParams.appendChild(document.createElement("br"))

	if (storeItem.strOpeningHours2 != "")
	{
	  var lblOpeningHours2 = searchParams.appendChild(document.createElement("label"))   
	  lblOpeningHours2.className="lbl"
	  lblOpeningHours2.appendChild(document.createTextNode("\u00a0"))      
	  searchParams.appendChild(document.createTextNode(storeItem.strOpeningHours2))   
	  searchParams.appendChild(document.createElement("br"))
	}

	if (storeItem.strOpeningHours3 != "")
	{
	  var lblOpeningHours3 = searchParams.appendChild(document.createElement("label"))   
	  lblOpeningHours3.className="lbl"
	  lblOpeningHours3.appendChild(document.createTextNode("\u00a0"))      
	  searchParams.appendChild(document.createTextNode(storeItem.strOpeningHours3))   
	  searchParams.appendChild(document.createElement("br"))
	}
	searchParams.appendChild(document.createElement("br"))

	if (storeItem.strSlogan != "")
	{
	var sloganCont = searchParams.appendChild(document.createElement("div"))   
	sloganCont.className="shopSlogan"
	sloganCont.appendChild(document.createTextNode(storeItem.strSlogan))
	}

	if (storeItem.strImageUrl != "")
	{
	var imageItem = searchParams.appendChild(document.createElement("img"))   
	imageItem.src = storeItem.strImageUrl
	}
      
}




