var _teaser = { 
	pics: { 1: false, 2: true, 3: false, 4: true, 5: false, 6: true, 7: false, 8: true, 9: false, 10: false },
	urls: { left: '/kifogasok/', right: '/tenyek/'},
	par: { freq: 10000, crossFadeSpeed: 500 }
}
	

$(function() {
 
 $(document).ready(function(){
	
	new CustomCheck( $('.mailform').find('input[type=checkbox]') );
	new CustomRadioGroup( $('.coach').find('input[type=radio]') );	
	$('.type').find('input[type=checkbox]').each(function(){ new CustomCheck( $(this) ); });
	if (!$.browser.mozilla) { 
		$('.mailform button').click(function(){ $(this).closest('form').submit(); }); 
		$('.type button').click(function(){ $(this).closest('form').submit(); }); 
	}
	
	if ($(".teaser").length > 0) {
		var _teaserInt = setTimeout('teaser()', _teaser.par.freq);
	}
	
	$(".print .scrollmedia").scrollable({
		size: 4,
		items: 'ul',
		prev: '.print .btn_scrollLeft',
		next: '.print .btn_scrollRight',
		speed: 150,
		easing: 'swing',
		clickable: false
	});	
	
	$(".radio .scrollmedia").scrollable({
		size: 4,
		items: 'ul',
		prev: '.radio .btn_scrollLeft',
		next: '.radio .btn_scrollRight',
		speed: 150,
		easing: 'swing',
		clickable: false
	});		
	
	$(".video .scrollmedia").scrollable({
		size: 4,
		items: 'ul',
		prev: '.video .btn_scrollLeft',
		next: '.video .btn_scrollRight',
		speed: 150,
		easing: 'swing',
		clickable: false
	});			
	
	$().frameIt('.frame');
	
	if ($.browser.msie && $.browser.version.substr(0,1)=="6") {
		if ($(".teaser").length > 0) {	
			$(".teaser").find('a').replaceWith('<div></div>').addClass('teaserlink teaserlink_'+$(".teaser div:first-child").attr('class')).css({zIndex: 1000, top:$(".teaser").position().top+168}).insertAfter(".teaser");
		}
		$(".menu li a span").each(function(){ $(this).wrap('<span class="fill"></span>'); });
		$(".random-excuse a, .more-videos a, .nezd-hallasd a, .carnation, .eventsnav a.back").wrapInner('<span class="fill"></span>');
		$("a.pfizer").find("img").replaceWith("<span>Pfizer</span>");
	}

	if ($('.download').length!=0) { $('.download ul li a.btn_view').lightBox(); }
	$('.privacy').lightBox();
	
	$("#excuse-message, #decision-message").keyup(function(){ if ($(this).val().length>200) { $(this).val($(this).val().substring(0,200)); } });
	
	if ($("div.coach").length) {
		
		$.historyInit(setSubpage);
		initHistory();
	
	}
	
	if ( $("div.audioplayer").length ) {
		flashVars = {
			'autostart': 'false',
			/*'frontcolor': 'BE739F',
			'lightcolor': '621643',*/
			'stretching': 'fill',
			'controlbar': 'bottom'
		};
		flashParams = {  };
	
		$("div.audioplayer").each(function() {
			flashVars.file = $(this).find("a").attr('href');
			
			swfobject.embedSWF(
				'swf/jw_player.swf',
				$(this).find("div").attr("id"),
				"270", "20", "9.0.0", "",
				flashVars, flashParams
			);
			
			$(this).find("a").css({display: 'none'});
		});
		
	}
		
 });
});

function initHistory() {
	$("a[rel='history']").click(function(){ 
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
		return false;
	});	
}

function setSubpage(hash) {
	
	if(hash) {
		
		$("div.sub_content_alert_overlay, div.sub_content_alert").replaceWith("");
	
		var tmp = hash.split("/"),
			hash = tmp[0],
			par1 = tmp[1],
			edzok = [ "rozsika", "ferdinand", "orzse" ],
			hash1 = $.inArray(hash, edzok);
			
		if (hash=="valassz") {
			$("div.state2").hide();
			$("div.state2 div.coachinfo").hide();
			$("div.state1").show();
			if (par1) {
				$("div.selectcoach input").removeAttr("checked").parent().find("div.checkbox").html("");
				if (par1>=1 && par1<=3) {
					$("div.selectcoach input#coach"+par1).attr( {checked:"checked"}).parent().find("div.checkbox").append("<b></b>");
				} 
			}
		} else if (hash1>=0 && hash1<=2) {
			$("div.state2").show();
			$("div.state2 div.coachinfo").hide();
			$("div.state2 div.coach"+(hash1+1)).show();
			$("div.state1").hide();
		} else {	
			$("div.state2").hide();
			$("div.state2 div.coachinfo").hide();
		}
		
	} else {
		$("div.state2").hide();
		$("div.state2 div.coachinfo").hide();
		$("div.state1").show();
	}
	
}

$.fn.backgroundPosition = function() {
		var p = $(this).css('background-position');
		if (typeof(p) == 'undefined') return $(this).css('background-position-x') + ' ' + $(this).css('background-position-y');
		else return p;
}

$.fn.frameIt = function(s) {	
	$(s).each(function() {
		if ($(this).hasClass('framepic')) {
			if ($(this).closest('.program').length==1) { var par = { left: 13, top: 14 }; }
			if ($(this).closest('.programs').length==1) { var par = { left: 8, top: 8 }; }
			var img = $(this).find('img').attr('src');
			$(this).wrap('<div class="framecont" style="background: url('+img+') '+par.left+'px '+par.top+'px no-repeat;"></div>');
			$(this).find('img').remove();
		}
	})
}


function array_keys(arr) {
  var re = new Array();
  for (var values in arr) {
    if (values!="in_array") re.push(values);
  }
  return re;
};

function doSetCookie(n,v,d) {
  var today = new Date(), expire = new Date(), dot = (document.location+"").split("/")[2].split(".");;
  if (d==null || d==0) d=1;
  expire.setTime(today.getTime() + 3600000*24*d);
  document.cookie = n+"="+escape(v)+";expires="+expire.toGMTString()+";path=/;domain=."+dot.slice(dot.length-2).join(".");
}

function setCookie() { doSetCookie("burnt","true",7); }

function teaser() {
	
	var np = (_teaserPic+1>array_keys(_teaser.pics).length) ? 1 : _teaserPic+1;
	var ns = (_teaser.pics[np]) ? 'right' : 'left';
	var nt = $(".teaser div").clone();
	
	var ni = '../i/pic/pic'+np+'.png';
	var objImagePreloader = new Image();
	objImagePreloader.onload = function() {
		$('<img>').attr('src',ni);
		setTimeout(function(){
			nt.attr('id','teaser_'+np).removeClass('left right').addClass(ns).css({zIndex: np, backgroundImage: 'url('+ni+')'});
			if($.browser.msie) {
				if ($.browser.version.substr(0,1)=="6") {
					nt.css({background: 'none', filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', src='"+ni+"', sizingMethod='crop')"}).hide();
					$(".teaserlink").attr('href',eval('_teaser.urls.'+ns)).removeClass('teaserlink_left teaserlink_right').addClass('teaserlink_'+ns);
				}
				if ($.browser.version.substr(0,1)=="7") {
					nt.find('a').attr('href',eval('_teaser.urls.'+ns)).end().hide();
				}
					$(".teaser div#teaser_"+_teaserPic).before(nt).hide();
					$(".teaser div#teaser_"+np).show();
					$(".teaser div#teaser_"+_teaserPic).remove();
					_teaserPic = np;
					var _teaserInt = setTimeout('teaser()', _teaser.par.freq);
			} else {
				nt.find('a').attr('href',eval('_teaser.urls.'+ns)).end().animate({opacity:0},0);
				$(".teaser div#teaser_"+_teaserPic).before(nt).animate({opacity:0}, _teaser.par.crossFadeSpeed);
				$(".teaser div#teaser_"+np).animate({opacity:1}, _teaser.par.crossFadeSpeed, function(){
					$(".teaser div#teaser_"+_teaserPic).remove();
					_teaserPic = np;
					var _teaserInt = setTimeout('teaser()', _teaser.par.freq);
				});				
			}
		},_teaser.par.freq);
		objImagePreloader.onload=function(){};
	};
	objImagePreloader.src = ni;		

}

/*var CustomRadioGroup = function( _arr ){
	_arr.each(function(i,s){
		new CustomCheck(s,i);
	});
}
CustomRadioGroup.prototype.resetGroup = function(){
	for (var i in this.group) {
		$(this.group[i].element).html('')
		this.group[i].input.removeAttr('checked');
	}
}*/


var CustomRadioGroup = function( _arr ){
	var that = this;
	this.group = [];
	_arr.each(function(i,s){
		that.group.push( new CustomCheck(s,i) );
	});
	for(var i in  this.group ){
		var o = this.group[i].clikk
		this.group[i].clikk = function(){			
			that.resetGroup();
			o.call(this);			
		}
	}
}

CustomRadioGroup.prototype.resetGroup = function(){
	for (var i in this.group) {
		$(this.group[i].element).html('')
		this.group[i].input.removeAttr('checked')
	}
}


var CustomCheck = function( _obj,i ){
	$(_obj).hide();
	var that = this;
	this.element = document.createElement('div');
	this.element.onclick = function(){ that.clikk(); };
	this.element.className = 'checkbox';
	this.element.rel=$(_obj).attr('id');
	this.element.id='custom_'+$(_obj).attr('id');
	$(_obj).after(this.element);
	this.input = $(_obj);	
	$(_obj).siblings('label').click(function(){  that.clikk(false); });
	return this;
}

CustomCheck.prototype.clikk = function (m) {
	if($(this.element).find('b').length < 1) {
		
		$(this.element).html('<b></b>');
		if (m!==false || $.browser.msie) {
			$("#"+$(this.element).attr('rel')).attr('checked','checked');
		}
	} else {
		$(this.element).html('');
		if (m) {
		$("#"+$(this.element).attr('rel')).removeAttr('checked');
		}
	}
}

function randomExcuse() {
	swfobject.embedSWF("swf/noexcuse.swf", "noexcuse", 535, 525, "9.0.0", "", { data_source: 'index/excuses/hash/rnd' }, { allowfullscreen: true, allowscriptaccess: "always", wmode: "transparent" });
	return false;
}



/* custom alert based on jquery.lightbox */
function customAlert(txt,settings) {
	
		settings = jQuery.extend({
			overlayBgColor: 		'#000',		
			overlayOpacity:			0.6,		
			fixedNavigation:		false,		
			containerBorderSize:	10,			
			containerResizeSpeed:	50		
		},settings);
		
		$('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-alert-box"><div>'+txt+'<a href="javascript:;" class="btn btn_more"><span>bezár</span></a></div></div></div>');	
			
		var arrPageSizes = ___getPageSize();
		$('#jquery-overlay').css({
			backgroundColor:	settings.overlayBgColor,
			opacity:			settings.overlayOpacity,
			width:				arrPageSizes[0],
			height:				arrPageSizes[1]
		}).fadeIn('fast');
		var arrPageScroll = ___getPageScroll();
		$('#jquery-lightbox').css({
			top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
			left:	arrPageScroll[0]
		}).show();
		
		$('#jquery-overlay,#jquery-lightbox,#lightbox-alert-box a.btn').click(function() {
			$('#jquery-lightbox').remove();
			$('#jquery-overlay').fadeOut('fast',function(){$('#jquery-overlay').remove();});
		});

		$(window).resize(function() {
			var arrPageSizes = ___getPageSize();
			$('#jquery-overlay').css({
				width:		arrPageSizes[0],
				height:		arrPageSizes[1]
			});
			var arrPageScroll = ___getPageScroll();
			$('#jquery-lightbox').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	arrPageScroll[0]
			});
		});
	
}

/**
 * getPageSize() by quirksmode.com
 */
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

/**
 * getPageScroll() by quirksmode.com
 */
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};

