/**
 * @author Egloo
 */

/**
 * hashCheck() function join point
 */
$.aop.around({target: window, method: 'hashCheck'}, function(invocation) { 
	if(currentHash != window.location.hash) {
		currentHash = window.location.hash;
		
		var query = $.getQuery();
		if(query) {
			_vars.userId = (query["uid"]) ? query["uid"] : null;
			$("#anotherTags").get(0).href = "/tag-list.html#uid=" + _vars.userId;
			$("#tagCloud #content").empty();
		}
		
		$("input[id=togglePhotoShow]").each(function() {
			this.checked = _vars.showAlbumBody;
		});
		
		return invocation.proceed(); 
	}
});

/**
 * ZWS_CallbackHandler(func, resp) function join point
 */
$.aop.around({target: window, method: 'ZWS_CallbackHandler'}, function(invocation) { 
	if(invocation.arguments[0] && invocation.arguments[0].status) {
		switch(invocation.arguments[0].status) {
			case 403:
			case 401:
				window.location = "/login.html?continueUrl=" + encodeURIComponent(window.location);
				break;
		}
	}

	// If both parameters are not null
	if(invocation.arguments[0] && invocation.arguments[1]) {
		return invocation.proceed();
	}
});

/**
 * setAllTags(data) function join point
 */
$.aop.around({target: window, method: 'setAllTags'}, function(invocation) { 
	$("#tagCloud #content").empty();
	$("#tagCloud").removeClass("indicator");

	if(invocation.arguments[0]) {
		if(invocation.arguments[0].status) {
			switch(invocation.arguments[0].status) {
				case 404:
					$("#tagCloud #content").css("text-align", "center").text("тегов нет");
					break;
			}
		} else {
			if(invocation.arguments[0].tags.length > 0) {
				return invocation.proceed();
			}
		}
	}
});

/**
 * setAllAlbums(data) function join point
 */
$.aop.around({target: window, method: 'setAllAlbums'}, function(invocation) { 
	if(invocation.arguments[0]) {
		if(invocation.arguments[0].status) {
			switch(invocation.arguments[0].status) {
				case 404:
					$("#album-indicator").removeClass("indicator");
					$("table", "#album-indicator").removeClass("hide");
					break;
			}
		} else {
			$("#album-indicator").addClass("hide");
			return invocation.proceed();
		}
	}
});

/**
 * renderSection(sectionIndex) function join point
 */
$.aop.around({target: window, method: 'renderSection'}, function(invocation) { 
	if(_pageData) {
		if(!_pageData.status) {
			return invocation.proceed();
		}
	}
});

/**
 * pageChanged(nextPage) function join point
 */
$.aop.around({target: window, method: 'pageChanged'}, function(invocation) { 
	if(_vars.currentPage && _vars.currentPage !== invocation.arguments[0]) {
		_vars.currentPage = invocation.arguments[0];
		
		return invocation.proceed();
	}
});
