(function($) {
	window.isIE6 = /msie 6/i.test(navigator.userAgent)? true:false;
	
	
	$(function() {

		var rounded = RoundedImages.createStyle({
			shadowOffset:0
		});
		
		var rotatedleft = rounded.using({
			rotation:		'5 5',
			borderRadius:	'0 0 0 0',
			borderColor:	'#fff',
			borderOpacity:	1,
			borderWidth:	3,
			shadowColor:	'#cbcbc6',
			shadowOffset:	1,
			shadowRadius:	0,
			shadowOpacity:	1,
			shadowStyle:	'soft'
		});
		
		var rotatedright = rounded.using({
			rotation:		'-5 -5',
			borderRadius:	'0 0 0 0',
			borderColor:	'#fff',
			borderOpacity:	1,
			borderWidth:	3,
			shadowColor:	'#cbcbc6',
			shadowOffset:	1,
			shadowRadius:	0,
			shadowOpacity:	1,
			shadowStyle:	'soft'
		});
		
		var rounded = rounded.using({
			borderRadius:	'5 5 5 5'
		});
		
		RoundedImages.parse($('img.rotated-left'), rotatedleft);
		RoundedImages.parse($('img.rotated-right'), rotatedright);
		RoundedImages.parse($('img.rounded'), rounded);
	
		var TipsEnAdvies = {
			init: function() {
				// init click event
				$('#tipsenadvies .issuelist').click(
					function(e){ 
						var target = e.target;
						TipsEnAdvies.activate(target); 
						e.preventDefault();
					}
				);
				// activate deeplinked (hashed) item
				var hash = document.location.hash;
				if(hash) {
					$('#tipsenadvies .issuelist a').each(function(){
						if(this.hash == hash) TipsEnAdvies.activate(this);
						setTimeout('window.scrollTo(0, 0)', 500);
					});
				}
				
				//add close button
				$('#tipsenadvies .box').append("<a href='#' class='close'>sluiten</a>");
				
				//close active tip and show default trigger
				$('.close','#tipsenadvies').click(function(e){
					TipsEnAdvies.close();
					e.preventDefault();
				});
			},
			resetAll: function() {
				// reset all items
				$('#tipsenadvies .issuelist li').removeClass('active');
				$('#tipsenadvies .answerlist li').removeClass('active');
			},
			activate: function(target) {
				var hash = target.hash;
				$('#tipsenadvies .trigger').hide();
				$('#tipsenadvies .box').css({ display: 'block' });
				TipsEnAdvies.resetAll(); 
				$(target).parent().addClass('active');
				$(hash).addClass('active');
			},
			close: function() {
				TipsEnAdvies.resetAll();
				$('#tipsenadvies .box').css({ display: 'none' })
				$('#tipsenadvies .trigger').show();
			}
			
		};
		
		if(document.getElementById('tipsenadvies')) TipsEnAdvies.init();
		$('body').addClass('js-enabled');
		
		// equalize boxes in a row
		var EqualizeBoxes = {
			init: function(root) {
				var self = this;
				if(!root) root = $(document);
				$(".equalize", root).each(function() {
					EqualizeBoxes.setHeight($(".equal", $(this)));
				});
			},
			setHeight: function(obj) {
				var h = 0;
				obj.each(function() { h = Math.max(h, $(this).get(0).offsetHeight); });
				obj.each(function() {
					var p = parseInt($(this).css("paddingTop")) + parseInt($(this).css("paddingBottom"));
					$(this).css({ minHeight: (h - p) + "px" });
					// compensate for stubborn IE6
					if(window.isIE6) $(this).css({ height: (h - p) + "px" });
				});
				// tickle DOM, for some browser
				$("body").toggleClass("tickled");
			}
		};
		
		window.setTimeout(function() { EqualizeBoxes.init(); }, 500);
		
		// clickable boxes
		$(".clickable .box").click(function(e){
			window.location=$(this).find("a").attr("href");
			return false;
		});
		
		// hoverable boxes
		$(".clickable .box").hover(
     		function () {
		    	$(this).toggleClass("hover");
		    }, 
		    function () {
		    	$(this).toggleClass("hover");
		  	}
	 	);
	
		// collapse on gift checkbox
		var checkbox = $(".send-gift");
		var section = $("#address-data");
		if(checkbox.attr('checked')) section.show(); else section.hide();
		checkbox.click(function(){
			if($(this).attr("checked")) $("#address-data").show(); else $("#address-data").hide();
		});
		
		// video(p)layer
		window.VideoPlayer = {
			videolayer: jQuery('<div id="videolayer" style="z-index: 3;"><div id="videoplayer" style="z-index: 3;"></div></div>'),
			init: function() {
				// launch video on video links in HTML
				$('a.launch-video').click(function(e) {
					var uri = $(this).attr('href');
					var title = $(this).attr('title');
					window.VideoPlayer.launch(uri);
					window.VirtualPageview.track(uri, title);
					e.preventDefault();
				});
			},
			launch: function(uri) {
				$('body').append(this.videolayer);
				//var flashvars = { xml:"style library/zwitsal/static/xml/videoplayer.xml" };
				var flashvars = { xml:"/Feeds/CommercialFeed.aspx" };
				var params = { wmode: 'transparent', scale:'noscale', salign: 'lt' };
				var attributes = {};
				//swfobject.embedSWF(uri, "videoplayer", "100%", "100%", "9.0.28", "style library/zwitsal/static/swf/expressInstall.swf", flashvars, params, attributes);
				swfobject.embedSWF(uri, "videoplayer", "100%", "100%", "9.0.28", "/style library/zwitsal/static/swf/expressInstall.swf", flashvars, params, attributes);
				if(window.isIE6) {
					this.videolayer.append('<iframe border="0" frameborder="0"></iframe>');
					this.videolayer.css({ position: 'absolute' });
					$(window).bind('scroll', window.VideoPlayer.steady);
					window.VideoPlayer.steady();
				}
			},
			close: function() {
				$('#videolayer').remove();
				if(window.isIE6) $(window).unbind('scroll', window.VideoPlayer.steady);
			},
			steady: function() {
				var yPos = document.documentElement.scrollTop;
				window.VideoPlayer.videolayer.css({ top: yPos });
			}
		};

		window.VideoPlayer.init();
		
		// popup
		window.PopupLayer = {
			poplayer: jQuery('<div id="poplayer"><div id="popcontent"><div id="poptop"></div><div id="popbody"><a href="#" id="popclose">Sluit</a><div id="popdata"></div></div><div id="popbottom"></div></div></div>'),
			init: function() {
				// launch video on links in HTML
				$('a.launch-popup').bind("click", function(e) {
					e.preventDefault();
				 	var uri = $(this).attr('href');
					window.PopupLayer.launch(uri);
				});
			},
			launch: function(uri) {
				$('body').append(this.poplayer);
				if(window.isIE6) {
					this.poplayer.append('<iframe border="0" frameborder="0"></iframe>');
					this.poplayer.css({ position: 'absolute' });
					$(window).bind('scroll', window.PopupLayer.steady);
					window.PopupLayer.steady();
				}
				$('a#popclose').click(function(e) {
					e.preventDefault();
					window.PopupLayer.close();
				});
				$.ajax({ 
					type: 'GET',
					url: uri, 
					success: function(data){
        		$('#popdata').html(data);
						Cufon.replace('#popdata .cufon a', { hover: true })('#popdata .cufon', { fontFamily: 'ITCMixageBold' });
     			}
				});
			},
			close: function() {
				$('#poplayer').remove();
				if(window.isIE6) $(window).unbind('scroll', window.PopupLayer.steady);
			},
			steady: function() {
				var yPos = document.documentElement.scrollTop;
				window.PopupLayer.poplayer.css({ top: yPos });
			}
		};

		window.setTimeout(window.PopupLayer.init, 1000);

		// cookie
		window.Cookie = {
			/**
		 	* Sets a cookie under the given name
			 * 
			 * @param {String} name
			 * @param {String} value
			 * @param {number} expires Days until the cookie expires 
			 */
			setCookie:function(name, value, expires){
				var date = new Date();
				date.setTime(date.getTime() + ((expires || 14) * 86400000));
				document.cookie = name + "=" + encodeURIComponent(value) + "; expires=" + date.toUTCString() + "; path=/";
			},
		
			/**
			 * Gets the cookie with the given name
			 * 
			 * @param {String} name
			 * @return {String} value
			 */
			getCookie:function(name){
				var reg = new RegExp(name+'=([^;$]+)', 'i');
				var cookie = reg.exec(document.cookie);
				return (cookie && cookie[1])? decodeURIComponent(cookie[1]) : '';
			}
		};
		

		// tips en advies trigger
		window.TipsEnAdviesTrigger = {
			goTo: function() {
				document.location = 'http://'+document.domain+'/TipsEnAdvies';
			}
		};

		// news scroller
		function NewsScroller(root) {
			this.selector = root;
			this.root = $(root)[0];
			if(this.root) {
				this.init();
			}
		}

		NewsScroller.prototype = {
			init:function () {
				this.index = -1;
				this.items = $('li', this.root);
				this.items.css(NewsScroller.BEHIND);
				this.start();
			},

			start: function () {
				this.stop();
				var next = this.next.bind(this);
				this.timer = window.setInterval(next, 5000);
				window.setTimeout(next, 500);
			},

			stop:function () {
				clearInterval(this.timer);
			},

			next:function () {
				// IE7 may lose node references because of swfaddress.js
				// next() is only called if root is initially found, so if it becomes lost, look again! it IS out there.
				if(!this.root || !this.root.innerHTML) {
					this.root = $(this.selector)[0];
					this.items = $(this.selector + ' li');
				}

				var l = this.items.length;
				var next = (this.index +1) % l;
				
				if(this.index >= 0) {
					var current = this.items.eq(this.index);
					current.css(NewsScroller.BEHIND).animate({top: -50});
				}

				var item = this.items.eq(next);
				item.css(NewsScroller.UPFRONT).animate({top: 0});

				this.index = next;
			}
		};

		NewsScroller.BEHIND = { zIndex: 1 };
		NewsScroller.UPFRONT = { zIndex: 2, top: 50 };

		new NewsScroller('.news-rotator');
		
		// tips scroller
		function TipsScroller(root) {
			this.selector = root;
			this.root = $(root)[0];
			if(this.root) {
				this.init();
			}
		}

		TipsScroller.prototype = {
			init:function () {
				this.index = -1;
				this.items = $('li', this.root);
				this.items.css(TipsScroller.BEHIND);
				this.start();
			},

			start: function () {
				this.stop();
				var next = this.next.bind(this);
				this.timer = window.setInterval(next, 5000);
				window.setTimeout(next, 500);
			},

			stop:function () {
				clearInterval(this.timer);
			},

			next:function () {
				// IE7 may lose node references because of swfaddress.js
				// next() is only called if root is initially found, so if it becomes lost, look again! it IS out there.
				if(!this.root || !this.root.innerHTML) {
					this.root = $(this.selector)[0];
					this.items = $(this.selector + ' li');
				}

				var l = this.items.length;
				var next = (this.index +1) % l;
				var nextnext = (next + 1) % l;
				
				if(this.index >= 0) {
					var current = this.items.eq(this.index);
					current.css(TipsScroller.BEHIND).animate({top: -155});
				}

				var item = this.items.eq(next);
				item.css(TipsScroller.UPFRONT).css({top: 150}).animate({top: 0});

				var item = this.items.eq(nextnext);
				item.css(TipsScroller.UPFRONT).css({top:300}).animate({top: 150});

				this.index = next;
			}
		};

		TipsScroller.BEHIND = { zIndex: 1 };
		TipsScroller.UPFRONT = { zIndex: 2 };

		new TipsScroller('.tips-rotator');
		
		// webtrends tagging
		window.VirtualPageview = {
			// virtual pageviews for content openend in an overlay
			track: function(uri, title) {
				var seg_1 = $('meta[name=WT.seg_1]').attr("content");
				var seg_2 = $('meta[name=WT.seg_2]').attr("content");
				var seg_3 = $('meta[name=WT.seg_3]').attr("content");
				
				dcsMultiTrack('DCS.dcsuri', uri, 'WT.ti', title, 'WT.seg_1', seg_1, 'WT.seg_2', seg_2, 'WT.seg_3', seg_3);
			},
			
			// virtual pageviews for sections in main flash application
			flashTrack: function(seg_1, seg_2, seg_3, title, uri) {
				if (uri != '/') {
					dcsMultiTrack('DCS.dcsuri', '/Pages/Home.aspx#' + uri, 'WT.ti', title, 'WT.seg_1', seg_1, 'WT.seg_2', seg_2, 'WT.seg_3', seg_3);
				}
			}
		};
	
	});
	
})(jQuery);
