/**
 * Javascripts for Group Jansen
 * 
 * Stijn Van Minnebruggen, Katrien Delrue
 * www.thesedays.com
 * 
 * Benny Belmans
 * www.artmania.net
 * 
 */


// on page load / Bb
	$(document).ready(function() {

		sifr();
		filter();
			
		$('input.clfld').clearFieldOnFocus();
		$('select.width_m').customSelect();
		
		if($('#home_gallery').length > 0) { // if homepage
		
			$('#home_gallery').homeGallery();
			newsTicker();
		
		}
		
		//setTimeout('slideShow()', 1000);
		
		$("#login_link a").fancybox();
	
		$(".iframe").fancybox({
				'titleShow'		: false,
				'width'			: 750,
				'height'		: 570,
				'scrolling'		: 'no'
			});	

		$(".lightbox").fancybox({
				'titleShow'		: false
			});				

		$(".lightgroup").fancybox({
				'titleShow'		: true,
				'titlePosition'	: 'over',
				'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
								  return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}				
			});
			

		// Cycle options
			
		$('.prodslide')
		   //.before('<div id="nav">')
		   .cycle({ 
				fx:    'fade', 
				speed:   300, 
				timeout: 3000, 
				pause:   1,
				pager:  '#prodnav',
				pagerEvent: 'mouseover',
				pauseOnPagerHover: true,
				//pagerClick: function() {},
				pagerAnchorBuilder: function(idx, slide) {
				// return sel string for existing anchor
				return '#prodnav li:eq(' + (idx) + ') a';
				}
		});		
			
	});
	



// submit form - filters / Bb
    function submitForm(){ 
	  document.form1.submit(); 
	}	


// Googlemap Projects / Bb

function SelectAll()
	{
		if(document.form1.chkResource)
			{
				if(document.form1.chkSelectAll.checked)
					{
						if(!document.form1.chkResource.length)
							{
								document.form1.chkResource.checked = true;
							}	
							else
							{
								for(var i = 0;i<document.form1.chkResource.length;i++)
									{
										if(!document.form1.chkResource[i].disabled)
										document.form1.chkResource[i].checked = true;
									}
							}
							CheckResources();
						}
				 else
						{
							if(!document.form1.chkResource.length)
							{
								document.form1.chkResource.checked = false;
							}	
							else
							{
								for(var i = 0;i<document.form1.chkResource.length;i++)
									{
										document.form1.chkResource[i].checked = false;
									}
							}
							CheckResources();
						}			
				}
	}

function CheckResources(strRegid)
 	{
 	var deviceId = "";
	for(i=0;i<document.form1.chkResource.length;i++)
		{
			if(document.form1.chkResource[i].checked ==false)
				{
					document.form1.chkSelectAll.checked=false;
				}
			else
				{
					document.form1.chkSelectAll.checked=true;
				}
		}
 	
		 if(!document.form1.chkResource.length)
		 	{
						if(document.form1.chkResource.checked)
							{
								if(!(deviceId == ""))
									{
										deviceId  = deviceId + "," +document.form1.chkResource.value;
									}
								else
									{
										deviceId  = document.form1.chkResource.value;
									}		
							}	
			}
		 else
		 	{
				for(var i = 0;i<document.form1.chkResource.length;i++)
					{
						if(document.form1.chkResource[i].checked)
							{
								if(!(deviceId == ""))
									{
										deviceId  = deviceId + "," +document.form1.chkResource[i].value;
									}
								else
									{
										deviceId  = document.form1.chkResource[i].value;
									}			
							}
						else
							{
								if(!document.form1.chkResource[i].disabled)
								document.form1.chkSelectAll.checked = false;
							}			
					}
			}	
			document.getElementById("ProjectId").value = deviceId;
			var arrLocationID = deviceId.split(",");
			for(var intLoopCounter = 0; intLoopCounter < arrLocationID.length; intLoopCounter ++)
				{
					HideLocations(arrLocationID[intLoopCounter]);
				}
	}


// slideshow
	var speed, iv, mover, num, arrowL, arrowR, width, padding, pixels, speed;
	function slideShow() {
		
		// settings
			mover = $('#ss_thumbs ul');
			num = mover.children('li').size();
			arrowL = $('#ss_left');
			arrowR = $('#ss_right');
			width = num * mover.children('li').width() + num * 8;
			mover.css('width', width);
			splitter = '@@@SPLIT@@@';
			padding = 80;
			speed = 40;
			pixels = 10;
		
		// add actions
			arrowL.hover(function() { slide(-1); }, function() { slide(0); });
			arrowR.hover(function() { slide(1); }, function() { slide(0); });
			arrowL.click(function(evt) { evt.preventDefault(); });
			arrowR.click(function(evt) { evt.preventDefault(); });
			mover.children('li').children('a').click(function(evt) {
				mover.children('li').children('a').each(function(e) { $(this).removeClass('sel'); });
				$(this).addClass('sel');
				var id = $(this).attr('rel');
				showLargeImg(id);
				evt.preventDefault();
			});
		
		// functions
			function slide(dir) {
				if(dir == 1) {
					iv = setInterval(function() {
						newX = parseFloat(mover.css('marginLeft'))-pixels;
						if(newX > 600-width-padding) mover.css('marginLeft', newX);
					}, speed);
				} else if(dir == -1) {
					iv = setInterval(function() {
						newX = parseFloat(mover.css('marginLeft'))+pixels;
						if(newX < pixels+padding) mover.css('marginLeft', newX);
					}, speed);
				} else {
					clearInterval(iv);
				}
			}
			
			function showLargeImg(id) {
				var url = 'ajax/refData.php';
				var post = '&id='+id;
				$.post(url, post, function(r) {
					r = r.split(splitter);
					$('#ss_img img').attr('src', r[1]);
					$('#ss_txt').html(r[0]);
				});
			}
		
	}


// home gallery - quick fix
	var current = 0;
	var iv;
	jQuery.fn.extend({
		homeGallery: function() {
			
			// preloader
				$('.gallery_nav a').each(function() {
					
					objimg = new Image();
					objimg.src = $(this).attr('rel');
					
					
				});
			
			//
			$('#item_intro h3').click(function(e) { 
						if ( $(this).hasClass('closed') ) {
							$('#item_intro').animate({height:"328",opacity:"1"},800);
						  $(this).removeClass('closed');	
						} else {
					    $('#item_intro').animate({height:"75",opacity:"0.8"},400);
						  $(this).addClass('closed');						
						}														 
						e.preventDefault(); 
			});
			$('.gallery_nav a').click(function(e) {
				clearInterval(iv);
				showHomeGallery($(this));
				e.preventDefault(); 
			 
			});
			
			// speed of home gallery
			iv = setInterval('randomItem()', 8000);
		
		}
	});
	function randomItem() {
		var max = $('.gallery_nav a').size();
		var rand = Math.floor(Math.random()*max);
		var item = $('.gallery_nav a')[rand];
		showHomeGallery($(item));
	}
	function showHomeGallery(currentItem) {
		if($('#home_gallery').length > 0) {
			
			$('.gallery_nav a').removeClass('current');
			
			var bg = currentItem.attr('rel');		
			if( bg != '') { 
			
				if(current == 1) {
				
					current = 2;
					$('#gallery_visual_2').css({
						'opacity': 0,
						'background-image': 'url('+bg+')'
					});
					$('#gallery_visual_2').stop().animate({opacity:'1'}, 1000);
				
				} else if (current == 2) {
				
					current = 1;
					$('#gallery_visual_1').css('background-image', 'url('+bg+')');
					$('#gallery_visual_2').stop().animate({opacity:'0'}, 1000);
				
				} else {
				
					current = 2;
					$('#gallery_visual_2').css({
						'opacity': 0,
						'background-image': 'url('+bg+')'
					});
					$('#gallery_visual_2').stop().animate({opacity:'1'}, 1000);
				
				}
			
				currentItem.addClass('current');
			
			}
			
			setTimeout(function() {
				$('#home_gallery .gallery_visual').css('backgroundImage', 'none');
			}, 2000);
			
			var pr = currentItem.attr('class');
			if( pr != undefined) {
				pr = pr.split('current').join('');
				pr = pr.split(' ').join('');
				$('.description img.proj').hide();
				$('.description img.'+pr+'_title').show();
			}
		}
	}


// clear field on focus, restore on blur
	jQuery.fn.extend({
		clearFieldOnFocus: function() {
			return this.each(function() {
				if($(this).attr('rel') == undefined) $(this).attr('rel', $(this).attr('value'));
				$(this).focus(function() { if($(this).attr('value') == $(this).attr('rel')) $(this).attr('value', ''); });
				$(this).blur(function() { if($(this).attr('value') == '') $(this).attr('value', $(this).attr('rel')); });
			});
		}
	});


// extend some math functions
	$.extend({
		sumPx: function() { var sum = 0; for(var i=0; i<arguments.length; i++) { sum += parseFloat(arguments[i]); } return sum+'px'; },
		cssBounds: function(px) { return (typeof px == 'string' && px.split('px').length> 1) ? parseFloat(px) : px+'px'; }
	});


// Custom select boxes
		jQuery.fn.extend({
			customSelect: function() {
		    if(!$.browser.msie){
				
					return this.each(function() {
						
						var sel_id = $(this).attr('id');
						var sel_class = $(this).attr('class');
						$(this).wrap('<div id="'+sel_id+'_box" class="sel_'+sel_class+'_box"></div>').parent().prepend('<div id="'+sel_id+'_label" class="sel_'+sel_class+'_label">Geen voorkeur</div>');
						$(this).change(function() { 
							var str = "";
							$("select option:selected").each(function () {
										str += $(this).text() + " ";
							});
							$('#'+sel_id+'_label').html(str); 
						}).change();
						
					});
					
				};
				
			}
		});



// show-hide errors

	function showError(v) {
		if(v != null && v != undefined) { if($('#error')) $('#error').html(v).show('slow');
		} else { if($('#error')) $('#error').show('slow');
		}
		return false;
	}
	
	function hideError() {
		if($('#error')) $('#error').hide('slow');
		return true;
	}
	
	function isEmail(v) {
		return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(v);
	}



// filtering system
	var cats = new Array();
	function filter() {
		
		// get current values
			getFilterValues();
			filterValues();
				
		// add actions
			$('input.filter').click(function(evt) {
				getFilterValues();
				filterValues();
			});
		
		// functions
			function getFilterValues() {
				cats = new Array();
				$('.filters ul li input').each(function() {
					if($(this).is(":checked")) {
						cats.push($(this).attr('id').split('filter_').join(''));
					}
				});
			}
			function filterValues() {
				$('.filterContent').children().hide();
				for(var i=0; i<=cats.length; i++) {
					$('.filterContent .cat_'+cats[i]).show();
				}
				if(typeof gMapFilter == 'function') { gMapFilter(cats); }
			}
		
	}


// news ticker
	var newsUl, newsItems, newsH, newsIv, newsSpeed, newsC, newsD;
	function newsTicker() {
		newsUl = $('.news_ticker ul');
		newsItems = newsUl.children('li').size();
		if(newsUl.length > 0 && newsItems > 1) {
			
			// set vars
				newsC = 1;
				newsD = 1;
				newsSpeed = 500;
				newsH = parseFloat(newsUl.css('height'));
			
			// add animation
				newsIv = setInterval(function() {
					
					// get current y position
						newsY = parseFloat(newsUl.css('marginTop'));
					
					// switch direction
						if(newsD == 1 && newsC == newsItems) newsD = -1;
						else if(newsD == -1 && newsC == 1) newsD = 1;
					
					// move the item
						if(newsD == 1) {						
							newY = newsY-newsH;
							newsC++;
						} else {
							newY = newsY+newsH;
							newsC--;
						}
					
					// animate
						newsUl.animate({marginTop: newY}, newsSpeed);
					
				}, 8000);
			
		}
	}

