function activatePlaceholders() {
	if (supports_input_placeholder()) {
		return false;
	}

	$('form').submit(function() {
		$(':text').each(function() {
			var self = $(this)
			if(self.attr('placeholder') == self.val()) self.val('')
		})
	})

	var inputs = document.getElementsByTagName('input');
	for (var i = 0; i < inputs.length; i++) {
		if (inputs[i].getAttribute('type') == 'text') {
			if (inputs[i].getAttribute('placeholder') && inputs[i].getAttribute('placeholder').length > 0) {
				if(inputs[i].value == ''){
					inputs[i].value = inputs[i].getAttribute('placeholder');
				}
				inputs[i].onfocus = function() {
					if (this.value == this.getAttribute('placeholder')) {
						this.style.color = '#000'
						this.value = '';
					}
					return false;
				}
				inputs[i].onblur = function() {
					if (this.value.length < 1) {
						this.style.color = '#999'
						this.value = this.getAttribute('placeholder');
					}
				}
			} else {
				inputs[i].onfocus = function() {
					this.style.color = '#000'
				}
				inputs[i].onblur = function() {
					this.style.color = '#999'
				}
			}
		}
	}
}

function confirmation() {
	if (!confirm('Are you sure?')){
		return false;
	}
}

function supports_input_placeholder() {
  var i = document.createElement('input');
  return 'placeholder' in i;
}

window.onload = function() {
	activatePlaceholders()
}

function toogleVisibility(name){
	var target = document.getElementById(name);
	target.style.visibility = (target.style.visibility == 'visible') ? 'hidden' : 'visible' ;
}

/******************************************************************************\
|******************************************************************************|
\******************************************************************************/

function init_voting(tagId, photoId) {
	$('#' + tagId).attr('photoid', photoId)
	$('#' + tagId + ' div').mouseover(voting_hover).mouseout(voting_hout).click(voting_click).each(function(index) {$(this).attr('xindex', index)})
}

function init_like_voting(tagId, photoId) {
	$('#' + tagId).attr('photoid', photoId).click(like_voting_click)
}

function prevent_voting(parent) {
	parent.children('div').unbind('mouseover', voting_hover).unbind('mouseout', voting_hout).unbind('click', voting_click)
	voting_highlight(parent.children('div'), false)
}

function prevent_like_voting(parent) {
	parent.unbind('click', like_voting_click)
	parent.addClass('active')
	parent.find('div.i-star').addClass('active')
	parent.siblings('.x-like-counter').children('div').html(function(index, old) {
		return(parseInt(old, 10) + 1)
	})
}

function like_voting_click() {
	prevent_like_voting($(this))
	$.ajax({
		url: VOTE_URL + $(this).attr('photoid') + '/4',
	})
}

function voting_click() {
	var parent = $(this).parent()
	prevent_voting(parent)
	$.ajax({
		url: VOTE_URL + $(this).parent().attr('photoid') + '/' + $(this).attr('xindex'),
		success: function(data) {
			var rating = parseInt(data, 10)
			parent.children('div').each(function(index) {
				voting_highlight($(this), index < rating)
			})
		}
	})
}

function voting_highlight(items, isActive) {
	items.each(function() {
		if(!$(this).attr('wasActive'))
			$(this).attr('wasActive', $(this).hasClass('active'))
		if(isActive)
			$(this).addClass('active')
		else
			$(this).removeClass('active')
	})
}

function voting_restore(items) {
	items.each(function() {
		if($(this).attr('wasActive') == 'true')
			$(this).addClass('active')
		else
			$(this).removeClass('active')
	})
}

function voting_hover() {
	voting_highlight($(this).prevAll('div'), true)
	voting_highlight($(this), true)
	voting_highlight($(this).nextAll('div'), false)
}

function voting_hout() {
	voting_restore($(this).siblings('div'))
	voting_restore($(this))
}

function change_comment_input(src) {
	src.style.display = 'none'
	var list = src.parentNode.getElementsByTagName('div')
	for(var i = 0; i < list.length; i++) {
		if(list[i].className == 'b-xinput') {
			list[i].style.display = 'block'
			list[i].getElementsByTagName('textarea')[0].focus()
			break
		}
	}
}

function gallery_toggle_edit() {
	$('#image_desc').toggle();
	$('#edit_image_desc').toggle();
	return false;
}


function changeTab(tabsId, tabKey) {
	var tabs = document.getElementById(tabsId)
	if(tabs) {
		processTabs(tabs, tabKey)
		processContents(tabs, tabKey)
	}
}

function processTabs(tabs, tabKey) {
	var list = tabs.getElementsByTagName('li')
	for(var i = 0; i < list.length; i++) {
		var item = list[i]
		var key = item.getAttribute('key')
		if(key) {
			if(key != tabKey)
				item.className = ''
			else
				item.className = 'active'
		}
	}
}

function processContents(tabs, tabKey) {
	var list = tabs.getElementsByTagName('div')
	for(var i = 0; i < list.length; i++) {
		var item = list[i]
		var key = item.getAttribute('key')
		if(key) {
			if(key != tabKey)
				item.style.display = 'none'
			else
				item.style.display = 'block'
		}
	}
}
