// Index
// -----------------------------------------------
var ind = {

	// Init
	// -------------------------------------------
	init : function(){
		try { document.execCommand("BackgroundImageCache", false, true); } catch (e) { }
		ind.build($(document));
	},

	// Build
	// -------------------------------------------
	build : function(div){
		div.find('form').each(function(){
			var frm = $(this);
			if(ind[frm.attr('id')].control){
				frm.bind('submit',ind[frm.attr('id')].control);
			}
			frm.iajax({
				cache: false,
				success: function(rst) {
					var reg;
					if(reg = /^javascript:(.+)$/.exec(rst)) eval(reg[1]);
					if(reg = /^location:(.+)$/.exec(rst)) location.href = ind.root+'/tr'+reg[1];
					if(ind[frm.attr('id')].success){
						eval('var fnc = ind.'+frm.attr('id')+'.success(rst)');
					}
				}
			});
		});
		ind.headline.init(div);
		ind.news.init(div);
		ind.event.init(div);
		ind.survey.init(div);
		ind.comment.init(div);
		ind.image.init(div);
		$('.prg-crs').carousel();
	},

	// Headline
	// -------------------------------------------
	headline : {
		init: function(div){
			var cid = 0;
			var num = 0;
			var hdv = $('.headline');
			var top = $('.headline .hdl-mnu p.top');
			var act = $('.headline .hdl-mnu p.act');
			var ttl = $('.headline .hdl-mnu b');
			var lnk = $('.headline .hdl-mnu a');
			top.html(act.html());
			$('.headline .hdl-prm').hide();

			var ifc = function(){
				var prm = $('.headline .hdl-mnu p.top a');
				if(prm.length == 1)	return;
				ind.headline.get(prm.eq(num).attr('rel'));
				num = num < prm.length-1 ? num+1 : 0;
			};
			var int = setInterval(ifc,3000); ifc();

			ttl.click(function(){
				var prn = $(this).parent('p');
				cid = prn.attr('rel');
				num = 1;

				lnk.removeClass('act');
				ttl.parent('p').removeClass('act');
				prn.find('a').eq(0).addClass('act');
				prn.addClass('act');

				top.html(prn.html());
				top.find('a').click(function(){
					var prm = $('.headline .hdl-mnu p.top a');
					num = prm.index(this)+1;
					if(num >= prm.length) num = 0;
					$(this).blur();
				});

				/*
				if(cid == 0)
				$('.headline .hdl-prm').show(); else
				$('.headline .hdl-prm').hide();
				*/				

				ind.headline.get(prn.find('a').eq(0).attr('rel'));
				$(this).blur();
			});
			hdv.mouseover(function(){
				clearInterval(int);
			});
			hdv.mouseout(function(){
				int = setInterval(ifc,3000);
			});
		},
		get: function(pid){
			$('.headline .hdl-prm a').removeClass('act');
			$('.headline .hdl-prm a[rel='+pid+']').addClass('act');
			$.ajax({
				url: ind.root+'/tr/ajax?action=headline&id='+pid,
				success: function(htm){
					$('.headline .hdl-mnu a').removeClass('act');
					$('.headline .hdl-mnu a[rel='+pid+']').addClass('act');
					$('.headline .hdl-smr').html(htm);
				}
			});
		}
	},

	// News
	// -------------------------------------------
	news : {
		init : function(div){
			var num = 0;
			var ndv = div.find('.news');
			var nws = div.find('.news .nws-mnu a');
			var ifc = function(){
				nws.removeClass('act');
				nws.eq(num).addClass('act');
				ind.news.get(nws.eq(num).attr('rel'));
				num = num < nws.length-1 ? num+1 : 0;
			};
			var int = setInterval(ifc,3000); ifc();
			nws.click(function(){
				nws.removeClass('act');
				$(this).addClass('act');
				num = $('.news .nws-mnu a').index(this)+1;
				if(num >= nws.length) num = 0;
			});
			ndv.mouseover(function(){
				clearInterval(int);
			});
			ndv.mouseout(function(){
				int = setInterval(ifc,3000);
			});
		},
		get : function(nid){
			$('.news .nws-cnt div').hide();
			$('.news .nws-cnt div[rel='+nid+']').show();
		}
	},

	// Event
	// -------------------------------------------
	event : {
		init : function(div){
			var evn = div.find('.event .evn-day a');
			var cnt = div.find('.event .evn-cnt');
			var spd = 0;
			var int = false;
			var mov = function(){
				try{
					var pos = parseInt(ind.event.lst.css('margin-top'))-spd;
					if(pos < ind.event.max && pos > ind.event.min) ind.event.lst.css('margin-top',pos);
				}
				catch(e){}
			}
			evn.click(function(){
				evn.parent('p').removeClass('act');
				$(this).parent('p').addClass('act');
				$(this).blur();
			});
			evn.each(function(){
				if($(this).parent('p').is('.act')) ind.event.day($(this).attr('rel'));
			});
			cnt.mousemove(function(e){
				var hgt = 312;
				var off = cnt.offset();
				var pos = (e.clientY+$(document).scrollTop())-off.top;
				if(pos>(hgt/2)) { spd = (pos-(hgt/2)); }
				else if (pos<(hgt/2)) {spd = -((hgt/2)-pos);}
				spd = parseInt(spd/20);

				//$('#test').html(pos);
			});
			cnt.hover(function(){ int = setInterval(function(){ mov(); },10); }, function(){ clearInterval(int); });
		},
		day : function(day){
			$('.event .evn-cnt div').hide();
			var lst = $('.event .evn-cnt div[rel='+day+']');
			lst.show();
			lst.css('margin-top',0);
			ind.event.lst = lst;
			ind.event.min = 312-ind.event.lst.height();
			ind.event.max = 0;
		}
	},

	// Survey
	// -------------------------------------------
	survey : {
		init : function(div){
			var srv = div.find('.survey .htm li');
			var fnc = function(){
				div.find('.survey .htm b').removeClass('act');
				$(this).find('b').addClass('act');
				ind.survey.ans = $(this).find('b').attr('rel');
			};
			srv.click(ind.survey.answer);
			srv.mouseover(fnc);
		},
		answer : function(){
			var pid = $('.survey .htm p').attr('rel');
			$.ajax({
				url: ind.root+'/tr/ajax?action=survey&id='+pid+'&answer='+ind.survey.ans,
				success: function(htm){
					$('.survey .htm').html(htm);
				}
			});
		}
	},

	// Image
	// -------------------------------------------
	image : {
		init : function(div){

		},
		open : function(fid){
			var div = $('#image');
			var cnt = $('#image .img-cnt');
			var img = $('#image .img-cnt img');
			img.attr('src', ind.root+'/tr/file/'+fid+'-cut480x360.jpg');
			cnt.css('margin-top',$(document).scrollTop()+100);
			div.height($(document).height());
			div.show();
		},
		close : function(){
			var div = $('#image');
			var img = $('#image img');
			img.attr('src', ind.root+'/content/images/spc.gif');
			div.hide();
		}
	},

	// Comment
	// -------------------------------------------
	comment: {
		init: function(div){
			var cmt = div.find('.comment');
			var txt = div.find('.comment .htm div');
			cmt.click(function(e){
				if(txt.css('display') != 'block'){
					txt.show();
					txt.focus();				
					if(e.target.tagName == 'A'){
						ind._cmt = true	
					}
				}
			});
		},
		submit: function(){
			var frm = $('#comment');
			frm.submit();
		},
		success: function(){
			var frm = $('#comment');
			var txt = $('#comment div');			
			txt.hide();			
			frm.find('input[name=name]').val('');
			frm.find('input[name=email]').val('');
			frm.find('input[name=phone]').val('');
			frm.find('textarea[name=comment]').val('');
			alert('Form başarıyla gönderildi');			
		},
		control: function(){
			var frm = $('#comment');		
			var txt = $('#comment div');
			if(ind._cmt == true){
				ind._cmt = false;
				return false;
			}
			if(
				frm.find('input[name=name]').val() == '' ||
				frm.find('input[name=email]').val() == '' ||
				frm.find('input[name=phone]').val() == '' ||
				frm.find('textarea[name=comment]').val() == ''				
			){ 
				alert('Lütfen tüm alanları doldurun');
				return false;
			}			
		}
	}
}