$(document).ready(function() {
	// Decorate sIFR
	if (typeof sIFR == "function") {
		if (typeof dontUseSIFR != "boolean" && !jQuery.browser.opera) {
			sIFR.replaceElement(named({sSelector:".bb-white h1.styled", sFlashSrc: src="/flash/Delicious.swf", sColor: "#000000", sBgColor: "#0a0a0a", sWmode: "transparent", sWidth: "400px", sHeight: "50px"}));
			sIFR.replaceElement(named({sSelector:"h1.styled", sFlashSrc: src="/flash/Delicious.swf", sColor: "#ffffff", sBgColor: "#0a0a0a", sWmode: "transparent", sWidth: "400px", sHeight: "30px"}));
			sIFR.replaceElement(named({sSelector:".bb-white h1.styled-bold", sFlashSrc: src="/flash/DeliciousBold.swf", sColor: "#000000", sBgColor: "#0a0a0a", sWmode: "transparent", sWidth: "400px", sHeight: "50px"}));
			sIFR.replaceElement(named({sSelector:"h1.styled-bold", sFlashSrc: src="/flash/DeliciousBold.swf", sColor: "#ffffff", sBgColor: "#0a0a0a", sWmode: "transparent", sWidth: "400px", sHeight: "30px"}));
			sIFR.replaceElement(named({sSelector:".project h1", sFlashSrc: src="/flash/Delicious.swf", sColor: "#ffffff", sBgColor: "#2A2A2A", sWmode: "transparent", sWidth: "245px", sHeight: "20px"}));
			sIFR.replaceElement(named({sSelector:"form.default h3", sFlashSrc: src="/flash/Delicious.swf", sColor: "#ffffff", sBgColor: "#2A2A2A", sWmode: "transparent"}));
		}
	}
	
	// Show JS only elements 
	$('.js-only').show();
	
	// init rollover images
	$('img.rollover').each( function( key, elm ) {
		$('<img>').attr( 'src', $(this).attr('hover') );
	});
	
	$('img.rollover').hover(function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('original', currentImg);
	}, function() {
		$(this).attr('src', $(this).attr('original'));
	});
	
	// track in GoogleAnalytics
	try {
		pageTracker = _gat._getTracker("UA-2535771-1");
		pageTracker._trackPageview();
	} catch(err) {}
	
	// activate screenshot buttons
	$('.button-screenshot a').click(function(event) {
		$(this).parent().parent().find('a.fancybox').trigger("click");
		event.preventDefault();
	});
	
	// Style textarea
	$('.textarea-wrapper textarea').focus(function() {
		$(this).parent().addClass('textarea-wrapper-focused');
	}).blur(function() {
		$(this).parent().removeClass('textarea-wrapper-focused');
	});
	
	// Style checkboxes
	if ($.fn.ezMark != undefined) {
		$('input:checkbox').ezMark();
	}
	
	
	$('#ie6close').click(function(e) {
		$('.noie6-box').hide();
		$('.noie6-screen').hide();
		e.preventDefault();
	});
	
	var userAgent = $.browser.version;
	userAgent = userAgent.substring(0,userAgent.indexOf('.'));
	version = userAgent;
	
	if (!$.browser.msie || version > 7) {
		$('.button-small-styled').hover(function() {
			$(this).addClass('button-small-styled-hover');
		}, function() {
			$(this).removeClass('button-small-styled-hover');
		}).focus(function() {
			$(this).addClass('button-small-styled-active');
		}).blur(function() {
			$(this).removeClass('button-small-styled-active');
		});
		
		$('.button-styled').hover(function() {
			$(this).addClass('button-styled-hover');
		}, function() {
			$(this).removeClass('button-styled-hover');
		}).focus(function() {
			$(this).addClass('button-styled-active');
		}).blur(function() {
			$(this).removeClass('button-styled-active');
		});
		
		$('.button-big-styled').hover(function() {
			$(this).addClass('button-big-styled-hover');
		}, function() {
			$(this).removeClass('button-big-styled-hover');
		}).focus(function() {
			$(this).addClass('button-big-styled-active');
		}).blur(function() {
			$(this).removeClass('button-big-styled-active');
		});
	}
	
});



jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


function initFancybox(selector, items) {
	if ($(selector).length > 0) {
		$(selector).fancybox({
			'hideOnContentClick': true,
			'zoomSpeedIn': 300,
			'zoomSpeedOut': 300,
			'overlayOpacity': 0.85
		});
	}
}


function initFilter() {
	var filterPanel = $('#filterPanel');
	var projects = $('.project');
	var tags = filterPanel.find('ul.tech-icons li');
	var tagSelection = false;
	var typeSelection = false;
	
	// init the open button
	$('#openFilterButton').click(function(event) {
		if (filterPanel.is(':visible')) {
			// hide the panel
			hidePanel();
		} else {
			// show the panel
			$(this).addClass('active');
			filterPanel.slideDown(function() {
				$(this).css('overflow', 'visible');
			});
		}
	});
	
	// init the close button
	filterPanel.find('.close-button a').click(function(event) {
		event.preventDefault();
		hidePanel();
	});
	
	// init the custom selectbox
	var typeSelectbox = $('#projectType').selectbox({
		inputClass: "selectbox-skined",
		containerClass: "selectbox-droplist",
		hoverClass: "hover",
		selectedClass: "active"
	});
	
	
	$('ul.tech-icons li img').click(function() {
		var $this = $(this);		
		if ($this.parent().is('.selected')) {
			tagSelection = false;
			$this.parent().removeClass('selected');
			tags.filter('.faded').removeClass('faded');
		} else {
			tagSelection = this.className;
			// select Tag
			tags.filter('.selected').removeClass('selected');
			$this.parent().addClass('selected').removeClass('faded');
			tags.filter(':not(.selected)').addClass('faded');
		}
		filterBySelection();
	});
	
	
	$('#projectType').change(function() {
		if (this.value != "all") {
			typeSelection = this.value;
		} else {
			typeSelection = false;
		}
		filterBySelection();
	});
	
	
	function filterBySelection() {
		if (tagSelection || typeSelection) {
			var projectsToShow = projects;
			if (tagSelection) {
				projectsToShow = projectsToShow.filter(function() {
				  return $(this).find('ul.icons li.' + tagSelection).length > 0;
				});
			}

			if (typeSelection) {
				projectsToShow = projectsToShow.filter('.' + typeSelection);
			}
			projects.hide();
			$($.unique(projectsToShow)).show();
		} else {
			projects.show();
		}
	}
	
	
	function hidePanel() {
		// reset the selection (part 1)
		tagSelection = typeSelection = false;
		filterBySelection();
		filterPanel.css('overflow', 'hidden').slideUp(function() {
			$('#openFilterButton').removeClass('active');
			// reset the selection (part 2)
			tags.removeClass('faded').filter('.selected').removeClass('selected');
			$('#projectType_input').val(typeSelectbox.get(0).options[0].text);
			$('#projectType_container ul li.active').removeClass('active');
			$('#projectType_container ul li:first').addClass('active');
		});
	}
}


function initCustomFileInput(fileInput, fakeTextInput, fakeButton) {
	fileInput.hover(function() {
		fakeButton.addClass('hover');	
	}, function() {
		fakeButton.removeClass('hover');
	}).focus(function() {
		fakeButton.addClass('active');
	}).blur(function() {
		fakeButton.removeClass('active');
	}).change(function(e) {
		fakeTextInput.val(fileInput.val());
	});
}


function initForm(options) {
	
	function onResponce(responseText, statusText, xhr, wrapped) {
		if (typeof(options.onResponce) === "function") {
			options.onResponce.call();
		}
		
		var response = eval(responseText);
		
		if (response.result === true) {
			if (typeof(options.onSuccess) === "function") {
				options.onSuccess.call();
			}
		}
		else if (response.result === false) {
			if (response.errorType === "server") {
				var errorContainer = $(options.serverErrorContainerSelector);
				if (errorContainer.length > 0 && response.errorMessage != undefined) {
					errorContainer.html(response.errorMessage).show();
				}
				track('DNContest', 'SubmitionServerError');
			}
			else if (response.errorType === "input") {
				if (response.errors !== undefined && response.errors.length > 0) {
					var field;
					for (var i in response.errors) {
						field = $('#' + response.errors[i].field);
						field.parents('.i-item').addClass('i-item-error').prepend('<div class="error">' + response.errors[i].message + '</div>');
					}
				}
			}
		}
	}
	
	var form = $(options.formSelector);
	
	form.ajaxForm({
		"success": onResponce,
		"type": "POST",
		"dataType": "json",
		"beforeSubmit" : options.beforeSubmit
	});
}


function presentLoadingView(container) {
	container.css('position', 'relative');
	loadingView = $('<div class="loading-view"></div>')
		.css({
			height: container.height(),
			width: container.width(),
			position: "absolute",
			top: "0",
			left: "0"
		});
	container.append(loadingView);
}


function dismissLoadingView() {
	if (loadingView) {
		loadingView.remove();
		loadingView = undefined;
	}
}


function openPopup(id, url, width, height) {
	window.open(url, id, "menubar=0,toolbar=0,location=0,scrollbars=1,resizable=1,status=0,width="+width+",height="+height+"");
}

function track(category, eventName) {
	pageTracker._trackEvent(category, eventName);
}
