
var sl_map_ctrl=function(){var container=null;var map=null;var markers=[];var baseMarker='';this.mgr=null;var _map=this;this.init=function(){container=document.getElementById(sl_config.map.container);map=new GMap2(container);map.addControl(new GSmallMapControl());this.focus(sl_config.map.center.lat,sl_config.map.center.long,sl_config.map.zoom);var mgrOptions={borderPadding:50,maxZoom:15,trackMarkers:false};this.mgr=new MarkerManager(map,mgrOptions);return this;}
this.focus=function(lat,long,zoom){return map.setCenter(new GLatLng(lat,long),zoom);}
this.getMarkers=function(){return markers;}
this.add=function(o){GEvent.addListener(o,'click',function(){});markers.push(o);}
this.activate=function(){this.mgr.addMarkers(markers,8);this.mgr.refresh();}
this.clear=function(){markers=[];this.mgr.clearMarkers();this.mgr.refresh();}
this.select=function(){}
this.remove=function(){}
this.show=function(){return $(container).fadeIn('normal');};this.hide=function(){return $(container).hide();};}

var sl_list_ctrl=function(){var container=null;var _list=this;var tiles=null;this.init=function(){container=$('#'+sl_config.list.container+' ul');tiles=[];return container.empty();}
this.add=function(o){tiles[o.id]=o;return container.append(o.tile);}
this.clear=function(){tiles=[];return container.empty();}
this.show=function(){return container.fadeIn('normal');};this.hide=function(){return container.hide('normal');};}

function _log(m){if(typeof console!='undefined')
console.log(m);else
alert(m);}
var sl_page_ctrl=function(){this.data=null;this.tpl=null;this.list=null;this.map=null;this.info=null;this.pager=null;this.filter=null;this.test=false;var _page=this;var currentItem=null;var currentFiliaal=null;this.loader=null;this.mssgr=null;this.addMsg=function(message){this.mssgr.append('<br />'.message);}
this.setup=function(){this.data=new sl_mdl_data();this.tpl=new sl_template();this.list=new sl_list_ctrl();this.map=new sl_map_ctrl();this.pager=new sl_pager();this.filter=new sl_filter_ctrl();this.loader=$('#loader');this.mssgr=$('#msgs');this.showimage=$('#info #sl_show_image img.showimage');this.showimage.fadeOut();$(document).bind('SL_DATA_LOAD_DONE',function(e,data){e.preventDefault();e.stopPropagation();_page.next(e,data);});$(document).bind('SL_ITEM_SELECT',function(e,data){e.preventDefault();e.stopPropagation();_page.select(e,data);});$(document).bind('SL_TILE_MOUSEOVER',function(e,data){e.preventDefault();e.stopPropagation();_page.doMouse(e,data);});$(document).bind('SL_TILE_MOUSEOUT',function(e,data){e.preventDefault();e.stopPropagation();_page.doMouse(e,data);});$(document).bind('SL_FILTER_CHANGE',function(e,data){e.preventDefault();e.stopPropagation();_page.dofilter(e,data);});this.info=sl_info.init($('#'+sl_config.shopinfo.container)).clear();this.tpl.init();this.list.init();this.list.hide();this.map.init();this.map.hide();this.filter.init();this.filter.hide();var data={status:'stepping',next:'filialen'}
return $(document).trigger('SL_DATA_LOAD_DONE',data);};this.next=function(evt,data){if(data.next=='done')
return this.build();else
return this.data.load(data.next);}
this.build=function(){this.buildList();this.buildMap();this.buildFilters();this.buildPager();this.activate();return true;};this.activate=function(){this.map.activate();this.map.show();this.list.show();this.pager.show();this.loader.hide('normal');}
this.buildList=function(){this.data.items.each(function(i,item){item.tile=_page.tpl.get_tile(item,_page.data.filiaal.get(item.filialen[0]));item.select=function(){this.tile.addClass('select').removeClass('deselect');}
item.deselect=function(){this.tile.addClass('deselect').removeClass('select');}
_page.list.add(item);});};this.buildMap=function(){this.data.filiaal.each(function(i,filiaal){filiaal.icon=_page.tpl.get_icon(filiaal);filiaal.select=function(){this.icon.icn.image=sl_config.icon_on.image;}
filiaal.deselect=function(){this.icon.icn.image=sl_config.icon_off.image;}
_page.map.add(filiaal.icon.marker);});};this.buildFilters=function(){this.data.filters.winkels.each(function(i,item){_page.filter.addWinkel(i,item);});this.data.filters.plaatsen.each(function(i,item){_page.filter.addStad(i,item);});this.filter.build('steden');this.filter.build('winkels');this.filter.show();}
this.buildPager=function(){this.pager.init(sl_config.list.container,'li',sl_config.pager.divide,sl_config.pager.count);this.pager.build();this.pager.set(1);}
this.select=function(evt,o){var theItem=null;var theFiliaal=null;var theType=null;var items=null;if(currentFiliaal!=null){if(typeof currentFiliaal.winkel=='string'){currentFiliaal.deselect();}else{}}
if(typeof(o.winkel)=='string'){theType='filiaal';theItem=o.aanbiedingen;theFiliaal=o;}else if(typeof(o.filialen)=='object'){theType='filiaal';theFiliaal=_page.data.filiaal.get(o.filialen[0]);theItem=theFiliaal.aanbiedingen;}
currentFiliaal=theFiliaal;if(theType=='filiaal'){var item=null;items=[];currentFiliaal.select();$.each(theItem,function(i,aanbieding){item=_page.data.items.get(aanbieding);if(typeof item!='undefined')items.push(item.tile.clone());});this.info.clear().set(currentFiliaal,items);}
this.map.mgr.refresh();return true;};this.doMouse=function(e,data){switch(e.type){case'SL_TILE_MOUSEOVER':return this.tileMouseover(data);case'SL_TILE_MOUSEOUT':return this.tileMouseout(data);}
return false;}
this.tileMouseover=function(el){this.showimage[0].src=el.src.replace('_thumb','');this.showimage.fadeIn(1000)}
this.tileMouseout=function(el){this.showimage.fadeOut(1000,function(){return _page.showImageReset();});}
this.showImageReset=function(){this.showimage[0].src=sl_config.base.image;}
this.dofilter=function(e,data){var type=data.parentNode.id;switch(type){case'steden':return this.dofilter_steden(data);case'winkels':return this.dofilter_winkels(data);}
return false;}
this.dofilter_winkels=function(data){var current=(typeof data=='string')?data:data.parentNode.options[data.parentNode.selectedIndex].value;this.map.clear();var aanbiedingen=[];this.data.filiaal.each(function(i,filiaal){if(filiaal.winkel_id==current||current=='kies'){$.each(filiaal.aanbiedingen,function(i,aanbieding){if($.inArray(aanbieding,aanbiedingen)==-1)
aanbiedingen.push(aanbieding);});_page.map.add(filiaal.icon.marker);}});this.list.clear();this.data.items.each(function(i,item){if($.inArray(item.id,aanbiedingen)!=-1)
_page.list.add(item);});this.pager.clear();this.pager.build();this.pager.set(1);return this.activate();}
this.dofilter_steden=function(data){var current=data.parentNode.options[data.parentNode.selectedIndex].text;var latlong=null;var aanbiedingen=[];this.data.filiaal.each(function(i,filiaal){if(filiaal.plaats==current){latlong=[filiaal.latitude,filiaal.longitude];$.each(filiaal.aanbiedingen,function(i,aanbieding){if($.inArray(aanbieding,aanbiedingen)==-1)
aanbiedingen.push(aanbieding);});}});this.map.focus(latlong[0],latlong[1],sl_config.map.stadzoom)
this.list.clear();this.data.items.each(function(i,item){if($.inArray(item.id,aanbiedingen)!=-1)
_page.list.add(item);});this.pager.clear();this.pager.build();this.pager.set(1);return this.activate();}
this.clear=function(){currentItem=null;currentFiliaal=null;this.data.clear();this.list.clear();this.map.clear();this.info.clear();this.pager.clear();return true;};this.getData=function(){};}
var sl_info={container:null,adress:null,logo:null,aanbiedingen:null,init:function(o){this.container=o;this.title=$(o.find('.shoptitle'))
this.adress=$(o.find('.adress'));this.logo=$(o.find('.logoimg'));this.aanbiedingen=$(o.find('.aanbiedingen ul'))
return this;},set:function(o,items){var _inf=this;this.title.html(o.winkel);this.adress.html(o.adres+'<br />'+o.postcode+' '+o.plaats);this.logo.attr('src',o.logo);$(items).each(function(i,el){_inf.aanbiedingen.append(el);});this.container.swapClass('close','open');return this;},clear:function(){this.title.html(' ');this.adress.html(' ');this.logo.attr('src','');this.aanbiedingen.html(' ');this.container.swapClass('open','close');return this;}}
jQuery.fn.swapClass=function(search,replace){return $(this).removeClass(search).addClass(replace);}

var sl_httpreq=null;var sl_mdl_data=function(){var raw=null;var status=null;this.items=new sl_data();this.filiaal=new sl_data();this.filters={'winkels':new sl_data(),'plaatsen':new sl_data()}
var _mdl=this;var _fn=null;this.load=function(resource){_fn=eval('parse_'+resource);var result=$.ajax({url:sl_config.data.uri+resource,dataType:sl_config.data.type,username:sl_config.data.username,password:sl_config.data.password,success:_fn,error:load_error});}
var parse_filialen=function(data,status){data=(typeof(data)=='string')?eval('('+[data]+')'):data;_mdl.filiaal.load(data);var data={status:'success',next:'aanbiedingen'}
return $(document).trigger('SL_DATA_LOAD_DONE',data);}
var parse_aanbiedingen=function(data,status){data=(typeof(data)=='string')?eval('('+[data]+')'):data;_mdl.items.load(data);var data={status:'success',next:'filters'}
return $(document).trigger('SL_DATA_LOAD_DONE',data);}
var parse_filters=function(data,status){data=(typeof(data)=='string')?eval('('+[data]+')'):data;_mdl.filters.winkels.load(data.winkels);_mdl.filters.plaatsen.load(data.plaatsen);var data={status:'success',next:'done'}
return $(document).trigger('SL_DATA_LOAD_DONE',data);}
var load_error=function(XMLHttpRequest,status,errorThrown){if(XMLHttpRequest.statusText=="OK"){return _fn(XMLHttpRequest.responseText,XMLHttpRequest.statusText);}
status=XMLHttpRequest.status;raw=XMLHttpRequest;var data={status:'fail'}
return $(document).trigger('SL_DATA_LOAD_DONE',data);}
this.get_status=function(){return status;}
this.get_raw=function(){return raw;}
this.clear=function(){this.items.reset();this.filiaal.reset();return this;};}
var sl_data=function(){this.data=[];this.add=function(i,m){return this.data[i]=m;}
this.load=function(data){this.data=data;return true;}
this.get=function(i){i=(typeof i=='undefined')?false:i;if(i==false)
return this.data;else if(this.data[i]=='undefined')
return false;else
return this.data[i];}
this.reset=function(){this.data=[];}
this.hasdata=function(){return(this.data.length>0);}
this.length=function(){return this.data.length;}
this.lastIdx=function(){return this.data.length-1;}
this.each=function(fn){if(this.data.length==undefined){for(i in this.data){fn(i,this.data[i]);}
return true;}else{return $(this.data).each(function(i,item){if(typeof item!='undefined'){fn(i,item);}})};}}

var sl_item=function(o){this.titel=o.titel;this.prijs=o.prijs;this.omschrijving=o.omschrijving;this.startdatum=o.dt_start.split(' ');this.einddatum=o.dt_stop.split(' ');this.afbeelding=o.afbeelding;this.filialen=o.filialen
this.tile=null;this.id=o.id;this.select=function(){this.tile.addClass('select').removeClass('deselect');}
this.deselect=function(){this.tile.addClass('deselect').removeClass('select');}}

var sl_filiaal=function(o){this.winkel=o.winkel;this.winkel_id=o.winkel_id;this.filiaal=o.filiaal;this.long=o.longitude;this.lat=o.latitude;this.logo=o.logo;this.adres=o.adres+' '+o.huisnummer;this.postcode=o.postcode;this.plaats=o.plaats;this.aanbiedingen=o.aanbiedingen;this.id=o.filiaal_id;this.icon=null;this.select=function(){this.icon.setImage(sl_config.icon_on.image);}
this.deselect=function(){x=this.icon.setImage(sl_config.icon_off.image);}}

var sl_pager=function(){this.total=null;this.divideby=null;this.counter=null;this.pages=null;this.itemspp=null;this.itemcontainer=null;this.item=null;this.items=null;this.pagercontainer=null;this.pageritems=null;var current=null;var currentItem=null;var _pager=this;this.init=function(container,item,divideby,counter){this.itemcontainer=$('#'+container);this.hide();this.item=item;this.divideby=divideby;this.counter=counter;$(document).bind('SL_PAGER_ACTION',function(e,data){_pager.flip(e,data);});return this;};this.show=function(){return this.itemcontainer.show('normal');};this.hide=function(){return this.itemcontainer.hide('normal');};this.build=function(){this.items=this.itemcontainer.find(this.item);this.total=this.items.length;this.pages=(this.divideby=='pages')?this.counter:Math.ceil((this.total/this.counter));this.itemspp=Math.ceil(this.total/this.pages);$('#sl_paginator').append(this.get_pager());this.pageritems=$('.sl_pager li');return this;}
this.clear=function(){this.pagercontainer.empty();this.items=null;this.total=null;this.pages=null;this.itemspp=null;this.pageritems=null;return true;}
this.set=function(idx){if(current!=null)
$(".sl_pager li[value="+current+"]").addClass('deselect').removeClass('select');$(".sl_pager li[value="+idx+"]").addClass('select').removeClass('deselect');this.set_nav(idx);var lo=parseInt((idx-1)*this.itemspp);var hi=parseInt(idx*this.itemspp);for(p=0;p<=(this.total);p++){if(p<lo||p>=hi)
$(this.items[p]).removeClass(sl_config.list.classname.open).addClass(sl_config.list.classname.close);else
$(this.items[p]).removeClass(sl_config.list.classname.close).addClass(sl_config.list.classname.open);}
current=idx;if(current==this.pages){this.pagercontainer.find('.next, .last').swapClass(sl_config.list.classname.open,sl_config.list.classname.close);}else{this.pagercontainer.find('.next, .last').swapClass(sl_config.list.classname.close,sl_config.list.classname.open);}
if(current==1){this.pagercontainer.find('.first, .previous').swapClass(sl_config.list.classname.open,sl_config.list.classname.close);}else{this.pagercontainer.find('.first, .previous').removeClass(sl_config.list.classname.close,sl_config.list.classname.open);}
return false;}
this.set_nav=function(idx){this.pageritems.each(function(){if(this.value!=-1){var cname=(this.value!=idx&&((this.value>idx&&this.value>(idx+3))||(this.value<idx&&this.value<(idx-3))))?[sl_config.list.classname.add,sl_config.list.classname.remove]:[sl_config.list.classname.remove,sl_config.list.classname.add];$(this).swapClass(cname[0],cname[1]);}});if(idx>(1+3)){this.pagercontainer.find('.dots_in').swapClass(sl_config.list.classname.remove,sl_config.list.classname.add);}else{this.pagercontainer.find('.dots_in').swapClass(sl_config.list.classname.add,sl_config.list.classname.remove)}
if(idx<(this.pages-3)){this.pagercontainer.find('.dots_out').swapClass(sl_config.list.classname.remove,sl_config.list.classname.add);}else{this.pagercontainer.find('.dots_out').swapClass(sl_config.list.classname.add,sl_config.list.classname.remove)}}
this.flip=function(evt,o){if(o.flip=='page')
return this.set(o.dest);else if(o.flip=='jump'){switch(o.dest){case'next':return this.set(current+1);case'previous':return this.set(current-1);case'first':return this.set(1);case'last':return this.set(this.pages);default:return this.set(1);}}
return this;}
this.get_pager=function(){var o=this.pagercontainer=$('<ul>',{'class':'sl_pager'});for(var m=1;m<=this.pages;m++){li=$('<li>',{'class':'deselect'}).bind('click',function(n){var data={flip:'page',dest:this.value};return $(document).trigger('SL_PAGER_ACTION',data);}).text(m);o.append(li.attr('value',m));}
o.prepend($('<li>',{'class':'dots_in'}).html('..'));o.prepend($('<li>',{'class':'previous'}).bind('click',function(n){var data={flip:'jump',dest:'previous'};return $(document).trigger('SL_PAGER_ACTION',data);}).html('&lt;'));o.prepend($('<li>',{'class':'first'}).bind('click',function(n){var data={flip:'jump',dest:'first'};return $(document).trigger('SL_PAGER_ACTION',data);}).html('&lt;&lt;'));o.append($('<li>',{'class':'dots_out'}).html('..'));o.append($('<li>',{'class':'next'}).bind('click',function(n){var data={flip:'jump',dest:'next'};return $(document).trigger('SL_PAGER_ACTION',data);}).html('&gt;'));o.append($('<li>',{'class':'last'}).bind('click',function(n){var data={flip:'jump',dest:'last'};return $(document).trigger('SL_PAGER_ACTION',data);}).html('&gt;&gt;'));return o;}}

var sl_template=function(){var tpl={icon:null,tile:null};this.init=function(){tpl.tile=this.parse_tile();}
this.get_tile=function(o,filiaal){var dt_stop=this.parse_date(o.dt_stop.split(' ')[0]);var dt_start=this.parse_date(o.dt_start.split(' ')[0]);var tile=tpl.tile.clone(true);var thmb=(o.afbeelding.length>0)?o.afbeelding.replace('.jpg','_thumb.jpg'):sl_config.base.image;tile.find('.title').attr('id',o.id);tile.find('.title .content').text(o.titel);tile.find('.title .price').html('&euro; '+o.prijs);tile.find('.omschrijving .content').text(o.omschrijving);tile.find('.omschrijving .date').html('<br />van: '+dt_start+' tot: '+dt_stop);tile.find('.afbeelding_img').attr('src',thmb);tile.find('.winkel .content').text(filiaal.winkel);tile.find('.logo_img').attr('src',filiaal.logo);;return tile;}
this.parse_tile=function(){return $('<li>',{className:'border-start border-end close deselect'}).append($('<h4>',{className:'title',id:'tile'}).append($('<span>',{className:'content'}).text('title'),$('<span>',{className:'price'}).html('&euro; '+'0000')).bind('click',function(i){return $(document).trigger('SL_ITEM_SELECT',this);}),$('<p>',{className:'omschrijving'}).append($('<span>',{className:'content'}).text('omschrijving'),$('<span>',{className:'date'}).text('einddatum'),$('<span>',{className:'afbeelding'}).append($('<img>',{className:'afbeelding_img',src:sl_config.base.image,height:'64px',width:'64px'}).bind('mouseover',function(){return $(document).trigger('SL_TILE_MOUSEOVER',this);}).bind('mouseout',function(){return $(document).trigger('SL_TILE_MOUSEOUT',this);}))),$('<p>',{className:'winkel'}).append($('<span>',{className:'content'}).text('filiaal.winkel'),$('<span>',{className:'logo'}).append($('<img>',{className:'logo_img',src:sl_config.base.image}))));}
this.parse_date=function(s){var a=s.split('-');return a[2]+'-'+a[1];}
this.get_icon=function(o){return new sl_map_icon(o);var icn=new GIcon();icn.image=sl_config.icon.image;icn.iconSize=new GSize(sl_config.icon.width,sl_config.icon.height);icn.iconAnchor=new GPoint(parseInt(sl_config.icon.width/2),sl_config.icon.height);var point=new GPoint(parseFloat(sl_config.map.long),parseFloat(sl_config.map.lat));var marker=new GMarker(point,{icon:icn});GEvent.addListener(marker,'click',function(){return $(document).trigger('SL_ITEM_SELECT',o);});return marker;}}
var sl_map_icon=function(o){this.icn=new GIcon();this.icn.image=sl_config.icon.image;this.icn.iconSize=new GSize(sl_config.icon.width,sl_config.icon.height);this.icn.iconAnchor=new GPoint(parseInt(sl_config.icon.width/2),sl_config.icon.height);this.point=new GPoint(parseFloat(o.longitude),parseFloat(o.latitude));this.marker=new GMarker(this.point,{icon:this.icn});GEvent.addListener(this.marker,'click',function(){return $(document).trigger('SL_ITEM_SELECT',o);});}

sl_filter_ctrl=function(){var container=null;this.winkels_names=[];this.winkels={};this.steden_names=[];this.steden={};_filter=this;this.init=function(){container=$('#filter');}
this.addWinkel=function(i,s){if($.inArray(s,this.winkels_names)==-1)
this.winkels_names[i]=s;if(this.winkels[s]==undefined)
this.winkels[s]=i;return true;}
this.addStad=function(i,s){if($.inArray(s,this.steden_names)==-1)
this.steden_names[i]=s;return true;return this.steden[s].push(o);}
this.build=function(type){ar=this[type+'_names'];$('#filter_'+type).empty().append(this.get_filter(type,ar));}
this.get_filter=function(type,ar){var select=$('<select>',{name:type,id:type,size:1}).bind('change',function(i){return $(document).trigger('SL_FILTER_CHANGE',this);});select.append($('<option>',{value:'kies'}).text('kies:'));$(ar).each(function(i,name){if(typeof name!='undefined')
select.append($('<option>',{value:i}).text(name));});return $('<p>').append($('<label>',{'for':type}).text(type),select,$('<img>',{src:'/_ui/img/glossy_arrow.gif'}),$('<a>',{href:'#'}).text(' overzicht '+type));}
this.show=function(){return container.show('normal');};this.hide=function(){return container.hide('normal');};}

var sl_config={base:{image:"/_ui/img/mpx.gif"},data:{uri:"/getresource/",type:"json",username:'webuser',password:'w3bus3r'},map:{container:'sl_map',center:{lat:52.373058,long:4.89325},landcenter:{lat:52.129309,long:4.661843},zoom:12,stadzoom:12,landzoom:9},icon:{image:"/_ui/img/map/pin_off_24.png",width:20,height:37},icon_on:{image:"/_ui/img/map/pin_on_24.png",width:20,height:37},icon_off:{image:"/_ui/img/map/pin_off_24.png",width:20,height:37},list:{container:'sl_list',classname:{close:'close',open:'open',remove:'remove',add:'add'}},shopinfo:{container:'sl_shopinfo'},pager:{divide:'items',count:5}};

