/**
 * Javascripts for Group Jansen
 * 
 * Stijn Van Minnebruggen
 * Katrien Delrue
 * 
 * These Days
 * www.thesedays.com
 * 
 */


// on page load
	$(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);
		
	});


// 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(); 
			 
			});
			
			iv = setInterval('randomItem()', 10000);
		
		}
	});
	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);
	}



// sifr 
  function sifr() {
		if(typeof sIFR == "function"){
			sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:"js/sifr/din.swf", sColor:"#2d2c2c", sLinkColor:"#2d2c2c", sBgColor:"#FFFFFF", sHoverColor:"#2d2c2c"}));
			sIFR.replaceElement(named({sSelector:".g_products h2", sFlashSrc:"js/sifr/din.swf", sColor:"#0492d0", sLinkColor:"#0492d0", sBgColor:"#FFFFFF", sHoverColor:"#0492d0"}));
			sIFR.replaceElement(named({sSelector:".g_finishings h2", sFlashSrc:"js/sifr/din.swf", sColor:"#b20838", sLinkColor:"#b20838", sBgColor:"#FFFFFF", sHoverColor:"#b20838"}));
			sIFR.replaceElement(named({sSelector:".g_realestate h2", sFlashSrc:"js/sifr/din.swf", sColor:"#e3378a", sLinkColor:"#e3378a", sBgColor:"#FFFFFF", sHoverColor:"#e3378a"}));
			sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:"js/sifr/din.swf", sColor:"#faa635", sLinkColor:"#faa635", sBgColor:"#FFFFFF", sHoverColor:"#faa635"}));
		};
	}



// 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);
			
		}
	}



