var log = function(txt) {
	console.log ? console.log(txt) : alert(msg);
};

var ARTICLE_FONT_SIZE = 'sol_styles';
var COOKIE_STYLES_OPTIONS = { path: '/', expires: 10 };

var getTypoStyleCookie = function() {
	if ($.cookie(ARTICLE_FONT_SIZE)) {
		setTypoStyle($.cookie(ARTICLE_FONT_SIZE))
	};
};

var focusFirstInput = function(obj) {
	var forms = (obj && document.getElementById(obj)) ? document.getElementById(obj).getElementsByTagName('form') : document.forms;
	if (forms.length > 0 && document.forms[0].elements[0]){
		var eltype = document.forms[0].elements[0].type;
		(eltype=="text" || eltype=="textarea" || eltype=="select-one" || eltype=="select-multiple" || eltype=="password") ? forms[0].elements[0].focus() : null;
	}
}

var setCoverItem = function() {
	$('#cover-controller a').mouseover(function() {
		$('#sect-cover-content li').css({
			'display': 'none'
		});
		$('#sect-cover-content li')[$(this).text()-1].style.display='block';

		$('#cover-controller a').each(function() {
			$(this).removeClass('active');
			// ie6'da active için background-position sıçıyor. 
			// style='background-position:' ile ilk yüklenişte stil veriyorum
			// mouseover'da bu stil'in kaldırılması lazım
			$(this).removeAttr("style");
		});

		$(this).addClass('active');
	});
};

var loadtableDetail = function(updateTo, getURL, obj) {
	$.ajax({
		type: "GET",
		url: getURL,
		dataType: "html",
		contentType: "text/html; charset=utf-8",

		beforeSend: function() {
			$(updateTo).addClass('loading');
		},

		success: function(res){
			$(updateTo).removeClass('loading');
			$(updateTo).html(res);
			$(obj).remove();
		}
	});
	return true;
};

var getAJAXData = function(getURL, updateTo, parentNodeID, activate) {
	$.ajax({
		type: "GET",
		url: getURL,
		dataType: "html",
		contentType: "text/html; charset=utf-8",

		beforeSend: function() {
			$(updateTo).addClass('loading');
		},

		success: function(res){
			$(updateTo).removeClass('loading');
			$(parentNodeID + ' li').each(function() {
				$(this).removeClass('active');
			});
			$(activate).parent().addClass('active');
			$(updateTo).html(res);
		}
	});
	return true;
};

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 setTypoStyle = function(type) {
	var articleHeaderObj = $('h1.article-header');
	var articleSpotObj = $('h2.article-spot')
	var articleBodyObj = $('div#article-body');

	var date = new Date();
	date.setTime(date.getTime() + (360 * 24 * 60 * 60 * 1000));

	switch (type){
		case 'small':
			$.cookie(ARTICLE_FONT_SIZE, 'small', { path: '/', expires: date });
			articleHeaderObj.css({'font-size': '3em'});
			articleSpotObj.css({'font-size': '1.4em'});
			articleBodyObj.css({'font-size': '.8em'});
		break;

		case 'normal':
			$.cookie(ARTICLE_FONT_SIZE, 'normal', { path: '/', expires: date });
			articleHeaderObj.css({'font-size': '3.4em'});
			articleSpotObj.css({'font-size': '1.8em'});
			articleBodyObj.css({'font-size': '1em'});
		break;

		case 'large':
			$.cookie(ARTICLE_FONT_SIZE, 'large', { path: '/', expires: date });
			articleHeaderObj.css({'font-size': '3.8em'});
			articleSpotObj.css({'font-size': '2.1em'});
			articleBodyObj.css({'font-size': '1.2em'});
		break;

	}
	return true;
};

function fotogaleri_initCallback(carousel) {
	jQuery('.fotogaleri-widget .carousel-control a').bind('mouseover', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});

	jQuery('.fotogaleri-widget .carousel-next').bind('click', function() {
		carousel.next();
		return false;
	});

	jQuery('.fotogaleri-widget .carousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
};


function videogaleri_initCallback(carousel) {
	jQuery('.videogaleri-widget .carousel-control a').bind('mouseover', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});

	jQuery('.videogaleri-widget .carousel-next').bind('click', function() {
		carousel.next();
		return false;
	});

	jQuery('.videogaleri-widget .carousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
};

function games_initCallback(carousel) {
	jQuery('.games-widget .carousel-control a').bind('mouseover', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});

	jQuery('.games-widget .carousel-next').bind('click', function() {
		carousel.next();
		return false;
	});

	jQuery('.games-widget .carousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
};


var searchTabSelector = function() {
	$('#search-box li label').click(function() {
		$('#search-box li').each(function() {
			$(this).removeClass("active");
		});

		$(this).parent().addClass("active");
	});

};

var nextCover = function(get) {
	var i = parseInt($('#cover-controller a.active').text());

	$('#sect-cover-content li').css({
		'display': 'none'
	});

	$('#cover-controller a').each(function() {
		$(this).removeClass('active');
	});

	var activate = (get=="next") ? i : i-2;
	activate = (activate == -1) ? $('#cover-controller a').length-1 : ( activate == $('#cover-controller a').length ? 0 : activate );

	$('#sect-cover-content li')[activate].style.display='block';
	$('#cover-controller a')[activate].className=$('#cover-controller a')[activate].className + ' active';
	$('#cover-controller a')[activate].focus();
};

var buildMembershipSteps = function(i) {
	$('ul.steps').each(function() {
		$(this).css({
			'display':'none'
		});
	});

	$('.steps')[(i ? i : 0)].style.display="block";
};

var toggleLoginBar = function() {
	$('#login-bar').toggle();
	return true;
};

var toggleScoreDetail = function() {
	$('#match-detail').toggle();
	return true;
};

$(document).ready(function() {
	setCoverItem();
	getTypoStyleCookie();
	searchTabSelector();
	focusFirstInput('content-body');

	$(".carousel").jcarousel({
		scroll: 1,
		initCallback: fotogaleri_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	$(".videogaleri-widget").jcarousel({
		scroll: 2,
		initCallback: videogaleri_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});

	$(".games-widget").jcarousel({
		scroll: 2,
		initCallback: games_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});

	if($('#sect-cover-content').length > 0 && $('#cover-controller').length > 0){
		$(document).keydown(function(e){
			var code = (e.keyCode ? e.keyCode : e.which);
			// log(e.which);
			switch(code){
				// left
				case 37: nextCover("prev");
				break;

				// left
				case 39: nextCover("next");
				break;
			}
		});
	}

	$('#fotogaleri-handler').live('load', function() {
		$(document).keydown(function(e){
			var code = (e.keyCode ? e.keyCode : e.which);
			switch(code){
				// left
				case 37: nextPhoto("prev");
				break;

				// left
				case 39: nextPhoto("next");
				break;

				// up
				case 38: nextPhoto("up");
				break;

				// down
				case 40: nextPhoto("down");
				break;
			}
		});
	});

	if($('#membership-steps').length > 0){
		buildMembershipSteps();

		$("#membership-steps li a").click(function() {

			$("#membership-steps li").each(function() {
				$(this).removeClass("active");
			});

			$(this).parent().addClass("active")

			buildMembershipSteps($(this).text()-1);
		});
	}

	/*
	if($('#svh')){
		$('#svh-btns a').each(function(el, i) {

			$(this).hover(function() {

				$('#svh-btns a').each(function(el, i) {
					$(this).removeClass('active');
				});

				$(this).addClass('active');

				$("#svh-content").animate({
					marginLeft: -(el * 284) + "px",
					opacity: 1
				});

			});
		});

		$('#svh').mouseover(function() {
			$('#svh-content-canvas').css({'display': 'block'});
		});

		$('#svh').mouseout(function() {
			$('#svh-content-canvas').css({'display': 'none'});
		});
	}
	*/
	
	if($('#svh')){
		
		$('#svh .btn-next').click(function() {
			$('#svh-btns').animate({
				marginLeft: "-180px"
			}, 250);
			$(this).removeClass("active");
			$("#svh .btn-prev").addClass("active");
		});

		$('#svh .btn-prev').click(function() {
			$('#svh-btns').animate({
				marginLeft: "0"
			}, 250);
			$(this).removeClass("active");
			$("#svh .btn-next").addClass("active");
		});
	
		
		$('#svh-btns a').each(function(el, i) {

			$(this).hover(function() {

				$('#svh-btns a').each(function(el, i) {
					$(this).removeClass('active');
				});

				$(this).addClass('active');
				
				$('#svh-content .content-item').each(function() {
					$(this).css({
						'opacity': ".2"
					});

					$(this).removeClass('block active');
					$(this).addClass('hidden');
				});

				var obj = $('#svh-content .content-item')[el];
				obj.className += " block";

				$(obj).animate({
					height: "100%",
					opacity: "1"
				})

			});
		});

		$('#svh').mouseover(function() {
			$('#svh-content-canvas').css({'display': 'block'});
		});

		
		$('#svh').mouseout(function() {
			$('#svh-content-canvas').css({'display': 'none'});
		});
	
		
	}
	
	
	$('#tepe-eposta').mouseover(function() {
		$('#te-superonline').show();
		$('#te-superposta').show();
	});

	
	$('#tepe-eposta').mouseout(function() {
		$('#te-superonline').hide();
		$('#te-superposta').hide();
	});
	
	
	externalLinks();

});





function shareThisPage(destSite){

	var title = "";	
	try {
		//title = $("title").html();
		title = $('h1.article-header').text();
	} catch( err ){
		title = "superonline";
	}		
	
	thisUrl = location.href;
	thisHost = document.location.host;
	
	/*
	thisExcept = "http://"+thisHost+"/muzik/";
	if  ( (document.location.hash.length>1) && (thisUrl.substr(0,thisExcept.length) == thisExcept ) ) {
		thisUrl="http://"+thisHost+"/muzik/"+document.location.hash.substr(1).replace("/muzik/","");
	}
	*/
	
	var targetURL = "";
	
	switch (destSite){
	case "facebook":
		targetURL = "http://www.facebook.com/sharer.php?u=" + thisUrl;
	break;

	case "twitter":
		targetURL = "http://twitter.com/home.php?status="+ title + " "+ thisUrl;
	break;

	case "friendfeed":
		targetURL = "http://friendfeed.com/share?url=" + thisUrl + "&title=" + title;
	break;

	case "stumbleupon":
		targetURL = "http://www.stumbleupon.com/refer.php?url=" + thisUrl + "&title=" + title;
	break;

	case "blogger":
		targetURL = "http://blogger.com";
	break;

	case "wordpress":
		targetURL = "http://wordpress.com";
	break;

	case "digg":
		targetURL = "http://digg.com/submit?url=" + thisUrl + "&title=" + title;
	break;

	case "delicious":
		targetURL = "http://del.icio.us/post?url=" + thisUrl + "&title="+ title;
	break;

	case "google":
		targetURL = "http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk=" + thisUrl  + "&title=" + title;
	break;

	case "yahoo":
		targetURL = "http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=" + thisUrl + "&title="+ title;
	break;

	case "myspace":
		targetURL = "http://www.myspace.com/Modules/PostTo/Pages/?c="+ thisUrl + "&t=" + title;
	break;

	case "live":
		targetURL = "https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=" + thisUrl + "&title="+ title;
	break;

	case "oyyla":
		targetURL = "http://www.oyyla.com/gonder?url=" + thisUrl;
	break;

	case "tusul":
		targetURL = "http://www.tusul.com/submit.php?url=" + thisUrl;
	break;

	case "technorati":
		targetURL = "http://www.technorati.com/faves?add=" + thisUrl;
	break;

	case "reddit":
		targetURL = "http://reddit.com/submit?url=" + thisUrl + "&title=" + title;
	break;

	}
	if(targetURL != ""){
		window.open(targetURL,"_blank");
	}	
}




