﻿jQuery.fn.extend({
	getQuery: function(qt) {
		hu = window.location.search.substring(1);
		gy = hu.split("&");
		for (i = 0; i < gy.length; i++) {
			ft = gy[i].split("=");
			if (ft[0] == qt) {
				return ft[1];
			}
		}
	},
	scrollTo: function(speed, easing) {
		return this.each(function() {
			var targetOffset = $(this).offset().top;
			$('html,body').animate({ scrollTop: targetOffset }, speed, easing);
		});
	},
	onlyNumber: function() {
		return this.each(function() {
			$(this).bind("keydown", function(e) {
				var keyCode = e.keyCode;
				if ((keyCode < 48 || keyCode > 57) && keyCode != 8 && keyCode != 0) {
					return false;
				}
			});
		});
	},
	onlyEmail: function() {
		return this.each(function() {
			$(this).bind("keydown", function(e) {
				var InvalidChars = " !#/*{[()]},;%^><\\?+:='`|şŞĞğÜüÇçÖöİı";
				kCode = InvalidChars.charAt(5);
				var keyCode = e.keyCode;
				if ((keyCode > 127 && keyCode != 222) || (keyCode == 73)) {
					return false;
				}
				for (i = 0; i <= InvalidChars.length - 1; i++) {
					if (keyCode == InvalidChars.charCodeAt(i)) {
						return false;
					}
				}
				return true;
			});
		});
	},
	onlyLetter: function() {
		return this.each(function() {
			$(this).bind("keydown", function(e) {
				var InvalidChars = "!#/*{[()]},;%^><\\?+:='`|&-_";
				var keyCode = e.keyCode;
				if (keyCode > 47 && keyCode < 58) {
					return false;
				}
				for (i = 0; i <= InvalidChars.length - 1; i++) {
					if (keyCode == InvalidChars.charCodeAt(i)) {
						return false;
					}
				}
			});
		});
	},
	placeHolder: function(attrName) {
		var _this;
		if (attrName == undefined || attrName == null) {
			attrName = "title";
		}
		return this.each(function() {
			_this = $(this);
			_this.val(_this.attr(attrName));
			_this.bind("focus", function() {
				if ($(this).val() == $(this).attr(attrName)) {
					$(this).val("");
				}
			});
			_this.bind("blur", function() {
				if ($(this).val() == "") {
					$(this).val($(this).attr(attrName));
				}
			});
		});
	},
	limitTextArea: function(maxlength) {
		function recalculate(objT) {
			var mobjT = $(objT)
			timeoutID = window.setTimeout(function() { truncateText(mobjT) }, 100);
		}
		function truncateText(mobjT) {
			var maxlength = mobjT.attr('limit');
			var toChange = mobjT.next();
			if (mobjT.val().length > maxlength) {
				mobjT.val(mobjT.val().substring(0, maxlength));
			}
			toChange.html((maxlength - mobjT.val().length) + ' Karakter kaldı.');
		}
		return this.each(function() {
			$(this).attr('limit', maxlength);
			$(this).after('<p class="remaining">' + maxlength + ' karakter kaldı.</span>');
			$(this).bind('keyup cut paste', function() {
				recalculate($(this));
			});
			recalculate(this);
		});
	},
	currency: function() {
		function del_comma(str) {
			while (str.indexOf(".") > 0) { str = str.replace(".", ""); }
			if (str.length < 2 && str[0] == 0) {
				str = 0;
			}
			return str;
		}
		this.each(function() {
			$(this).bind("keyup", function() {
				try {
					var tmpVal = "";
					var index = 0;
					var inputVal = $(this).val().toString();
					var dot_pos = inputVal.indexOf(",");
					if (dot_pos > 0)
						inputVal = inputVal.substr(0, dot_pos);

					inputVal = del_comma(inputVal);
					var len = Math.floor(((inputVal.length - 1) / 3));
					var begin = inputVal.length - len * 3;

					tmpVal = inputVal.substr(0, begin);
					var i = 0;
					for (i = 0; i < len; i++) {
						tmpVal = tmpVal + ".";
						var start_pos = i * 3 + begin;
						var tmp = inputVal.substr(start_pos, 3);
						tmpVal = tmpVal + tmp;
					}
					inputVal = tmpVal;
					$(this).val(inputVal);
				}
				catch (e) {
					return 0;
				}
			});
		});
	}
});

// social

var social = {
    FACEBOOK: 1,
    TWITTER: 2,
    FRIENDFEED: 3,
    YAHOO: 4,
    GOOGLE: 5,
    DELICIOUS: 6,
    LIVE: 7,
    MYSPACE: 8,
    BLOGGER: 9,
    _url: "",
    _title: "",
    setURL: function(url) {
        this._url = url;
    },
    getURL: function() {
        return escape(this._url);
    },
    setTitle: function(title) {
        this._title = title;
    },
    getTitle: function(){
        return encodeURI(this._title);
    },
    share: function(o) {
        var socialURL;
        switch (o) {
            case this.FACEBOOK:
                socialURL = "http://www.facebook.com/sharer.php?u=" + this.getURL() + "&amp;title=" + this.getTitle();
                break;
            case this.TWITTER:
                socialURL = "http://twitter.com/home?status=" + this.getTitle() + "+" + this.getURL();
                break;
            case this.FRIENDFEED:
                socialURL = "http://friendfeed.com/?url=" + this.getTitle() + " - " + this.getURL();
                break;
            case this.YAHOO:
                socialURL = "http://myweb2.search.yahoo.com/myresults/bookmarklet?&amp;u=" + this.getURL() + "&amp;t=" + this.getTitle();
                break;
            case this.GOOGLE:
                socialURL = "http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=" + this.getURL() + "&amp;title=" + this.getTitle();
                break;
            case this.DELICIOUS:
                socialURL = "http://del.icio.us/post?url=" + this.getURL() + "&amp;title=" + this.getTitle();
                break;
            case this.LIVE:
                socialURL = "https://favorites.live.com/quickadd.aspx?marklet=1&mkt=tr-tr&top=1&url=" + this.getURL() + "&title=" + this.getTitle();
                break;
            case this.MYSPACE:
                socialURL = "http://www.myspace.com/Modules/PostTo/Pages/?u=" + this.getURL() + "&title=" + this.getTitle();
                break;
            case this.BLOGGER:
                socialURL = "https://www.google.com/accounts/ServiceLogin?service=blogger&continue=https%3A%2F%2Fwww.blogger.com%2Floginz%3Fd%3Dhttp%253A%252F%252Fwww.blogger.com%252Fblog_this.pyra%253Ft%2526u%253D" + this.getURL() + "&title=" + this.getTitle();
                break;
        }
        window.open(socialURL, "_blank");
    }
};


    social.setTitle(document.title);
    social.setURL(document.location);
