// JavaScript by Selim Cotal
var global = {
	is_tr : $('.tr').length,
	ie_warning : function() {
		if ($.browser.msie && $.browser.version <= 6) {
			if ($('.tr').length) {
				$.reject({
					reject: {msie5: true, msie6: true}, 
					display: ['firefox','chrome','msie','safari','opera'],
					closeCookie: true,
					header: "Kullanmış olduğunuz Internet Explorer sürümünün modasının geçtiğini biliyor muydunuz?",
					paragraph1: 'Günümüz modern websitelerinden tam olarak faydalanmak istiyorsanız, aşağıdaki son sürüm tarayıcılardan birisini kurmanızı öneririm.',
					paragraph2: 'İndirmek için sadece tıklamanız yeterli.',
					closeMessage: "Bu uyarıyı bir daha görmek istemiyorsanız pencereyi kapatmanız yeterli.",
					closeLink: 'Pencereyi Kapat'
				});			
			} else {
				$.reject({
					reject: {msie5: true, msie6: true}, 
					display: ['firefox','chrome','msie','safari','opera'],
					closeCookie: true
				});
			}
		}
	},
	comment_form : function() {
		var error = false;
		var arr = ['name', 'email', 'website', 'message'];
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		jQuery.each(arr, function() {
			var el = $("#" + this);
			if (el.val() == '') {
				error = true;
				jAlert('Lütfen gerekli alanları doldurunuz.', 'Hata');
				return false;
			}
			if (el.attr('id') == 'email') {
				if (reg.test($('#email').val()) == false) {
					error = true;
					jAlert('Lütfen dogru bir e-posta adresi giriniz.', 'Hata');
					return false;
				}
			}
		});
		if (!error) {
			$('.loading').html('&nbsp;&nbsp;<img src="/res/img/loading.gif" />');
			$.post("/wp-comments-post.php", {
				author  		: $('#author').val(),
				email	  		: $('#email').val(),
				url				: $('#url').val(),
				comment			: $('#comment').val(),
				comment_post_ID : $('#comment_post_ID').val(),
				comment_parent 	: 0
			},
			function(data){
				$('.loading').empty();
				if (data == 1) {
					jAlert("Yorumunuz başarıyla iletilmiştir.", 'Onay');
					jQuery.each(arr, function() { var el = $("#" + this); el.val(''); });
				} else {
					jAlert(data, 'Hata');
				}
			});
		}
	},	
	contact_form : function() {
		var error = false;
		var arr = ['name', 'about', 'email', 'message'];
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		jQuery.each(arr, function() {
			var el = $("#" + this);
			if (el.val() == '') {
				error = true;
				if (global.is_tr) {	jAlert('Lütfen gerekli alanları doldurunuz.', 'Hata'); }
				else { jAlert('Please fill out the form.', 'Error'); }
				
				return false;
			}
			if (el.attr('id') == 'email') {
				if (reg.test($('#email').val()) == false) {
					error = true;
					if (global.is_tr) {	jAlert('Lütfen dogru bir e-posta adresi giriniz.', 'Hata'); }
					else { jAlert('Please specify a correct e-mail address.', 'Error'); }
					return false;
				}
			}
		});
		if (!error) {
			$('.loading').html('&nbsp;&nbsp;<img src="/res/img/loading.gif" />');
			$.post("/wp-xhr.php", {
				action: '093729e8caa1aebe1320795f480b467bfb8623ca',
				name   : $('#name').val(),
				email  : $('#email').val(),
				subject: $('#about').val(),
				message: $('#message').val()
			},
			function(data){
				$('.loading').empty();
				if (data == 1) {
					if (global.is_tr) {	jAlert("Tesekkür ederiz.<br />Mesajınız başarı ile gönderilmiştir.", 'Onay'); }
					else { jAlert('Thank you<br /> Your message has been sent successfully.', 'OK'); }
					jQuery.each(arr, function() { var el = $("#" + this); el.val(''); });
				} else {
					if (global.is_tr) {	jAlert("Bir hata oluştu.<br />Lütfen tekrar deneyiniz.", 'Hata'); }
					else { jAlert('Something went wrong.<br />Please try again.', 'Ooops!'); }			
				}
			});
		}
	},
	lastfm : {
		show 	: 4,
		username: 'selimcotal',
		apikey	: '507cb2c1692b73ed98dc22f289fd203f',
		artSize	: 'medium',
		stripslashes : function(str) { return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1'); },	
		init : function() {
			switch (global.lastfm.artSize) {
				case 'small':
					imgSize = 0;
					break;
				case 'medium':
					imgSize = 1;
					break;
				case 'large':
					imgSize = 2;
					break;
			}
			var lastUrl = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='+global.lastfm.username+'&api_key='+global.lastfm.apikey+'&limit='+global.lastfm.show+'&format=json&callback=?';
			$.ajaxSetup({ cache: true });
			$.getJSON(lastUrl, function(data) {
				$('div#lastfm').empty();
				$.each(data.recenttracks.track, function(i, item){
					if (item.image[1]['#text'] == '') art = 'http://cdn.selimcotal.com/img/noartwork.gif';
					else art = global.lastfm.stripslashes(item.image[imgSize]['#text']);
					if (i++ == 0) firstitem = ' first'; else firstitem = ''; 
					var html =	'<div class="cd'+firstitem+'">' + 
									'<a rel="nofollow" class="blank" href="' + global.lastfm.stripslashes(item.url) + '" title="' + item.artist['#text'] + ' - ' + item.name + '">' + 
										'<img class="albumcover" src="' + art + '" alt="' + item.artist['#text'] + ' - ' + item.name + '" />' + 
										'<span class="jewelcase"></span>' +
									'</a>' +
								'</div>';
					if (i == 4) return;
					$('#lastfm').append(html);
					
				});
			});
		}
	},
	twitter : {
		api_url : 'http://search.twitter.com/search.json',
		username: 'selimcotal',
		init : function() {
			$.ajaxSetup({ cache: true });
			$.getJSON(global.twitter.api_url + '?callback=?&rpp=1&q=from:' + global.twitter.username,	function(data) {
				$.each(data.results, function(i, tweet) {
					if (tweet.text !== undefined) {
						var tweet = global.twitter.linkify(tweet.text);
						$('.tweet_text').html(tweet);
						$('.tweetime').html(global.twitter.time(tweet.created_at));
					}
				});
			});
		},
		time : function(time_value) {
			var values = time_value.split(" ");
			time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
			var parsed_date = Date.parse(time_value);
			var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
			var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
			delta = delta + (relative_to.getTimezoneOffset() * 60);
			var r = '';
			
			var bir_dakika, birkac_dakika, dakika, bir_saat, saat, gun, bir_gun;
			if ($('body.tr').length > 0) {
				bir_dakika : 'az önce';
				birkac_dakika : 'birkaç dakika önce';
				dakika = ' dakika önce';
				bir_saat = '1 saat önce'			
				saat = ' saat önce';
				gun = ' gün önce';
				bir_gun = '1 gün önce';
			} else {
				bir_dakika : 'a minute ago';
				birkac_dakika : 'couple of minutes ago';				
				dakika = '  minutes ago';
				bir_saat = 'an hour ago'
				saat = ' hours ago';
				gun = ' days ago';
				bir_gun = '1 day ago';
			}
			
			if (delta < 60) {
				r = bir_dakika;
			} else if(delta < 120) {
				r = birkac_dakika;
			} else if(delta < (45*60)) {
				r = (parseInt(delta / 60)).toString() + dakika;
			} else if(delta < (90*60)) {
				r = bir_saat;
			} else if(delta < (24*60*60)) {
				r = '' + (parseInt(delta / 3600)).toString() + saat;
			} else if(delta < (48*60*60)) {
				r = bir_gun;
			} else {
				r = (parseInt(delta / 86400)).toString() + gun;
			}
			return r;
		},
		linkify : function(text) {
			text = text.replace(/(https?:\/\/\S+)/gi, function (s) {
				return '<a href="' + s + '">' + s + '</a>';
			});
			text = text.replace(/(^|)@(\w+)/gi, function (s) {
				return '<a href="http://twitter.com/' + s.substr(1) + '">' + s + '</a>';
			});
			text = text.replace(/(^|)#(\w+)/gi, function (s) {
				return '<a href="http://search.twitter.com/search?q=' + s.replace(/#/,'%23') + '">' + s + '</a>';
			 });
			return text;				
		}
	},
	new_window : function() {
		$('a.blank').live('click', function() {
			window.open(this.href);
			return false;
		});	
	},
	init : function() {
		
		global.twitter.init();
		global.lastfm.init();
		
		$('.btn.contact_send').click(function() {
			global.contact_form();
		});

		$('.btn.comment_send').click(function() {
			global.comment_form();
		});

		if ($("a[rel^='prettyPhoto']").length) {
			$("a[rel^='prettyPhoto']").prettyPhoto({
				//animationSpeed: 0,
				showTitle : false,
				theme: 'dark_rounded' /* light_rounded / dark_rounded / light_square / dark_square */									   
			});
	
			/*
			$(document).ready(function(){
				$("a[rel^='prettyPhoto']").prettyPhoto({
					animationSpeed: 'normal', /* fast/slow/normal 
					padding: 40, /* padding for each side of the picture 
					opacity: 0.35, /* Value betwee 0 and 1 
					showTitle: true, /* true/false 
					allowresize: true, /* true/false 
					counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 
					theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square 
					hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto 
					modal: false, /* If set to true, only the close button will close the window 
					changepicturecallback: function(){}, /* Called everytime an item is shown/changed 
					callback: function(){} /* Called when prettyPhoto is closed 
				});
			});
			*/
		}
		
		global.ie_warning();
		global.new_window();
		
		$('.fld .txt, .fld textarea').bind('focus', function(){$(this).addClass('black');});
		$('.fld .txt, .fld textarea').bind('blur', function(){$(this).removeClass('black');});


		$('.drums.tp, .drums.btm').click(function() {
			$('html, body').animate({scrollTop:0}, 1500);
		});
		
		$('.sort a').bind('click', function() {
			if ($(this).is('.delimiter')) return;
			var cname = $(this).attr('class').split(' ').slice(-1);
			if (cname == 'active') return;
			$('.sort a').removeClass('active');
			$(this).addClass('active');
			$('.reel').addClass('hide');
			$('.reel.' + cname).removeClass('hide');
			return false;
		});
		
		$('.explicit_content').bind('click', function() {
			var title, message, href;
			href = $(this).attr('href');
			if (global.is_tr) {
				title = 'Müstehcen İçerik';
				message = '5 Posta erotik öğeler içerir.<br />Sayfayı görüntülemek istediğinizden emin misiniz?';
			} else {
				title = 'Explicit Content';
				message = '5 Posta contains explicit content.<br />Are you sure you want to view the site 5 Posta ?';
			}
			jConfirm(message, title, function(r) {
				if (r) window.open(href);		
			});				
			return false;
		});


		/*
		$('.boxgrid.captionfull').bind('click', function() {
			
			//if ($(this).is('.boxgrid')) {
				var alink = $(this).children('div.cover').children('p').children();	
				
				alink.triggerHandler('click');
				/*
				if (alink.attr('blank')) {
					alink.triggerHandler('click');
				} else {
					window.open(alink.attr('href'));		
				}
				
			}
			
			
			//var href = $(this).find('*[class=blank]').attr('href');
			var elA = $(this).find('*[class=blank], a');
			if ($(this).is('.explicit_content')) {
				var title, message;
				if (global.is_tr) {
					title = 'Müstehcen İçerik';
					message = '5 Posta erotik öğeler içerir.<br />Sayfayı görüntülemek istediğinizden emin misiniz?';
				} else {
					title = 'Explicit Content';
					message = '5 Posta contains explicit content.<br />Are you sure you want to view the site 5 Posta ?';
				}
				jConfirm(message, title, function(r) {
					if (r) elA.trigger('click');
				});				
				return;
			}
		});
		*/
		$('.boxgrid.captionfull').hover(function(){
			$('.cover', this).stop().animate({top:'178px'},{queue:false,duration:300});
		}, function() {
			$('.cover', this).stop().animate({top:'260px'},{queue:false,duration:300});
		});
		 
		$('.boxgrid, .square').hover(
			function() {$(this).addClass('hover');},
			function() {$(this).removeClass('hover');}
		);
		
	}	
};

$(document).ready(function() {global.init();});
if (typeof(console) == 'undefined') { var console = { log : function(s) { alert(s);	} }; }
