var _ie6 = false;
if ($.browser.msie && $.browser.version == '6.0') _ie6 = true;
var _hold, _btn, _tabs, s1_f, s2_f;

// main code
$(document).ready(function(){
	initSifr();
	slideGall('.gall-hold');
	phoneGall('.phone-gall');
	if (!$.browser.safari) {
		initTabs('.aanbevolen .tabset');
		initTabs('.applicaties-serv-cnt .tabset');
		//initTabs('.telephone-nav .tabset');
	} else {
		setTimeout(function(){
			initTabs('.aanbevolen .tabset');
			initTabs('.applicaties-serv-cnt .tabset');
			//initTabs('.telephone-nav .tabset');
		}, 150);
	}	
	slideBox();
	telephoneNav();
	
	initStars();
	initToestelenSpecial('.toestelenspecial');
	frmTel = $('[name=frmTelefoon]');
	if(frmTel.attr('name') == 'frmTelefoon'){
		$('#toestellen').change(function(){
			setTimeout(function(){
				$('[name=frmTelefoon]').submit();
			}, 150);
		});
	}
	
	//melding op niet bekend
	$('.letop').live('click', showTip);
	$('#unkownCell').click(submitUnknow);
	
	//geef je meninig.
	$('#geef_je_mening').click(geef_je_mening);
	$('#beoordeel_zelf').click(geef_je_mening);

	$('#smsfrm .btn').click(submitTip);
	$('#mailfrm .btn').click(submitTip);
	
	//fNummer 
	var _valNur= '06';
	$('#smsfrm').find('#fNummer').focus(function(){
		if(this.value == _valNur){
			this.value = '';
		}
	}).blur(function(){
		if(this.value == ''){
			this.value = _valNur;
		}
		else{
		}
	});
	
	//fnaam & fnaam2
	var _valfnaam= 'afzender';
	$('#smsfrm').find('#fnaam').focus(function(){
		if(this.value == _valfnaam){
			this.value = '';
		}
	}).blur(function(){
		if(this.value == ''){
			this.value = _valfnaam;
		}
		else{
		}
	});
	$('#mailfrm').find('#fnaam2').focus(function(){
		if(this.value == _valfnaam){
			this.value = '';
		}
	}).blur(function(){
		if(this.value == ''){
			this.value = _valfnaam;
		}
		else{
		}
	});
	
	//fMail
	var _valMail = 'e-mailadres';
	$('#mailfrm').find('#fMail').focus(function(){
		if(this.value == _valMail){
			this.value = '';
		}
	}).blur(function(){
		if(this.value == ''){
			this.value = _valMail;
		}
		else{
		}
	});
	
	initSubmitApp();
});

function initSubmitApp(){
	//.mobielesite, .widget, .applicatie
	$('#mobielesite').click(doChangeSoort);
	$('#widget').click(doChangeSoort);
	$('#applicatie').click(doChangeSoort);
	$('#mobielesite').removeAttr('checked');
	$('#applicatie').removeAttr('checked');
	$('#widget').removeAttr('checked');
	$('#sendEigenaar').click(sendEigenaar);
	$('#sendGebruiker').click(sendGebruiker);

}

function doChangeSoort(){
	if($(this).attr('checked')){
		$('.' + $(this).attr('id')).slideDown();
	}else{
		$('.' + $(this).attr('id')).slideUp();
	}
}
function submitTip(){
	frm = $(this).parent().parent();
	
	trim06($('#fNummer'));
	
	//alert(frm.attr('id'));
	$.ajax({
	   type: "POST",
	   dataType: "json",
	   url: "/ajax/tip",
	   data: frm.serialize(),
	   success: function(msg){
	   		if(msg.okey){
				//alert('Tip is verzonden');
				var _this = this;
				//window.top.bedankt(msg.view, window);
				bedankt(msg.view);
			}else{
				if(msg.error != ''){
					alert(msg.error);
				}else{
					alert('Tip is niet verzonden');
				}
			}
	   }
	 });
	return false;
}

function showTip(){
	var _this = this;
	show_tip(_this);
	return false;
}
function geef_je_mening(){
	var _this = this;
	//alert('asdf');
	geef_je_mening_popup(_this);

	return false;
}

/*--- function telephoneNav ---*/

function hideSelects() {
	if (_ie6) $('select').css('visibility','hidden');
}
function showSelects() {
	if (_ie6) $('select').css('visibility','visible');
}
function telephoneNav(){
	_hold = $('.telephone-nav .tabs-hold');
	_btn = $('.telephone-nav .tabset a.tab');
	_tabs = $('.tabs-hold .tab');
	s1_f = false;
	s2_f = false;
	
	_btn.each(function(_ind, _el) {
		var _box = $('#'+_el.rel.substr(_el.rel.indexOf("#") + 1));
		if($(_el).hasClass('active')) {
			_box.show();
			_a = _ind;
		}
		else {
			_box.hide();
		}
	});
	
	//default value van stap 1 .... kopje andere telefoon aanpassen
	var _val = 'andere telefoon';
	_tabs.find('.stap1').find('.text').focus(function(){
		if(this.value == _val){
			this.value = '';
		}
		_tabs.find('.stap1').find('select').attr('disabled', 'disabled');
	}).blur(function(){
		if(this.value == ''){
			_tabs.find('.stap1').find('select').attr('disabled', '');
			this.value = _val;
			s1_f = false;
		}
		else{
			s1_f = true;
		}
	});
	
	//default value van stap 3 	
	var _val2 = 'vul code in';
	_tabs.find('.stap3').find('.text1').focus(function(){
		if(this.value == _val2){
			this.value = '';
		}
	}).blur(function(){
		if(this.value == ''){
			this.value = _val2;
		}
	});
	
	//default value van stap 3 
	var _val3 = '06-nummer';
	_tabs.find('.stap3').find('.text2').focus(function(){
		if(this.value == _val3){
			this.value = '';
		}
	}).blur(function(){
		if(this.value == ''){
			this.value = _val3;
		}
	});
	
	//naar stap 1 telefoon kiezen
	_hold.find('a.btn-goto-stap1').die('click').live('click', function(){
		var _thisHref = '#tab2';
		$('#btn-tab1').click(showTab);
		_btn.removeClass('active').filter('[rel="'+_thisHref+'"]').addClass('active');
		_tabs.hide();
		_tabs.filter(_thisHref).show();
		
		var _s1 = _tabs.find('.stap1')
		var _s2 = _tabs.find('.stap1-a')
		return false;
	});
	
	
	//ander toestel vinden
	_tabs.find('a.simple').click(function(){
		var _s1 = _tabs.find('.stap1').show();
		var _s2 = _tabs.find('.stap1-a').hide();
		
		_tabs.find('a.simple').hide();
		_tabs.find('a.btn-goto-stap2').hide();
	})
	$('#toestellen').change(changeToestel);
	
	//naar applicatie keuze venster gaan
	var btn_st2 = _hold.find('a.btn-goto-stap2');
	btn_st2.live('click', function(){
		if($('[name=titelapplicatie]').length == 1){
			$('[name=titelapplicatie]').attr('checked','checked');
		}
		//	$('#btn-tab2').click(showTab);
		//	$('#btn-tab3').click(showTab);
		//	_hold.find('.btn-goto-stap4').trigger('click');
		//}else{
			var _thisHref = '#tab3';
			$('#btn-tab2').click(showTab);
			_btn.removeClass('active').filter('[rel="'+_thisHref+'"]').addClass('active');
			_tabs.hide();
			_tabs.filter(_thisHref).show();		
			
			//tooltips activeren op de applcaties		
			$('.tip').tooltip({ 
				track: true,
				left: -10,
				top:25
			});
		//}
		return false;
	});
	
	//naar stap 3 (telefoon + code invul venster)
	_hold.find('.btn-goto-stap4').click(checkAppSelected);
	
	
	//applicatie daadwerkelijk verzenden en bedankt venster openen
	_hold.find('.btn-goto-stap5').click(checkCode);
	
}
function showTab(){
	//alert();
	_btn.removeClass('active').filter('[rel="'+$(this).attr('rel')+'"]').addClass('active');
	_tabs.hide();
	_tabs.filter($(this).attr('rel')).show();	
	return false;
}
function checkAppSelected(){
	/* controleren of er een app is geselecteerd */
	if(!$('[name=titelapplicatie]').filter(':checked').val()){
		alert('kies een type applicatie');
	}else{		
		$.ajax({
		   type: "POST",
		   dataType: "json",
		   url: "/ajax/applicatie",
		   data: $('#wizzard').serialize(),
		   success: function(msg){
		   		$('.stap3 p').html(msg.stap3);
		   		$('#geef_je_mening').attr('href', msg.link);
			   	var _thisHref = '#tab4';
			   	$('#btn-tab3').click(showTab);
					_btn.removeClass('active').filter('[rel="'+_thisHref+'"]').addClass('active');
					_tabs.hide();
					_tabs.filter(_thisHref).show();
		   		}
		 });
	}
	return false;
}
	
function checkCode(){
	var blnE = false;
	if(document.wizzard.nummer.value == "06-nummer"){
		alert('geef je 06-nummer op');
		$('[name=nummer]').focus();
		blnE = true
	}
	if(document.wizzard.code.value == "vul code in" && !blnE){
		alert('vul de code in');
		$('[name=code]').focus();
		blnE = true
	}
	trim06($('input[name=nummer]'));

	if(!blnE){
		$.ajax({
		   type: "POST",
		   dataType: "json",
		   url: "/ajax/send",
		   data: $('#wizzard').serialize(),
		   success: function(msg){
		   		if(msg.okey){
					var _thisHref = '#tab5';
					_btn.removeClass('active').filter('[rel="'+_thisHref+'"]').addClass('active');
					_tabs.hide();
					_tabs.filter(_thisHref).show();
				}else{
					alert('code en/of telefoonnummer zijn niet geldig');
				}
		   }
		 });/* controleren of er een code/ is geselecteerd */
	}
	return false;
}

/*-- changing toestel --*/
function changeToestel(el){
	toestelID = $(this).val();
	$.ajax({
	   type: "POST",
	   dataType: "json",
	   url: "/ajax/stap1",
	   data: "toestelID=" + toestelID + "&titelID=" + $('#titelID').val(),
	   success: function(msg){
			$('.stap1-a').html(msg.stap1);
			$('.stap2').html(msg.stap2);
			$('.stap3 p').html(msg.stap3);
			
			var _s1 = _tabs.find('.stap1').hide();
			var _s2 = _tabs.find('.stap1-a').show();
			_tabs.find('a.simple').show();
			_tabs.find('a.btn-goto-stap2').show();
	   }
	 });
}
function submitUnknow(el){
	toestelID = -1;
	$.cookie('toestelID', toestelID, {expires: 356, path:'/'});
	
	$.ajax({
	   type: "POST",
	   dataType: "json",
	   url: "/ajax/stap1",
	   data: "toestelID=" + toestelID + "&titelID=" + $('#titelID').val(),
	   success: function(msg){
			$('.stap1-a').html(msg.stap1);
			$('.stap2').html(msg.stap2);
			$('.stap3 p').html(msg.stap3);
			
			var _s1 = _tabs.find('.stap1').hide();
			var _s2 = _tabs.find('.stap1-a').show();
			_tabs.find('a.simple').show();
			_tabs.find('a.btn-goto-stap2').show();
	   }
	 });
}

/*-- -slideBox --*/
var elopen;
function slideBox(){
	
	if($('a.btn-telefoon').hasClass('active')){	
		elopen = $('a.btn-telefoon');
		$('div.telefoon-text').css({display:'block'});
	}
	
	$('a.btn-genres').click(function(){
		
		if(!$('a.btn-genres').hasClass('active')){
			$(this).parents('.applicaties-serv-cnt').find('a').not(this).removeClass('active');
			$(this).toggleClass('active');
			if(elopen != $(this) && elopen != null){
				$('div.telefoon-text').slideToggle(400, resizeOuterIFrame);
			}
			$('div.genres-text').slideDown(400, resizeOuterIFrame);
			elopen = $(this);
		}
		return false;
	});
	
	if($('a.btn-genres').hasClass('active')){
		elopen = $('a.btn-genres');
		$('div.genres-text').css({display:'block'});
	}

}
/*--- phoneGall ---*/
function phoneGall(h_list){
	$(h_list).each(function(){
		var _a = 0;
		var _img = $(this).find('.gallery-img .display');
		var _btn = $(this).find('.gall-list a').each(function(_i){
			if($(this).hasClass('active')) _a = _i;
			this.onclick = function(){
				if(_a != _i){
					_img.attr('src', this.href);
					$(this).parent().addClass('active');
					_btn.eq(_a).parent().removeClass('active');
					_a = _i;
				}
				return false;
			}
		});
		_img.attr('src', _btn.eq(_a).attr('href'));
	});
}
/*--- slideGall ---*/
function slideGall(h_list){
	var _duration = 400;
	$(h_list).each(function(){
		var gal_hold = $(this);
		var _hold = gal_hold.find('.slide-box .slide-cnt');
		var _a = 0; 
		var _step = - gal_hold.find('.slide-box').width();
		var _btn = gal_hold.find('.slide-nav ul a').each(function(_i){
			if($(this).hasClass('active')) _a = _i;
			this.onclick = function(){
				changeEl(_i);
				return false;
			}
		});
		_btn.eq(_a).addClass('active');
		_hold.css('margin-left', _a*_step);
		
		gal_hold.find('.slide-nav .prev-slide').click(function(){
			changeEl(_a - 1);
			return false;
		});
		gal_hold.find('.slide-nav .next-slide').click(function(){
			changeEl(_a + 1);
			return false;
		});
		function changeEl(_ind){
			if(_ind > _btn.length - 1) _ind = 0;
			else if(_ind < 0) _ind = _btn.length - 1;
			if(_ind != _a){
				
				_hold.animate({marginLeft: _ind*_step}, {queue:false, duration:_duration });
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				//set
				if($(".block"+ _ind).hasClass(".block"+ _ind)){
					sIFR.replaceElement(named({sSelector:".block"+ _ind +" .product-box strong span", sFlashSrc:"/swf/grotesk.swf", sColor:"#E30074", sLinkColor:"#E30074", nPaddingTop:"0", nPaddingBottom:"0", nPaddingLeft:"0", sWmode:"transparent"}));
					$(".block"+ _ind).attr("class", "slide-area");
				}
				_a = _ind;
			}
		}
	});
}

function initToestelenSpecial(_el){
	$(_el).each(function(ind, el) {
		var btn = $(el).find('a.add');
		$(btn).click(addToestelen);
		$('a.remove', el).live('click', removeToestel);
	});
}

function removeToestel(){
	$(this).parent().slideUp(250).remove();
	$('.result ul').each(function(i, el){
		$(el).find('li').removeClass('odd');
		$(el).find('li:odd').addClass('odd');
	});
}

function addToestelen(e){
	var holder = $(this).parent();
	var select = $(holder).find('.toestellen');
	var merk = $(holder).find('.merken').find(':selected').text();
	var result = $(holder).parent().find('ul');
	var i = 0;
	
	$(select).find(':selected').each(function(i, el){
		if($(this).attr('value') != ""){
			el = $("<li><input type=\"hidden\" value=\"" + $(this).attr('value') + "\" name=\"" + $(holder).attr('id') + "toestellen[]\"><a hef=\"#\" class=\"remove toestel" + $(this).attr('value') + "\">verwijder</a><span>" + merk +"</span>" + $(this).text() + "</li>");
			$(result).append(el);
			
			$(el).hide().slideDown(250).fadeIn(250);
			i++;
		}
	});
	$(select).find(':selected').remove();
	$(result).find('li:odd').removeClass('odd').addClass('odd');
	return false;
}

/*--- Tabs ---*/
var strSelect = "";
function initTabs(h_list) {
	$(h_list).each(function(_ind, _el) {
		var btn_h = $(_el);
		var _btn = $(_el).find('a.tab');
		var _a = 0;
		_btn.each(function(_ind, _el) {
			var _box = $('#'+_el.href.substr(_el.href.indexOf("#") + 1));
			if($(_el).hasClass('active')) {
				_box.show();
				_a = _ind;
			}
			else {
				_box.hide();
			}
			_el.onclick = function() {
				if(!$(this).is('[href="'+this.href+'"]')){
					if ($(this).is('.tab') && $(this).parents().is('.applicaties-serv-cnt')) {
						$('a.btn-more-text').removeClass('active');
						$('div.open-text').slideUp(400);
					}
					_btn.not(this).each(function(i, button){
						$('#'+ button.href.substr(button.href.indexOf("#") + 1)).hide();
						$(button).removeClass('active');	
					})
					$('#'+ this.href.substr(this.href.indexOf("#") + 1)).show();
					
					//alert(this.href.substr(this.href.indexOf("#")+1) );
					var txt = $('#'+ this.href.substr(this.href.indexOf("#") + 1)).find('span.sIFR-alternate:last').html();
					//alert(txt);
					if(!txt){
						txt = $('.sIFR-alternate', '#'+ this.href.substr(this.href.indexOf("#") + 1)).html();
					}
					//alert(txt);
					$('span.sIFR-replaced', '#'+ this.href.substr(this.href.indexOf("#") + 1)).html(txt);
					//$('span.sIFR-replaced', '#'+ this.href.substr(this.href.indexOf("#") + 1)).css('visibility','show');
					$('span.sIFR-replaced', '#'+ this.href.substr(this.href.indexOf("#") + 1)).removeAttr('class');
					
					//initSifr();
					strSelect = this.href.substr(this.href.indexOf("#") + 1);
					//setTimeout(function(){
					
						sIFR.replaceElement(named({sSelector: "#"+ strSelect + " strong span", sFlashSrc:"/swf/grotesk.swf", sColor:"#E30074", sLinkColor:"#E30074", nPaddingTop:"0", nPaddingBottom:"0", nPaddingLeft:"0", sWmode:"transparent"}));
					//}, 150);
					$(this).addClass('active');
					resizeOuterIFrame();
					
				}
				return false;
			}
		});
	});
}

function sendGebruiker(){
	var blnError;
	blnError = false;
	
	if(document.frmApp.gebruiker_naam.value == '' && !blnError){
		document.frmApp.gebruiker_naam.focus();
		alert('Geef de naam op');
		blnError = true;
	}
	
	if((document.frmApp.gebruiker_url.value == '' || document.frmApp.gebruiker_url.value == 'http://') && !blnError){
		document.frmApp.gebruiker_url.focus();
		alert('Geef de website op');
		blnError = true;
	}
	
	
	if(!blnError){
		document.frmApp.submit();
	}
}

function sendEigenaar(){
	var blnError;
	blnError = false;

	
	if(document.frmApp.naam.value == '' && !blnError){
		document.frmApp.naam.focus();
		alert('Geef de naam op');
		blnError = true;
	}
	
	if((document.frmApp.url.value == '' || document.frmApp.url.value == 'http://') && !blnError){
		document.frmApp.url.focus();
		alert('Geef de website op');
		blnError = true;
	}
	
	//soort
	
	if(!$('input[name=fkCategorie]').filter(':checked').val() && !blnError){ 
		alert('Je dient wel een genre te selectern');
		blnError = true;
	}
	
	if(document.frmApp.intro.value == '' && !blnError){
		document.frmApp.intro.focus();
		alert('Geef een korte beschrijving op');
		blnError = true;
	}
	
	if(document.frmApp.omschrijving.value == '' && !blnError){
		document.frmApp.omschrijving.focus();
		alert('Geef een beschrijving op');
		blnError = true;
	}
	
	if(document.frmApp.support.value == '' && !blnError){
		document.frmApp.support.focus();
		alert('Geef een adres voor support');
		blnError = true;
	}
	
	
	if(!$('#voorwaarden').attr('checked') && !blnError){ 
		alert('Je dient wel akkoord te gaan met de voorwaarden');
		blnError = true;
	}
	
	if(!blnError){
		document.frmApp.submit();
	}
}
function trim06(el){
	$(el).val($(el).val().replace(/ /g,""));
	$(el).val($(el).val().replace("-",""));
}
function initStars(){
	$('.stars span').each(function(i){
		//alert($(this).attr('class'));
		$(this).css({width: $(this).attr('class') + 'px'});
	})
}