jQuery(function($) { // for conflict with prototype.js
	
jQuery(document).ready(function(){ 
	
	// getPageSize() by quirksmode.com
	function ___getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	};
	
	//getPageScroll() by quirksmode.com
	function ___getPageScroll() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
	};
	
	$('a[href|=#]').click(function() {
		return false;
	});
	
	//MainMenu 
	$('#menu li').hover( function () {
		$(this).addClass('hover'); 
	}, function () {
		$(this).removeClass('hover');
	});
	
	//Login/Register
	/*$('.regform').hide();
	$('#reglink').click(function(){
		$('.regform').slideDown(200);
		$('#loginpanel').slideUp(200);
	});
	
	$('#forgotpw1').hide();
	$('#forgotpw0').click(function(){
		$('#forgotpw1').fadeIn(200);
	});*/
	
	//Featured
	/*$('#featured .slides')
	.before('<ul class="dots">')
	.cycle({
		speed: 1000, 
    	timeout: 10000,
		pause: 1,
		pager:  '.dots'
		});
	$('#featured .slides img').hover( function () {
		$('a.activeSlide').hide().addClass('pause').fadeIn(500);
	}, function () {
		$('a.activeSlide').hide().removeClass('pause').fadeIn(500);
	});*/
	
	
	//Checkout
	$('#shipping li input').change(function(){
		if($(this).attr('checked')) {
			$('#shipping li').removeClass('active');
			$(this).parent().parent().addClass('active');
		}
	});
	
	$('#bank_transfer').hide();
	$('#bank_transfer0').hover( function () {
		$('#bank_transfer').fadeIn(200);
	}, function () {
		$('#bank_transfer').fadeOut(200);
	});
	
	
	
	
	//Favs
	$('#favs1').hide();
	$('#favs h3 span').hide();
	$('#favs0').click(function(){
		$('#favs1').slideToggle(100);
		$('#favs h3 span').toggle();
	});
	$('#favs1 li:nth-child(2n) a').addClass('even');
	
	/*
	$("#favs_next").mousedown(function(){
        scrolling = true;
        startScrolling($("#favs1 ul"), "-=20px");
    }).mouseup(function(){
        scrolling = false;
    });
    $("#favs_prev").mousedown(function(){
        scrolling = true;
        startScrolling($("#favs1 ul"), "+=20px");
    }).mouseup(function(){
        scrolling = false;
    });*/
	
	/*
	var total = $('#favs1 ul li').size();
	var show = 10;
	var first = 0;
	var last = show;
	
	$('#favs1 li:gt('+(show-1)+')').hide();
	if(total<=show) { $('#favs1 div').hide(); }
	$('#favs_prev').addClass('disabled');
	$('#favs1 div a').click(function(){
		
		if($(this).attr('id')=='favs_next') {
			if(last<total) {
				first++; last++;
				$('#favs1 li:eq('+(first-1)+')').hide();
				$('#favs1 li:eq('+(last-1)+')').show();
				$('#favs_prev').removeClass('disabled');
			}
			if(last==total) { $('#favs_next').addClass('disabled'); }
		} else {
			if(last>show) {
				first--; last--;
				$('#favs1 li:eq('+first+')').show();
				$('#favs1 li:eq('+last+')').hide();
				$('#favs_next').removeClass('disabled');
			}
			if(first==0) { $('#favs_prev').addClass('disabled'); }
		}
	});
	*/
	
	
	
	
	//Toolbar
	$("#sample_text").change(function(){
		if($("#sample_text option:selected").val()=="my_text") {
			$("#my_text").fadeIn(300);
			$("#my_text").focus();
			$("#language").hide();
		} else {
			$("#my_text").hide();
			$("#language").fadeIn(300);
			$("#language").focus();
		}
	});
	$("#month").hide();
	$("#browse").change(function(){
		if($("#browse option:selected").val()!=="all") {
			$("#month").fadeIn(300);
			$("#month").val("all");
		} else {
			$("#month").hide();
		}
	});
	
	
	//Tip
	$('.tip').each(function(){
		content = $(this).html();
		$(this).html("<img src=\"/themes/itf/images/tip.png\" width=\"15\" height=\"15\" border=\"0\" alt=\"Tip\" /><span>"+content+"</span>");
	});
	$('.tip, .tipword').children('span').hide();
	$('.tip, .tipword').children('span').prepend("<img src=\"themes/itf/images/tip-arrow.png\" width=\"5\" height=\"9\" border=\"0\" alt=\"Tip\" class=\"tip-arrow\" />");
	$('.tip img').hover( function () {
		tipWidth = $(this).next('span').width();
		parentWidth = $(this).parent().width();
		max = 250;
		if(tipWidth > max) { $(this).next('span').css({width: max + "px", whiteSpace: "normal"}); }
		$(this).next('span').css({left: parentWidth + 10 + "px"});
		$(this).next('span').fadeIn(100);
	}, function () {
		$(this).next('span').fadeOut(200);
	});
	$('.tipword').hover( function () {
		tipWidth = $(this).children('span').width();
		parentWidth = $(this).width();
		max = 250;
		if(tipWidth > max) { $(this).children('span').css({width: max + "px", whiteSpace: "normal"}); }
		$(this).children('span').css({left: parentWidth + 10 + "px"});
		$(this).children('span').fadeIn(100);
	}, function () {
		$(this).children('span').fadeOut(200);
	});
	
	
	//Static
	$('#static .note').prepend("<img src=\"themes/itf/images/static-above.png\" width=\"9\" height=\"9\" border=\"0\" alt=\"Above:\" />&nbsp;&nbsp;");
	
	
	//Plural
	$("#users").change(function () {
		if($("#users option:selected").val()!=="1") { $("#plural").html('s'); } else { $("#plural").html(''); }
	});
	
	
	//GlobalExpander {[<ext><exc>] [<ext><exc>] [<ext><exc>]}
	$(".exc").hide();
	$(".show_first *:first-child .exc").show();
	$(".show_first *:first-child .ext").addClass("active");
	$(".show_all .exc").show();
	$(".show_all .ext").addClass("active");
	$(".ext").click(function(){
		if($(this).hasClass("active")==false && $(this).parent().parent().hasClass('.show_all')==false) {
			$(this).parent().parent().find(".ext").removeClass("active");
			$(this).parent().parent().find(".exc").slideUp(200);
		}
		$(this).toggleClass("active");
		$(this).next(".exc").slideToggle(200);
	});
	$(".view_exc").show();
	
	
	//StaticPageExpander
	$(".expand>li>div").hide();
	$(".expand.show_first>li:first-child>div").show();
	$(".expand.show_first>li:first-child").addClass("active");
	$(".expand>li>h3").click(function()
	{	
		if($(this).parent().hasClass("active")==false) {
			$(this).parent().parent().find("li").removeClass("active");
			$(this).parent().parent().find("li>div").slideUp(200);
		}
		$(this).parent().toggleClass("active");
		$(this).next("div").slideToggle(200);
	});
	
	
	
	//FontCollection
	$('.tooltip').hide();
	$('#collection li').hover( function () {
		$(this).children('.tooltip').show();
	}, function () {
		$(this).children('.tooltip').hide();
	});
	$('#collection li').mousemove(function(e){
		var pos = $(this).offset();
		var pos = pos.left;
		var width = $(this).children('.tooltip').width();
		var x = e.pageX - pos - width/2;
		$('.tooltip').css({left: x + "px"});		
	});
	
	
	//FontThumbs
	/*$('#fonts li').hover( function () {
		$(this).find('img').animate({top: + (-75) + 'px'},200);
	}, function () {
		$(this).find('img').animate({top: + (0) + 'px'},200);
	});*/
	
	// SidebarLanguages
	$('#languages .exc ul').makeacolumnlists({cols:2,colWidth:112,equalHeight:true});
	// SidebarShare
	$('#share_more').hide();
	$('#more').click(function() {
		$(this).text($(this).text()=='Show all services'?'Show less':'Show all services');
		$('#share_more').slideToggle(200);
	});
	$('#languages .exc ul').makeacolumnlists({cols:2,colWidth:112,equalHeight:true});
	
	
	
	//ImageTags
	$('#inuse .image .tag').hide();
	$('#inuse .image').hover( function () {
		$('#inuse .image .tag').fadeIn(200);
	}, function () {
		$('#inuse .image .tag').fadeOut(500);
	});
	
	
	//Floater
	$(".floater").hide();
	$(".glyph").click(function() {
		$(".floater.alts").hide();
		var arrPageScroll = ___getPageScroll(); var parentTop = $('#glyphs').offset().top;
		$(".floater.alts").css({top:arrPageScroll[1] + 100 - parentTop}).fadeIn(100);
	});
	$("#viewall").click(function() {
		$(".floater.all").hide();
		var arrPageScroll = ___getPageScroll(); var parentTop = $('#glyphs').offset().top;
		$(".floater.all").css({top:arrPageScroll[1] + 100 - parentTop}).fadeIn(100);
	});
	
	$(".floater .close").click(function() { $(this).parent("div").parent(".floater").fadeOut(100); });
	
	$(".floater .close").hover( function () {
		floaterTitle = $(this).text();
		if($(this).parent().parent().hasClass('popup')) { var X = "Close"; } else { var X = "Cancel"; }
		$(this).css({width: $(this).width()});
		$(this).html(X);
	}, function () {
		$(this).html(floaterTitle);
	});
	
	$(".floater.autohide").animate({opacity:1},3000).fadeOut(500);
	//$(".floater .go").click(function() { $(this).parent('div').parent('.floater').fadeOut(100); });
	$(".floater.show").show();
	
	//GlyphSearch
	$('#glyphsearch .results').hide();
	$("#glyphsearch input").focus(function() { $(this).siblings('.results').fadeIn(100); });
	$("#glyphsearch input").blur(function() { $(this).siblings('.results').fadeOut(300); });
	
	
	//WaterfallSamples
	$('#font-samples .image div span').hide();
	$('#font-samples .image div').hover( function () {
		$(this).children('span').fadeIn(100);
	}, function () {
		$(this).children('span').fadeOut(100);
	});
	
	
	//BuyingOptions
	$('tr.sample div').hide();
	$('#bo a.item').click( function () {
		$(this).toggleClass('active');
		$(this).parent('td').parent('tr').next('tr.sample').find('div').slideToggle(100);
	});
	
	
	//Products
	$('#products a.button').hide();
	$('#products li').hover( function () {
		$(this).children('span').find('a.button').show();
	}, function () {
		$(this).children('span').find('a.button').hide();
	});
	
	$('.rows li ul').each(function(){$(this).css({height: $(this).height() + "px"}); });
	
	
	//Articles
	$('#comments').hide();
	$('#comments0').click( function () {
		$('#comments').slideDown(200);
		var comments = $('#comments').offset().top;
        $('html,body').animate({scrollTop: comments}, 1000);
		$(this).fadeOut(200);
	});
	
	
	//Blog
	setTimeout(function() {
		$('#blog').masonry({ singleMode: true, itemSelector: 'li'});
		}, 1000);


	//Authors
	/*$('#authors').masonry({ singleMode: true, itemSelector: 'li.index'});*/
	$('#authors ul').makeacolumnlists({cols:5,colWidth:198,equalHeight:false,startN:1});
	
	
	//StudioWork
	$('#sw .project').hover( function () {
		$(this).find('img').animate({opacity:0.5},100);
		//$(this).children('.tooltip').show();
	}, function () {
		$(this).find('img').animate({opacity:1},100);
		//$(this).children('.tooltip').hide();
	});
	/*$('#sw .project').mousemove(function(e){
		var pos = $(this).offset();
		var posx = pos.left;
		var posy = pos.top;
		var width = $(this).children('.tooltip').width();
		var x = e.pageX - width/2 - posx - 10;
		var y = e.pageY - posy - 50;
		$('.tooltip').css({left: x + "px", top: y + "px"});		
	});*/
	
	
	// Apps
	$('#history .sliders').hide();
	$('#history input').change(function(){
		$(this).parent().toggleClass('on');
		$(this).parent().find('.sliders').hide();
	});
	$('#history .color a').click( function () {
		$(this).parent().next('.sliders').slideToggle(100);
	});
	$('#history .sliders a.button').click( function () {
		$(this).parent().slideUp(100);
	});
	$('#h_showall input').change(function(){
		if($(this).attr('checked')) {
			$('#history li input').attr('checked','checked');
			$('#history li').addClass('on');
		} else {
			$('#history li input').removeAttr('checked');
			$('#history li').removeClass('on');
			$('#history .sliders').hide();
		}
	});
	
	$('#invert').hide();
	$('#canvas').hover( function () {
		$('#invert').fadeIn(200);
	}, function () {
		$('#invert').fadeOut(200);
	});
	
	$('#invert').click( function () {
		$(this).toggleClass('active');
		$('#canvas').toggleClass('invert');
	});
	
		
	//Help
	var url = window.location.pathname.split( '/' );
	var landing_topic = 'buying-fonts';
	if(url[2])landing_topic = url[2];
	$('#help .topic-content').hide();
	$('#help #' + landing_topic + '-content').show();
	$('#help #' + landing_topic).addClass('active');
	
	var landing_topic = 'basics';
	$('#help.wfs .topic-content').hide();
	$('#help.wfs #' + landing_topic + '-content').show();
	$('#help.wfs #' + landing_topic).addClass('active');
	
	$('#help h3').click(function(){
		$(this).parent().parent().find('h3').removeClass('active');
		$(this).addClass('active');
		var target = $(this).attr('id');
		$('.topic-content').hide();
		$('#' + target + '-content').show();
	});
	
	$('#help .topic-content .expand').show();
		
	
	//Account
	$('#sections a.expand i').hide();
	$('#sections a.expand').hover( function () {
		$(this).children('i').slideDown(150);
	}, function () {
		$(this).children('i').slideUp(150);
	});
	
	if(!$("#xaddress").attr('checked')) { $("#xaddress_form").hide(); }
	$("#xaddress").change(function(){ $("#xaddress_form").slideToggle(200); });
	
	var noZIP = new Array("ireland","panama");
	var x = 100;
	
	var country = $("#country option:selected").val();
	if(country!=="united_states_of_america") { $("#us").hide(); };
	if(country!=="canada") { $("#ca").hide(); };
	if($.inArray(country,noZIP)>-1) { $("#zip").hide(); }
	
	var xcountry = $("#xcountry option:selected").val();
	if(xcountry!=="united_states_of_america") { $("#xus").hide(); };
	if(xcountry!=="canada") { $("#xca").hide(); };
	if($.inArray(xcountry,noZIP)>-1) { $("#xzip").hide(); }
	
	$("#country").change(function(){
		var country = $("#country option:selected").val();
		if($.inArray(country,noZIP)>-1) { $("#zip").fadeOut(x); } else { $("#zip").fadeIn(x); }
		if(country=="united_states_of_america") { $("#us").fadeIn(x); } else { $("#us").fadeOut(x); }
		if(country=="canada") { $("#ca").fadeIn(x); } else { $("#ca").fadeOut(x); }
	});
	$("#xcountry").change(function(){
		var country = $("#xcountry option:selected").val();
		if($.inArray(country,noZIP)>-1) { $("#xzip").fadeOut(x); } else { $("#xzip").fadeIn(x); }
		if(country=="united_states_of_america") { $("#xus").fadeIn(x); } else { $("#xus").fadeOut(x); }
		if(country=="canada") { $("#xca").fadeIn(x); } else { $("#xca").fadeOut(x); }
	});
	
	$(".myform label.warning").prepend("<img src=\"system/images/warning.png\" width=\"9\" height=\"9\" border=\"0\" alt=\"Required field!\"/>&nbsp;");
	
	$('#EmailMerge').hide();
	$('#PasswordMerge').hide();
	$('#forgotpw0').hide();
	$('label.MergeNote').hide();
	$('#merge_accounts-0').click(function(){
		//$('#merge_accounts-1').slideToggle(200); 
		$('#PasswordMerge').slideToggle(300);
		$('#EmailMerge').slideToggle(300);
		$('#forgotpw0').slideToggle(300);
		$('label.MergeNote').slideToggle(300);
	});
	
	$(".zebra tr:nth-child(2n)").addClass("even");
	
	
	//My Orders
	$('#track0').click(function() {
		var arrPageScroll = ___getPageScroll();
		$('#track1').css({top:arrPageScroll[1] + 20}).fadeIn(100);
	});

	//WebFonts
	var landing_tab = 'wfapp_edit';
	$('#wfapp .part').hide();
	$('#wfapp .part.' + landing_tab).show();
	$('#wfapp .tabs a[id|=' + landing_tab + ']').addClass('active');
	
	$('#wfapp .tabs a').click(function(){
		$(this).parent().parent().find('a').removeClass('active');
		$(this).addClass('active');
		var target = $(this).attr('id');
		$(this).parent().parent().parent().parent().find('.part').hide();
		$(this).parent().parent().parent().parent().find('.'+target).show();
	});
	
	$('.language_custom').hide();
	$('#language_select select').change(function(){
		$(this).find('option:selected').val()=='custom_set'?
		$('.language_custom').fadeIn(200).focus():
		$('.language_custom').fadeOut(200).text('');
		$('.language_select_button').text('Add');
	});
	$('.language_custom_edit').click(function(){
		$('.language_custom').fadeIn(200).focus();
		$('.language_select_button').text('Save');
	});
	
	$('#font_select select').not('#font_select div:first-child select').attr('disabled','disabled');
	$('#font_select select').change(function(){
		if($(this).children('option:selected').val()!=='') {
			$(this).parent().next('div').find('select').removeAttr('disabled');
			$(this).parent().next('div').find('select').focus();
		} else {
			$(this).parent().nextAll('div').find('select').children('option:first-child').attr('selected','selected');
			$(this).parent().nextAll('div').find('select').attr('disabled','disabled');
		}
	});
	
	$('#bcalc #input').keyup(function() {
		var v = $(this).val();
		$(this).next('span').html(Math.round(v * 0.02) + ' MB');
	});
	
	$('.codes textarea').click(function(){
		$(this).select();
	});
	
	$('.stats_details').hide();
	$('#wfapp a[id|=wfapp_details]').click(function() {
		$(this).text($(this).text()=='Show details'?'Hide details':'Show details');
		$(this).parent().parent().parent().parent().find('.stats_details').slideToggle(200);
	});
	
	$('#wfapp a#wfapp_setbudget').click(function() {
		var arrPageScroll = ___getPageScroll();
		$('#wfapp_budget_floater').css({top:arrPageScroll[1] + 20}).fadeIn(100);
	});
	
	$('#wfapp a#wfapp_emailcode').click(function() {
		var arrPageScroll = ___getPageScroll();
		$('#wfapp_code_floater').css({top:arrPageScroll[1] + 20}).fadeIn(100);
	});
	
	//Corporate Fonts
	$('#corporate1').hide();
	$('#corporate0 .button').click(function(){
		$('#corporate1').slideDown(200);
		$('#corporate0').slideUp(200);
	});
	
	
	//Search
	$("#results>li:nth-child(2n)").addClass("even");
	
	$("#SearchForm_SearchForm_Search").click(function(){
		$(this).attr("value","");
	});
	
});

});// end jquery function

function writeTo(u, d) {
	var l="\x6D\x61\x69";
	l+="\x6C\x74\x6F\x3A"+u;
	window.location=l+"\x40"+d;
	}
