var log = function(txt) {
	(console || console.log) ? console.log(txt) : alert(txt);
};

var focusFirstInput = function(obj) {
	var container =  document.getElementById(obj) || document.getElementById("content-sect-body");
	var forms = container.getElementsByTagName("form");
	if (forms.length > 0 && forms[0].elements[0]){
		var eltype = forms[0].elements[0].type;
		(eltype=="text" || eltype=="textarea" || eltype=="password") ? forms[0].elements[0].focus() : void(0);
	}
}

__init_input_text__ = function(obj, txt){
	var txt = txt ? txt : "search";
	var inputBox = $(obj);

	if (!inputBox) {
		return false;
	}

	inputBox.val(txt);

	inputBox.focus(function(){
		$(this).val() == txt ? $(this).val("") : void(0);
	});

	inputBox.blur(function(){
		$(this).val() == "" ? $(this).val(txt) : void(0);
	});

};

var getTemplate = function(url, obj, callback) {
	$.ajax({
		type: "GET",
		url: url,
		dataType: "html",
		contentType: "text/html; charset=utf-8",
		async: false,
		cache: false,
		timeout: 1000,

		success: function(template) {
			$(obj).html(template);
		},

		complete: function(template){
			if (callback) {
				callback();
			};
		}
	});

};

var coverFader = function(){
	clearInterval(coverTabTimer);
	var index = $(".cover-img img.active").index();
	var next = index+1;
	next = next < $('.cover-img img').length ? next : 0;
	var nextItem = $('.cover-img img')[next];

	$(".cover-img img.active").fadeOut(800, function(){
		$(this).removeClass('active');
	});

	$(nextItem).fadeIn(1200, function(){
		$(nextItem).addClass('active');
	});
	coverTabTimer = setInterval(coverFader, 5000);
}

var cufon_init = function(){
  Cufon.replace('#content-sect h1');
  Cufon.replace('#content-sect h2');
  Cufon.replace('#content-sect h3');
  Cufon.replace('#content-sect h4');
  Cufon.replace('#content-sect h5');
  Cufon.replace('.newsbox h3');
  Cufon.replace('.dialog-header h3');
}

var __init__ = function() {
	$(".cover-img img:first-child").addClass('active');
	if ($('body#home .cover-img').length>0) {
		coverTabTimer = setInterval(coverFader, 5000);
	}

	cufon_init();

	$(function() {
		$('#photo-gallery a').lightBox({
			fixedNavigation:true,
			txtOf:'/',
			txtImage:'Resim',
			imageLoading:'/static/images/base/loading.gif',
			imageBtnPrev:'/static/images/addons/lightbox-btn-prev.gif',
			imageBtnNext:'/static/images/addons/lightbox-btn-next.gif',
			imageBtnClose:'/static/images/addons/lightbox-btn-close.gif',
			imageBlank:'/static/images/addons/lightbox-blank.gif'
			});
	});

	$('a[class*="modal-dialog"]').click(function(e) {
		e.preventDefault();
		$('#dialog-window').html('');
		var url = $(this).attr('href');
		var rel = $(this).attr('rel');
		$("#dialog-window").modal({
			appendTo: $(document).body,
			closeClass: "modalClose",
			closeHTML: "<a id='modalClose' class='align-right'>close</a>",
			opacity: 40,
			overlayId: "overlay-mask",
			width: '660',
			height: '600',
			position: ['50px'],
			autoPosition: false,
			autoResize: true,
			escClose: true,
			overlayClose: true,
			focus: false,
			persist: false,
			overlayCss: { backgroundColor: "#000" },
			zIndex: 9999,
			modal:true,
			onOpen: function(dialog) {
				dialog.data.html('<div class="dialog-loading-box">&nbsp;</div>');
				dialog.overlay.fadeIn(500, function() {
					dialog.container.fadeIn(250, function() {
						dialog.data.fadeIn(250, function() {

							getTemplate(url, '#dialog-window', function() {
								$('#dialog-window').animate({
									'height': '100%'
								});
								__modal_init__();
							});
						});
					});
				});
			},

			onShow: function(dialog) {
				__modal_init__();
			},

			onClose: function(dialog) {
				dialog.data.html('');
				$.modal.close();
			}
		});
		return false;
	});


};

__modal_init__ = function(){
	cufon_init();
	return true;
}

$(document).ready(function(){

	$('#newsletter-email') ? __init_input_text__('#newsletter-email', 'e-posta adresiniz') : void(0);

	__init__();
});
