var log = function(msg) {
	console.log ? console.log(msg) : alert(msg);
};

var QA = function() {
	$('h4.question').click(function() {

		$('div.answer').each(function() {
			$(this).css({
				'height':'0px',
				'opacity': '0'
			});
		});

		$(this).next('div.answer').css({
			'height': 'toggle'
		});

		$(this).next('div.answer').animate({
			'height': 'toggle',
			'opacity': 'toggle'
		}, 500);

	});
};

var cook={

	set: function(name,value,expr) {
		if(expr) {
			var date = new Date();
			date.setTime(date.getTime()+(expr*24*60*60*1000));
			var expires = ";expires=" + date.toGMTString();
		}
		else var expires="";
		document.cookie = name + "=" + value + expires + ";path=/";
	},

	get: function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0; i<ca.length; i++){
			var c = ca[i];
			while(c.charAt(0)==' ') c=c.substring(1,c.length);
			if(c.indexOf(nameEQ)==0) return c.substring(nameEQ.length,c.length);
		};
		return null;
	},

	burn: function(name) {
		(this.get(name)) ? (this.set(name, "" ,-1)) : void(0);
	}
};

var externalLinks = function(){
	if(!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0;i<anchors.length;i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel")=="external"){

			if( anchor.onclick == undefined ){
				   anchor.onclick = function(e){
				 window.open(this.href);
				 return false;
			   }
			}	
		}
	}
};

var coverTabs = function (tabInterval) {
	var tabContainers = $('div.home-cover-sect div.home-cover-content div.tab-content > div');
	// var tabContainers = $('div.home-cover-sect div.home-cover-content div.tab-content > div, div.news-full div.top-read-news');
	tabContainers.removeClass('active').hide().filter(':first').show().addClass('active');

	// $('div.home-cover-sect div.home-cover-nav ul li a, div.news-nav ul li a').click(function () {
	$('div.home-cover-sect div.home-cover-nav ul li a').click(function () {
		clearInterval(tabInterval);

		tabInterval = setInterval('fadeMate()', 5000);

		tabContainers.hide().removeClass('active');
		tabContainers.filter(this.hash).show().addClass('active');
		$('div.home-cover-sect div.home-cover-nav ul li, div.news-nav ul li').removeClass('active');

		$(this).parent().addClass('active');

		return false;
	}).filter(':first').click().addClass('active');
}

var callTemplate = function(url, obj, callback) {
	$.ajax({
		type: "GET",
		url: url,
		dataType: "html",
		contentType: "text/html; charset=utf-8",

		success: function(template) {
			$(obj).html(template);
		},

		complete: function(template){

			if (callback) {
				callback();
			};
		}
	});
};

var openDialog = function() {
	$("#dialog-window").modal({
		appendTo: $(document).body,
		closeClass: "modalClose",
		closeHTML: "<a id='modalClose'>Close</a>",
		opacity: 80,
		overlayId: "overlay-mask",
		minHeight: 400,
		minWidth: 450,
		autoResize: false,
		escClose: true,
		overlayClose: false,
		focus: true,
		overlayCss: { backgroundColor: "#000" },
		containerCss: {width:450, backgroundColor: "#fff", padding:"0 10px 0 0", border:"4px solid #1a5385"},
		zIndex: 999999999,
		zIndex: 999999999,
		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);
				});
			});
		},
		onClose: function(dialog) {
			dialog.data.html('');
			$.modal.close();
		}
	});
	return true;
};

var fadeMate = function(){

	var contObj = $('.tab-content div.cover-item.active');
	var nextContObj = contObj.next('div.cover-item').length>0 ? contObj.next('div.cover-item') : contObj.parent().children('div.cover-item:first-child');

	var tabObj = $('.home-cover-nav ul li.active');
	var nextTabObj = tabObj.next('li').length>0 ? tabObj.next('li') : tabObj.parent().children('li:first-child');

	contObj.removeClass('active').fadeOut();
	nextContObj.addClass('active').fadeIn();

	tabObj.removeClass('active');
	nextTabObj.addClass('active');

}

var __init__ = function() {

    var _lng_ = $("body").attr('lang');

    externalLinks();

    $('a.toggle-qa').click(function() {
        var answerObj = $(this).parent().prev();
        if (answerObj.hasClass('hidden')) {

            answerObj.removeClass('hidden');
            $(this).next().removeClass('hidden');
            $(this).addClass('hidden');

        } else {

            answerObj.addClass('hidden');
            $(this).prev().removeClass('hidden');
            $(this).addClass('hidden');
        }

    });


    $('#btn-domain-search').click(function() {
        $('#domain-search-from').submit();
    });

    $('#domain-search-from').submit(function() {

        if ($("#qd").val() == "") {
            _lng_ == "tr" ? alert("Lütfen aramak istediğiniz alan adını yazınız") : alert("Please specify domain name to search");
            $("#qd").focus();
            return false;
        }

        if ($("select#domain-ext option:selected").val() == "") {
            _lng_ == "tr" ? alert("Lütfen alan adı uzantısı seçiniz") : alert("Please select domain extension");
            $("select#domain-ext").focus();
            return false;
        }

        return true;
    })


    $('#txtsearch').focus(function() {
        $(this).val().toLowerCase() == 'arama' ? $(this).val('') : void (0);
    });

    $('#txtsearch').blur(function() {
        $(this).val() == '' ? $(this).val('Arama') : void (0);
    });

    $('.thumbs-list-body a.show-more-link').click(function() {
        $(this).prev('div.thumb-list').children('div.thumbs-more').removeClass('hidden');
        $(this).next('a').removeClass('hidden');
        $(this).addClass('hidden');
    });

    $('.thumbs-list-body a.show-less-link').click(function() {
        $(this).prev().prev('div.thumb-list').children('div.thumbs-more').addClass('hidden');
        $(this).prev('a').removeClass('hidden');
        $(this).addClass('hidden');
    });

    
//       	$('a[rel*="dialogWindow"]').live("click", function(e) {        
//            e.preventDefault();

//            var url = $(this).attr('href');
//            var rel = $(this).attr('rel');

//            //var timer;

//            $("#dialog-window").modal({
//                appendTo: $(document).body,
//                closeClass: "modalClose",
//                closeHTML: "<a id='modalClose'>Close</a>",
//                opacity: 80,
//                overlayId: "overlay-mask",
//                minHeight: 500,
//                minWidth: 450,
//                autoResize: false,
//                escClose: true,
//                overlayClose: false,
//                focus: true,
//                overlayCss: { backgroundColor: "#000" },
//                containerCss: { width: 450, backgroundColor: "#fff", padding: "0 10px 0 0", border: "4px solid #1a5385" },
//                zIndex: 999999999,
//                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() {
//                                callTemplate(url, '#dialog-window');
//                                // timer = setTimeout("callTemplate('"+ url +"','#dialog-window')", 250);
//                            });
//                        });
//                    });
//                },
//                onClose: function(dialog) {
//                    // clearTimeout(timer);
//                    dialog.data.html('');
//                    $.modal.close();
//                }
//            });
//        });

    $('ul#prim-nav li span span.more-icon').click(function() {
        var parentObj = $(this).parent().parent();

        if (!parentObj.hasClass('active')) {
            if (parentObj.hasClass('pop')) {
                parentObj.removeClass('pop');
            } else {
                $('ul#prim-nav li.pop').removeClass('pop');
                parentObj.addClass('pop');
            }
        }

    });

    $('.newsbox-pager a').live('click', function(e) {
        var requestURL = $(this).attr('href');
        var fromId = $('#newsbox-detail-link').attr('rel');
        requestURL += "&id=" + fromId;
        $.ajax({
            type: "GET",
            url: requestURL,
            dataType: "json",
            contentType: "application/json; charset=utf-8",

            success: function(res) {
                if (res.Success == "True") {
                    $('#newsbox-item-img').attr('src', res.LargeImage);
                    $('#newsbox-item-date').text(res.NewsDate);
                    $('#newsbox-description').html(res.DescriptionMini);
                    $('#newsbox-header').html(res.Header);
                    $('#newsbox-detail-link').attr('href', res.FullUrl);
                    $('#newsbox-detail-link').attr('rel', res.Id);
                }
            }
        });
        return false;
    });


    $('.category-cover-pager li a').live('click', function(e) {
        e.preventDefault();
        var requestURL = $(this).attr('href');
        var fromId = $('#category-cover-item-more-link').attr('rel');
        requestURL += "&id=" + fromId;

        $.ajax({
            type: "GET",
            url: requestURL,
            dataType: "json",
            contentType: "application/json; charset=utf-8",

            success: function(res) {
                if (res.Success == "True") {
                    $('#category-cover-title').text(res.Header)
                    $('#category-cover-item-date').text(res.NewsDate);
                    $('#category-cover-description').html(res.Description)
                    $('#cover-galery-main-img').attr('src', res.LargeImage);
                    $('#category-cover-item-more-link').attr('href', res.FullUrl);
                    $('#category-cover-item-more-link').attr('rel', res.Id);
                }
            }
        });
    });

};


$(document).ready(function() {

	var tabInterval = setInterval('fadeMate()', 5000);
	coverTabs(tabInterval);

	__init__();

});

function numbersonly(myfield, e, dec) {
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;
    keychar = String.fromCharCode(key);

    // control keys
    if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27))
        return true;

    // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
        return true;

    // decimal point jump
    else if (dec && (keychar == ".")) {
        myfield.form.elements[dec].focus();
        return false;
    }
    else
        return false;
}


