/**
 * 	Common JS.
 *		All common stuff.
 *
 *		Version: 0.0.1
 *		Created: Wed, 20 Jul 2011 14:49:24 +0200  (Daantje ruig.com)
 *		Updated: Wed, 20 Jul 2011 14:49:24 +0200
 */



	//some globals
	var __canvasInstance = 0;

	
	var makeRoundedImage = function(img,radius,stroke){
		var x = y = 1;
		var width = img.width();
		var height = img.height();
		if (typeof stroke == "undefined" ) {
			stroke = true;
		}
		if (typeof radius === "undefined") {
			radius = 10;
		}


		//build canvas
		img.hide();
		$('<canvas height="' + (height + 2) + '" width="' + (width + 2) + '" align="' + img.attr('align') + '" style="margin-left:' + img.attr('hspace') + 'px;margin-right:' + img.attr('hspace') + 'px;margin-top:' + img.attr('vspace') + 'px;margin-bottom:' + img.attr('vspace') + 'px;" id="canvasInstance_' + __canvasInstance + '"></canvas>').insertAfter(img);
		var ctx = document.getElementById("canvasInstance_" + __canvasInstance).getContext("2d");

		//get memory
		ctx.save();

		//start path
		ctx.beginPath();
		ctx.moveTo(x + radius, y);
		ctx.lineTo(x + width - radius, y);
		ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
		ctx.lineTo(x + width, y + height - radius);
		ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
		ctx.lineTo(x + radius, y + height);
		ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
		ctx.lineTo(x, y + radius);
		ctx.quadraticCurveTo(x, y, x + radius, y);
		ctx.closePath();

		ctx.lineWidth = 1;
		ctx.strokeStyle = "#666666"; // stroke color
		ctx.stroke();

		ctx.clip();

		//fill clip with image
		var imgLoader = new Image();
		imgLoader.onload = function(){
			//done parse and hide original to be on the safe side
			ctx.drawImage(imgLoader, 1, 1, width, height);
			ctx.restore();
		};
		imgLoader.src = img.attr('src');

		__canvasInstance++;
	}

	
	//make objects same height
	var make_same_height = function(selector){
		var max_height = 0;
		$(selector).each(function(){
			if($(this).height() > max_height){
				max_height = $(this).height();
			}
		});
		$(selector).height(max_height);	
	}


	
	//emulate popup function...
	var popWindow = function(u,w,h,popEl){
		if(!popEl){
			e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
			if(e == 'png' || e == 'gif' || e == 'jpg')
				popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><img src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" /></div>",{overlay:20});
			else
				popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><iframe src=\""+ u +"\"  width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>",{overlay:20});
		}
		popEl.modal({
				onOpen: function (dialog) {
							dialog.data.show();
							//dialog.data.addClass('alert');
							dialog.container.css('height','auto');
							dialog.overlay.fadeIn('normal', function () {
								dialog.container.show('normal', function () {
									//dialog.data.slideDown('normal'); // See Other Notes below regarding
															   // data display property and
															   // iframe details
								});
							});
						},
				onClose: function (dialog) {
							//dialog.data.slideUp('normal', function () {
							  dialog.container.hide('normal', function () {
								dialog.overlay.fadeOut('normal', function () {
								  $.modal.close(); // must call this to have SimpleModal
												   // re-insert the data correctly and
												   // clean up the dialog elements
								$('#alert').remove();
								});
							  });
							//});
						}
		});
		$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1); 
	}

	var vuid=0;
	var uid = function(){
		return vuid++ ;
	}
	
	//init
	$(document).ready(function(){
		
		if($('.spec_group_menu select').length){
			$('.spec_group_menu select').change(function(){
				$(this).parents('form').submit();
			});
		}
		
		
		//change all popups...
		var img = new Array();
		$(".article a").each(function(){
			i = 0;
	
			//change all onclick popups
			if($(this).attr('onclick')){
				i = $(this).attr('onclick').toString().indexOf("dow.open(this.href,");
	
				if(i){
					w = 640;
					h = 480;
					
					u = $(this).attr('href');
					e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
					
					if(e == 'png' || e == 'gif' || e == 'jpg'){
						var id = 'popwinlink_' + uid();
						$(this).attr('id',id);
	
						img[id] = new Image();
						img[id].onload = function(){
							img[id].onload = null;
							w = img[id].width;
							h = img[id].height;
		
							$("a#" + id).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");
						}
						img[id].src = u;
					}else{
						c = $(this).attr('onclick').toString();
	
						h = parseInt(c.match(/height=([0-9]+)/)[1]) + 25;
						w = parseInt(c.match(/width=([0-9]+)/)[1]) + 25;
		
						$(this).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");				
					}
				}
	
			//change all movie files
			}else if($(this).attr('href') && $(this).attr('href').match(/.(flv|FLV)$/)){
				//get filename...
				u = $(this).attr('href').match(/(\/db\/.*.(flv|FLV)$)/)[1];
				$(this).addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/lemstramotoren.nl/video.php?file=" + u + "',320,260);");		
			}
		});

		
	});
