var PPinit = false, showFullSrcPopup = false;

$(function() {
	
	$("#tl_1").show();
	$("a.top-news-select-tab-link").click(function(e) {
		e.preventDefault();
		var i = this.getAttribute("id").charAt(this.getAttribute("id").length - 1);
		$("a.top-news-select-tab-link").parent().removeClass("active");
		$(this).parent().addClass("active");
		$(".top-news-select-box").hide();
		$("#tl_" + i).show();
	});
	
	$("#nl_1").show();
	$("a.news-select-tab-link").click(function(e) {
		e.preventDefault();
		var i = this.getAttribute("id").charAt(this.getAttribute("id").length - 1);
		$("a.news-select-tab-link").parent().removeClass("active");
		$(this).parent().addClass("active");
		$(".news-select-box").hide();
		$("#nl_" + i).show();
	});
	
	$("#vl_1").show();
	$("a.video-select-tab-link").click(function(e) {
		e.preventDefault();
		var i = this.getAttribute("id").charAt(this.getAttribute("id").length - 1);
		$("a.video-select-tab-link").parent().removeClass("active");
		$(this).parent().addClass("active");
		$(".video-select-box").hide();
		$("#vl_" + i).show();
	});
	
	$("#w_7").show();
	$(".weather-region a.city").click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		var i = this.getAttribute("id").charAt(this.getAttribute("id").length - 1);
		$(".weather-region-box").hide();
		$(".weather-select-region-box a.current-region").html($(this).html());
		$(".weather-region").hide();
		$("#w_" + i).show();
	});
	
	$(".weather-select-region-box a.current-region").click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		$(".weather-region").show();
	});
	
	$("#video-filter select").change(function(e) {
		var url = window.location.pathname, u = '';
		var a = url.split("/");
		for(var i = 0; i < a.length; i++) {
			if(a[i] == "sort")
				u = '/' + a[i] + '/' + a[i + 1];
		}
		if(this.value == 0)
			window.location = '/index/gallery' + u;
		else
			window.location = '/index/gallery' + u + '/album/' + this.value;
	});
	
	$("#video-filter2 select").change(function(e) {
		var url = window.location.pathname, u = '';
		var a = url.split("/");
		for(var i = 0; i < a.length; i++) {
			if(a[i] == "sort")
				u = '/' + a[i] + '/' + a[i + 1];
		}
		if(this.value == 0)
			window.location = '/index/view-media' + u;
		else
			window.location = '/index/view-media' + u + '/album/' + this.value;
	});
	
	$('a.show-full-image').click(function(e) {
		e.preventDefault();
		var src = this.children[0].getAttribute("src");
		var alt = this.children[0].getAttribute("alt");
		showFullSrc(src, alt);
	});
	
	var index = 1;
	$('.relate-gallery-select-box .images-arr-left').click(function(e){
		var l = $('.gallery-images-list td');
		if(index > 1){
			$('.gallery-images-list').animate({
				marginLeft: '+=150'
			}, 200, function() {
				index--;
			});
		}
	});
	$('.relate-gallery-select-box .images-arr-right').click(function(e){
		var l = $('.gallery-images-list td');
		if(index <= (l.length - 3)){
			$('.gallery-images-list').animate({
				marginLeft: '-=150'
			}, 200, function() {
				index++;
			});
		}
	});
	
	var index2 = 1;
	$('.relate-gallery-select-box .video-arr-left').click(function(e){
		var l = $('.gallery-video-list td');
		if(index2 > 1){
			$('.gallery-video-list').animate({
				marginLeft: '+=150'
			}, 200, function() {
				index2--;
			});
		}
	});
	$('.relate-gallery-select-box .video-arr-right').click(function(e){
		var l = $('.gallery-images-list td');
		if(index2 <= (l.length - 3)){
			$('.gallery-video-list').animate({
				marginLeft: '-=150'
			}, 200, function() {
				index2++;
			});
		}
	});
	
	$('.login-drop').click(function(e) {
		e.preventDefault();
		$(this.offsetParent).toggleClass('open');
	});
	
});

function reloadPage()
{
	window.location.reload();
}

/*--------------------------------------------------------------------------
 *
 * Set Language
 */
function setLanguage(name)
{
	if (typeof ActiveXObject != "undefined") {
		SendRequest("GET", "/index/set-language?lang=" + name, "", function(responce) {
			if(responce.status == 200) {
				reloadPage();
			}
			else {
				alert("There was an error processing your request");
			}
		});
	}
	else if (typeof XMLHttpRequest != "undefined") { 
		var xhr = $.ajax({
			type: "GET",
			url: "/index/set-language?lang=" + name,
			dataType: "html"
		})
		.complete(function(responce) {
			if(responce.responseText == "ok") {
				reloadPage();
			}
			else {
				alert("There was an error processing your request");
			}
		});
	}
}

/*--------------------------------------------------------------------------
 *
 * Set click
 */
var clicked = false;

function doClick(sel,type)
{
	if(!clicked)
	{
		var xhr = $.ajax({
			type: "GET",
			url: "/admin/set-click?id=" + sel + '&type=' + type,
			dataType: "html"
		});
	}
}

/*--------------------------------------------------------------------------
 *
 * Move window
 */
function moveWindow(anchor) {
	if(anchor == "comments")
		window.location.hash = "comments";
}

/*--------------------------------------------------------------------------
 *
 * Set Grade
 */
var evaluated = false;

function grade(vale, id, vote, generalVote)
{
	if(!evaluated)
	{
		evaluated = true;
		var xhr = $.ajax({
			type: "GET",
			url: "/admin/set-grade?grade=" + vale + '&id=' + id
		})
		.success(function(responce){
			if(responce == "1")
				resetGrade(vale, id, vote, generalVote);
		});
	}
}

function resetGrade(vale, id, vote, generalVote)
{
	if(generalVote > 0)
		vote ++;
	
	generalVote = parseInt(generalVote) + parseInt(vale);
	currentVote = Math.round(generalVote/vote);
	var vl = 1;
	for(i = 0; i < currentVote; i++)
	{
		document.getElementById("star_" + vl).src = "/public/images/design/star.png";
		vl++;
	}
	for(i=0; i<5-currentVote; i++)
	{
		document.getElementById("star_" + vl).src = "/public/images/design/grey_star.png";
		vl++;
	}
}

/*--------------------------------------------------------------------------
 *
 * Load Content
 */
function loadContent(options) {
	var defaults = {
		type: "GET",
		url: "404.html",
		dataType: "text"
	};
	var options = $.extend(defaults, options);
	
	var xhr = $.ajax({
			type: options.type,
			url: options.url,
			dataType: options.dataType
		})
		.success(function(result) {
			if(result.error != 1) {
				options.callback(result);
			}
			else if(typeof result == "object") {
				alert(error_notobject);
			}
			else {
				alert(error_message);
			}
		})
		.error(function() {
			alert(error_message);
		});
}

/*--------------------------------------------------------------------------
 *
 * Show full image
 */
function showFullSrc(src, alt, w, h) {
	if(!showFullSrcPopup) {
		$("body").append('<div id="popupBox"><div class="close">&times;</div><div class="content"></div></div>');
		$("#popupBox .close").click(function(){
			$("#popupBox").css("visibility", "hidden");
		});
		showFullSrcPopup = true;
	}
	
	$("#popupBox .content").html('').append('<img src="' + src + '" alt="' + alt + '" />');
	if($("#popupBox .content img").outerWidth() > 800) {
		$("#popupBox .content img").css("width", "800px");
	}
	else if($("#popupBox .content img").outerHeight() > window.innerHeight) {
		$("#popupBox .content img").css("height", window.innerHeight - 40 +"px");
	}
	
	if(window.innerHeight)
		$("#popupBox").css("top", window.innerHeight/2 - $("#popupBox").outerHeight()/2 + "px");
	else
		$("#popupBox").css("top", "100px");
	$("#popupBox").css("left", document.body.clientWidth/2 - $("#popupBox").outerWidth()/2 + "px");
	$("#popupBox").css("visibility", "visible");
	
	$(window).keypress(function(event) {
		if(event.keyCode == 27) {
			$("#popupBox").css("visibility", "hidden");
			$(window).unbind('keypress');
		}
	});
}
