/*
 * 2009 Yasin Kuyu
 * http://yasinkuyu.net
 * Date: 08/10/2009
 */
$(document).ready(function () {

	var ops = "#options";
	var loc = window.location;
	var currentPath = window.location.pathname;

	var searchInput = $(".searchinput");
	var searchBtn = $(".searchbtn");
	var commentMessage = $('#commentMessage');
	var submitComment = $('#submitComment');
	var fastSubmit = $("#fastSubmit");
	var fast2 = $("#fast2");
	
	//alert(currentPath);
    if (currentPath == "/turkce" || currentPath == "/english") {
        //$("#notify_bar").html("<div id='lang_notify'><span>" + lng_langnotify + "  <a href='javascript:location.reload(true)'>" + lng_refreshpage + "</span></a></div>");
        //$("#notify_bar").height(50);
		$('#lang_notify').hide().delay(8000);
    }

	$("body").click(function () {
        $(ops +" li ul").hide();
		return true;
    });
	
    // UI Button
    $(".uiSubmitButton, .uiLinkButton").button();
	
	// Set input functions
	$("input[type='text']").clearInput();
	$("#comments textarea").clearInput();

	// options
    $(ops +" li > a").hover(function () {
        $(ops +" li ul").show();
		return true;
    });
	
	// options
	
	// fastSubmit
    $("#fastSubmit #domain").focus(function () {
        fast2.show();
    });
	
	$("#fastSubmit .uiSubmitButton").submit(function () {
		var qs = formBuilder($('#fastSubmit input'));
		$.ajax({
		  url: "/singup",
		  data: qs,
		  complete: function(xmlHttp) {
			alert(xmlHttp.status);
			alert(qs);
			window.location = "/singup"; //+ formBuilder($('#fastSubmit input'));
		  }
		});
    });
	// fastSubmit
	
	// Search
    searchBtn.click(function () {
		searchFnc();
    });

	searchInput.focus(function () {
		searchInput.width(280);
    });
	// Search
	
	searchInput.keyup(function(e) {
		if(e.keyCode == 13) {
		searchFnc();
		}
	});		

    $.fn.readmore.defaults.more_link = '<a class="more something">' + lng_showmore + '</a>';
    $(".short").readmore({ substr_len: 70 });
	$(".Captcha a").click(function () {
        RefreshImage()
		return true;
    });
	
	// Comments
	function submitForm(name)
	{
	  document.name.submit();
	}
	function formBuilder(id){
		arr = [];
		$(id).each(function(){
			arr.push($(this).attr('name')+'='+$(this).val());
		});
		qs = arr.join('&');
		return qs;
	}
	function searchFnc(){
		var sq = searchInput.val();
			sq = sq.replace(";", "")
			sq = sq.replace(":", "")
			sq = sq.replace("'", "")
			sq = sq.replace("/", "")
			sq = sq.replace(/ /g, "-")
			window.location = "/" + lng_urlsearch + "/" + sq;
	}
	function RefreshImage() {
		var objImage = document.images["imgCaptcha"];
		if (objImage == undefined) {
			return;
		}
		var now = new Date();
		objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
	}
	
	submitComment.hide();
    submitComment.click(function () {
		ajaxComment();
    });
	
    commentMessage.focus(function () {
		commentMessage.height(80);
		submitComment.show();
    });	

	function ajaxComment(d){
		$('.commentResult').html('<center><img src="Interface/img/loader.gif"  /></center>');
		$.ajax({
		type:'POST',
		url: 'Includes/AjaxSubmitComment.ashx',
		data: 'cid='+ $('#cid').val() +'&cmt='+ $('#commentMessage').val(),
		success: function(regd){ 
			$('.commentResult').fadeIn().html(regd);
		},
		error: function(regd){
			$('.commentResult').html(regd);
			//$('.commentResult').html(regd).fadeIn('slow');
		}
		});
	   return false;
	}
});

