/*******************************************************************************

	CSS on Sails Framework
	Title: TeknoSeyir
	Author: XHTMLized (http://www.xhtmlized.com/)
	Date: February 2011

*******************************************************************************/

jQuery.fn.extend({
	/* Return a valid #hash from href attribute in IE */
	hrefHash: function(){
		return jQuery(this).attr('href').substr(jQuery(this).attr('href').indexOf('#'));
	},
	/* Set equal heights to columns */
	equalHeights: function(){
		var tallest= 0; 
		
		jQuery(this).each(function(){
			if(jQuery(this).height() > tallest){
				tallest = jQuery(this).height();
			}
		}).css({'min-height': tallest});
	},
	/* Hide field label on focus */
	labels: function(){
		return this.each(function(){
			var label = jQuery(this),
				input = jQuery('#' + label.attr('for'));
				
			input
				.focus(function(){
					label.hide();
				})
				.blur(function(){
					if(input.val()===''){
						label.show();
					}
				});
			
			setTimeout(function(){
				if(input.val() !== ''){
					label.hide();
				}
			}, 10);
		});
	}
});



var Engine = {
	slides: function() {
		var container = jQuery('.featured-content'),
			carousel = jQuery('.carousel-main', container),
			thumbs = jQuery('.carousel-thumbs').length ? jQuery('.carousel-thumbs') : null,
	        btnNext = jQuery('<a href="javascript:void(0)" class="next">İleri</a>').appendTo(carousel),
	        btnPrev = jQuery('<a href="javascript:void(0)" class="prev">Geri</a>').appendTo(carousel),
	        temp = '';

		if(thumbs) {
			var	pointer = jQuery('<span class="pointer" />').appendTo(thumbs),
				baseOffset = thumbs.offset().left,
				pointerOffset = parseInt(jQuery('a:first', thumbs).width() / 2) - 9;
		
			var animatePointer = function(offset) {
				pointer.animate({ left: offset }, 'normal');
			};
		
			jQuery('a', thumbs).each(function(){
				jQuery(this).attr('id', 'carousel-thumb-' + jQuery(this).hrefHash().replace('#carousel-item-', ''));
				jQuery(this).data('offset', parseInt(jQuery(this).offset().left - baseOffset + pointerOffset));
			});

		    thumbs.find('a').bind('click', function() {
				if(!jQuery(this).is('.current')) {
					jQuery('.pagination', container).find('a[href="#' + jQuery(this).hrefHash().replace('#carousel-item-', '') + '"]').click();
					jQuery('.current', thumbs).removeClass('current');
					jQuery(this).addClass('current');
					animatePointer(jQuery(this).data('offset'));
				}
	
		        return false;
		    });
		    
		    jQuery('a:first', thumbs).addClass('current');
		    
			btnNext.click(function(){
				temp = jQuery('.pagination .current', container);
				
				if(temp.is(':last-child')) {
					jQuery('.current', thumbs).removeClass('current');
					jQuery('li:first a', thumbs).addClass('current');
					
					animatePointer(jQuery('.current', thumbs).data('offset'));
				} else {
					jQuery('.current', thumbs).addClass('old').parent().next().find('a').addClass('current');
					jQuery('.old', thumbs).removeClass('current old');
					
					animatePointer(jQuery('.current', thumbs).data('offset'));
				}
	
				return false;
			});
			
			btnPrev.click(function(){
				temp = jQuery('.pagination .current', container);
				
				if(temp.is(':first-child')) {
					jQuery('.current', thumbs).removeClass('current');
					jQuery('li:last a', thumbs).addClass('current');
					
					animatePointer(jQuery('.current', thumbs).data('offset'));
				} else {
					jQuery('.current', thumbs).addClass('old').parent().prev().find('a').addClass('current');
					jQuery('.old', thumbs).removeClass('current old');
					
					animatePointer(jQuery('.current', thumbs).data('offset'));
				}
			
				return false;
			});

			animatePointer(jQuery('.current', thumbs).data('offset'));
		}

		jQuery('.carousel-main').slides({
			effect: 'slide, fade',
			hoverPause: true,
			autoHeight: true,
			fadeSpeed: 250
		});
	},

	equalColumns: function() {
		jQuery('.community .primary, .community .sidebar').equalHeights();
	},
	
	hideLabels: function() {
		jQuery('.toolbar label').labels();
	},
	
	fixes: {
		lastChild: function() {
			if(!jQuery.browser.msie) {
				return; 
			}
			
			jQuery('.videos li:last-child, .sidebar-b .hentry:last-child').addClass('last');
		}
	}
};

/* General */

function importJS(uri, extra) {
	var s = document.createElement('script');
    s.type = 'text/javascript';
    s.async = true;
    s.src = uri;
	if(extra) {
		var ext = document.createTextNode(extra);
		s.appendChild(ext);
	}
    var x = document.getElementsByTagName('script')[0];
    x.parentNode.insertBefore(s, x);
}

function loading(div, height) {
	if(! height) height = 500;
	
	var pad = (height - 48) / 2;
	div.html('<div class="loading"><img src="' + baseUri + '/images/loading/white-big.gif" width="48" height="48" border="0" alt="Yükleniyor..." style="padding: ' + pad + 'px 0px;" /></div>');
}

function relative_date(ts) {
	var now = new Date();
	var timeSince = now.getTime() - ts;
	var inSeconds = timeSince / 1000;
	var inMinutes = inSeconds / 60;
	var inHours = inMinutes / 60;
	var inDays = inHours / 24;
	var inMonths = inDays / 30;
	var inYears = inDays / 365;
	
	var secs = " saniye önce";
	var mins = " dakika önce";
	var hours = " saat önce";
	var days = " gün önce";
	var months = " ay önce";
	var years = " yıl önce";

	if(inMinutes < 1) {
		return (Math.round(inSeconds) + secs);
	} else if(inHours < 1) {
		return (Math.round(inMinutes) + mins);
	} else if(inDays < 1) {
		return (Math.round(inHours) + hours);
	} else if(inMonths < 1) {
		return (Math.round(inDays) + days);
	} else if(inYears < 1) {
		return (Math.round(inMonths) + months);
	} else {
		return (Math.round(inYears) + years);
	}
}

/* Social */

function fb_share(u, t) {
	if(! u) u = location.href;
	if(! t)	t = document.title;
	window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
	return false;
}

/* Category Loader */

var category_loader_id;
var category_loader_paged;

function load_category(id, paged) {
	var div = jQuery('#article-container');
	loading(div);
	jQuery.post(ajaxUri, {action: 'load_category', c: id, paged: paged}, function(response) {
		div.html(response);
		var posttypedesc = id;
		jQuery('#category-list a.current').removeClass('current');
		jQuery('#category-descriptor-' + posttypedesc).addClass('current');
		category_loader_id = id;
		category_loader_paged = paged;
	});
}

/* Category Loader for Sidebar*/

var category_loader_sidebar_id;

function load_category_sidebar(id) {
	var div = jQuery('#article-ajax-container');
	loading(div);
	jQuery.post(ajaxUri, {action: 'load_category_sidebar', c: id}, function(response) {
		div.html(response);
		var posttypedesc = id;
		jQuery('#category-list a.current').removeClass('current');
		jQuery('#category-descriptor-' + posttypedesc).addClass('current');
		category_loader_sidebar_id = id;
	});
}

function load_latest_sidebar(exclude) {
	var div = jQuery('#article-ajax-container');
	loading(div);
	jQuery.post(ajaxUri, {action: 'load_latest_sidebar', exclude: exclude}, function(response) {
		div.html(response);
		var posttypedesc = 0;
		jQuery('#category-list a.current').removeClass('current');
		jQuery('#category-descriptor-' + posttypedesc).addClass('current');
		category_loader_sidebar_id = 0;
	});
}

/* Microlist Loader */

var microlist_loader_tag;
var microlist_loader_paged;

function load_microlist(tag, paged) {
	var div = jQuery('#microlist-ajax-container');
	loading(div);
	jQuery.post(ajaxUri, {action: 'load_microlist', tag: tag, paged: paged}, function(response) {
		div.html(response);
		category_loader_tag = tag;
		category_loader_paged = paged;
	});
}

/* Alternative */

function focus_alternative(obj) {
	var defval = jQuery(obj).attr('alt');
	if(jQuery(obj).val() == defval) jQuery(obj).val('');
}

function blur_alternative(obj) {
	var defval = jQuery(obj).attr('alt');
	if(jQuery(obj).val() == '') jQuery(obj).val(defval);
}
