﻿var mee = {
    bodyShim: $('#body-shim')
    ,enc: function(t) {
        return t.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
    }
    ,maps: {
        show: function(x, y, guid) {
            var map = new GMap2(document.getElementById("google_maps"));
            map.addControl(new GSmallMapControl());
            map.setCenter(new GLatLng(x, y), 13); 

            var icon = new GIcon();
            icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
            icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
            icon.iconSize = new GSize(12, 20);
            icon.shadowSize = new GSize(22, 20);
            icon.iconAnchor = new GPoint(6, 20);
            icon.infoWindowAnchor = new GPoint(5, 1);
            
            var point = new GLatLng(x, y);
            var marker = new GMarker(point, icon)
            map.addOverlay(marker); 
            
            GEvent.addListener(marker, "click", function() {
                $.ajax({
                    type: 'POST'
                    , url: 'WebService.asmx/SocialeKaart_GetOrganisatie'
                    , data: "{ guid: '" + guid + "'}"
                    , contentType: 'application/json; charset=utf-8'
                    , dataType: 'json'
                    , success: function(result) {
                        var x = function(t) {
                            if(t) {
                                return t.toString();
                            }
                            return '';
                        }
                        var json = eval('(' + result.d + ')');

                        var html = '<table><tbody>';
                        html += '<tr><td>Naam</td><td>' + x(json.OrganisationDetail.org_Naam) + '</td></tr>';
                        html += '<tr><td>Adres</td><td>' + x(json.OrganisationDetail.org_Huisadres.straat) + ' '  + x(json.OrganisationDetail.org_Huisadres.nummer) + x(json.OrganisationDetail.org_Huisadres.toevoeging) + '</td></tr>';
                        html += '<tr><td></td><td>' + x(json.OrganisationDetail.org_Huisadres.postcode) + ' ' +  x(json.OrganisationDetail.org_Huisadres.plaats) + '</td></tr>';
                        html += '<tr><td>Telefoon</td><td>' + x(json.OrganisationDetail.org_Tel1.netnummer) + ' ' + x(json.OrganisationDetail.org_Tel1.abonneenummer) + '</td></tr>';                        
                        html += '</tbody></table>';
                        
                        marker.openInfoWindowHtml(html);
                    }
                    , error: function(xhr, result) {
                        //TODO: general handler
                        alert('mislukt');
                    }
                });
		    });

        }
    }
	,zoeken : {
	    el: null
		,onfocus : function(el){
			el.addClass('focus');
			el.attr('value', '');
		}
		, defocus : function(el){
			if(el.attr('value') == '' || el.attr('value') == 'Typ hier de trefwoorden of vraag waar u naar zoekt'){
				el.attr('value', 'Typ hier de trefwoorden of vraag waar u naar zoekt');
				if(el.hasClass('focus'))	
					el.removeClass('focus');
			}
		}
		,keyup: function(v) {
		    if(mee.zoeken.el == null) {
                mee.zoeken.el = $('#zoeken-autoComplete');
            }
            
            mee.zoeken.el.show();
            
		    if(v.length >= 3) {
		        $.ajax({
                    type: 'POST'
                    , url: 'WebService.asmx/SearchSuggest'
                    , data: "{ search: '" + v + "'}"
                    , contentType: 'application/json; charset=utf-8'
                    , dataType: 'json'
                    , success: function(result) {
                        var json = eval('(' + result.d + ')');

                        var l = json.items.length;
                        var html = '';
                        
                        if (l > 0) {
                            html += '<ul>';
                            jQuery.each(json.items, function(i, item) {
                                var name = item.name;
                                var tekst = item.tekst;
                                var pagina = item.pagina;
                                
                                html += '<li><a href="' + pagina + '">' + name + '</a></li>';
                            });
                            html += '</ul>';
                        }
                        else {
                        
                        }
                        
                        mee.zoeken.el.html(html);
                    }
                    , error: function(xhr, result) {
                        //TODO: general handler
                        alert('mislukt');
                    }
                });
		    }
		    else {
		         mee.zoeken.el.html('');
		         mee.zoeken.el.hide();
		    }
		}
	}
	
	, flash: {
        params: {
            wmode: null
            , allowScriptAccess: "sameDomain"
            , allowFullscreen: true
        }
        , init: function(id, src, width, height, vars, wmode) {
            if (!vars)
                vars = {};

            if (wmode)
                this.params.wmode = wmode;
            else
                this.params.wmode = "transparent";

            vars.ws = mee.options.appPath + "/webservice.asmx?wsdl";            
            
            if(w4u.site.iframe() !== true) {
                swfobject.embedSWF(src, id, width, height, "9.0.0", false, vars, mee.flash.params);
            }
        }
    }	
	, speler: {
        load: function(film, still) {
            if ($('#flash-homePlayer').length == 1) {
                if(mee.options.appPath == 'http://meefriesland.synion.nl') {
                    mee.flash.init('flash-homePlayer', "src/swf/home.swf", 460, 380, {
                        film: mee.options.appPath + '/cms/Data/media/Video/Home/BERT home tekst.flv'
                        ,still: mee.options.appPath + '/cms/Data/media/Video/Home/BERT home_stil.flv'
                    });
                }
                else {
                    mee.flash.init('flash-homePlayer', "src/swf/home.swf", 460, 380, {
                        film: film
                        ,still: still
                    });	
                }		      
            }
            else {
                 mee.flash.init('flash-video-player', "src/swf/player.swf", 363, 249, {
                    film: film
                    ,still: still
                });	
            }	      
        }
    }
    , hover: {
        el: null
        , show: function(t) {
            if(mee.hover.el == null) {
                mee.hover.el = $('#woord-hover');
            }
            
            mee.hover.el.html('<div class="top"></div>' +
                '<div class="mid">' +
                    '<h4>'+ t.innerHTML + '</h4>' +                                
                '</div>' +
                '<div class="bot"></div>');                                    
                            
            var p = $(t).position();
            mee.hover.el.css("top", p.top + 15);                            
            mee.hover.el.css("left", p.left);
            
            mee.bodyShim.show().one("click", function() {
                mee.bodyShim.hide();
                mee.hover.el.hide();
            });
            
            mee.hover.el.show();
            
            $.ajax({
                type: 'POST'
                , url: 'WebService.asmx/SearchSuggest'
                , data: "{ search: '" + t.innerHTML + "'}"
                , contentType: 'application/json; charset=utf-8'
                , dataType: 'json'
                , success: function(result) {
                    var json = eval('(' + result.d + ')');

                    var item = json.items.length > 0 ? json.items[0] : null;
                    if(item != null) {                    
                        var html = '<div class="top"></div>' +
                            '<div class="mid">' +
                                '<h4>'+ item.name + '</h4>' +
                                '<p>'+ mee.enc(item.tekst) + '</p>' +
                                '<ul>' +
                                    '<li class="topItm">' +
                                        '<a href="'+ item.pagina + '">' + item.paginaTekst + '</a>' +
                                    '</li>' +
                                '</ul>';
                        if(item.extern) {
                            html += '<a class="agenda-more" href="' + item.extern + '" rel="external">' +
                                    '<strong>Meer informatie</strong>' +
                                    '<span>' + item.extern + '</span>  ' +                              
                                '</a>';
                        }
                        
                        html += '</div><div class="bot"></div>';   
                        
                        mee.hover.el.html(html).html();
                    }
                }
            });
        }
    }
};

var w4u = {
    site: {
        iframe: function() {
            return window.location != window.parent.location ? true : false;
        }
    }
};

$(document).ready(function() {
    $('a#specifiekeInfo').click(function() {
        var div = $('div#specifiekeInfoSub');
        div.slideToggle("normal");
        return false;
    });
    
    $('a#emailBoxBtn,a#emailBoxCloseBtn').click(function() {
        var div = $('div#emailBoxTarget');
        div.slideToggle("normal");
        return false;
    });
    
    $('a#chatBoxBtn').click(function() {
        window.open('http://livechat.boldchat.com/aid/298700765092244427/bc.chat?cwdid=1334397467855432992&amp;url=' + escape(document.location.href), 
        'Chat1630735921336927979', 
        'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=640,height=480');
        return false;
    });

    $('a#home-chat').click(function() {
        window.open('http://livechat.boldchat.com/aid/298700765092244427/bc.chat?cwdid=1334397467855432992&amp;url=' + escape(document.location.href),
        'Chat1630735921336927979',
        'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=640,height=480');
        return false;
    });
    
    
    
    $('dfn').click(function() {
        mee.hover.show(this);
    });
    
    
	$('#search-text').click(function(){
		mee.zoeken.onfocus($(this));
	}).keyup(function() {
	    mee.zoeken.keyup(this.value);
	});
	
	$('#search-text').blur(function(){
		mee.zoeken.defocus($(this));
	}); 
    
    $('a[rel="external"]').click(function () {
        $(this).attr({'target':'_blank'});
    });
});
