var gbd = '#dummy';
var gbdh = '#header li a';
var gbds = '#dummy';
var gmd = 'h2, h3';
var glt = '#intro h1, #prevs div.head h2';
var gbk = '#dummy';

Cufon.replace(gbd, { fontFamily: 'GothamBold' });
Cufon.replace(gbdh, { fontFamily: 'GothamBold', hover: true });
Cufon.replace(gbds, { fontFamily: 'GothamBold', textShadow: '#584e68 1px 1px' });
Cufon.replace(gbk, { fontFamily: 'GothamBook' });
Cufon.replace(gmd, { fontFamily: 'GothamMedium' });
Cufon.replace(glt, { fontFamily: 'GothamLight' });

$(function(){
	$(window).load(function(){
		$('body').addClass('js');
	});
	$('#prslid').prslid();
	$('textarea').disRes();
	$('#preview').insprev();
//	$('#cform').formfocus();
	$('#scroller').scroller();
	$('#browsers li').showopt();
	$('#cform div.file input').styleInput();
	$('#projects').previews();
});

(function($) {

//	PROJECT PREVIEWS

	$.fn.previews = function(){
		if (this.length == 0){
			return $(this);
		}
		var loc = {};
		$('body').append('<div id="overlay"></div><div id="prevs"></div>')
		var $prevs = $('#prevs').css('left', '-50%');
		var $projs = $(this).children('div.project');

		var html = '<div class="head"><ul class="tabs"><li><a class="page" href="#" title="View Page"><span>View Page</span></a></li><li><a href="#" class="html" title="View HTML"><span>View HTML</span></a></li><li><a class="css" href="#" title="View CSS"><span>View CSS</span></a></li></ul><h2></h2></div>';
		if ($.browser.msie){
			for (i=0; i<$projs.length; i++){
				var prid = $projs.eq(i).attr('id');
				html += '<div class="' + prid + ' css obj"><iframe></iframe></div><div class="' + prid + ' html obj"><iframe></iframe></div><div class="' + prid + ' page obj"><iframe></iframe></div>';
			}
		}
		else {
			for (i=0; i<$projs.length; i++){
				var prid = $projs.eq(i).attr('id');
				html += '<div class="' + prid + ' css obj"><object type="text/html"></object></div><div class="' + prid + ' html obj"><object type="text/html"></object></div><div class="' + prid + ' page obj"><object type="text/html"></object></div>';
			}
		}
		$prevs.append($(html));

		var szfn = function(){
			var win = {
				wd: $(window).width(),
				ht: $(window).height()
			}
			$prevs.css({
				width: win.wd - 200,
				height: win.ht - 200,
				marginTop: -Math.round(win.ht/2) + 100,
				marginLeft: -Math.round(win.wd/2) + 100
			});
			$prevs.children('div.obj').height($prevs.height() - 50);
			if ($.browser.msie){
				$prevs.find('iframe').css({
					width: win.wd - 200 + 4,
					height: win.ht - 200 - 50 + 4
				});
			}
		}
		szfn();
		$(window).resize(szfn);

		var $over = $('#overlay');
		var clfn = function(){
			var $par = $(this).parent('div');
			loc.prid = $par.attr('id');
			var objs = Array('css', 'html', 'page');
			var urls = Array();
			urls[0] = 'http://' + document.domain + '/pageview/css/';
			urls[1] = 'http://' + document.domain + '/pageview/html/';
			urls[2] = 'http://dev.xhtmlmydesign.com/';
			$prevs.find('div.obj').hide();
			if ($.browser.msie){
				var param = 'src';
			}
			else {
				var param = 'data';
			}
			for (i=0; i<urls.length; i++){
				var $o = $prevs.find('div.' + loc.prid + '.' + objs[i]).children();
				if ($o.attr(param) != urls[i] + loc.prid){
					$o.attr(param, urls[i] + loc.prid);
				}
			}
			$prevs.hide().css('left', '50%').find('div.head h2').text($par.children('h3').text());
			setTimeout(function(){
				$prevs.find('div.' + loc.prid).show();
				$prevs.add($over).fadeIn(800);
				Cufon.refresh();
			}, 200);
			$('#page').css({
				overflow: 'hidden',
				height: $(window).height()
			});
		}
		$(this).find('span.overlay').click(clfn);

		var ovclfn = function(){
			$(this).add($prevs).fadeOut(800, function(){
				$prevs.find('div.obj').hide();
			});
			$('#page').css({
				height: 'auto',
				overflow: 'visible'
			});
		}
		$over.click(ovclfn);

		var navclfn = function(){
			var $this = $(this);
			$('div.' + loc.prid).not($('div.' + loc.prid + '.' + $this.attr('class'))).fadeOut(800);
			$('div.' + loc.prid + '.' + $this.attr('class')).fadeIn(800);
			return false;
		}
		$prevs.find('div.head a').click(navclfn);

		return $(this);
	}

//	SCROLLER

	$.fn.scroller = function(){
		if (this.length == 0){
			return $(this);
		}
		var $obj = $(this);
		Cufon.replace('#scroller > a, #portgal > a', { fontFamily: 'Gotham_Black', hover: true });

		var count = 0;
		var $img = $obj.find('img').hide().eq(0).show().end();
		var sshow = function(){
			$img.eq(count).fadeOut(800);
			count++;
			if (count == $img.length){
				count = 0;
			}
			$img.eq(count).fadeIn(800);
		}

		var int = setInterval(sshow, 5000);

		return $(this);
	}

//	DISABLE TEXTAREA RESIZING

	$.fn.disRes = function(){
		if (this.length == 0){
			return $(this);
		}
		if ($.browser.webkit){
			$(this).css('resize', 'none');
		}
		return $(this);
	}

//	BROWSER OPTIONS

	$.fn.showopt = function(){
		if (this.length == 0){
			return $(this);
		}
		var show = function(){
			$(this).stop().animate({ width: 221 }, 450).find('div.versions').stop().animate({ width: 221 }, 450);
		}
		var hide = function(){
			$(this).stop().animate({ width: 57 }, 450).find('div.versions').stop().animate({ width: 0 }, 450);
		}
		$(this).hover(show, hide);
		return $(this);
	}

//	STYLE FILE INPUT

	$.fn.styleInput = function(){
		if (this.length == 0){
			return $(this);
		}
		var clfn = function(){
			$(this).removeAttr('value');
			$(this).parents('#cform').find('p.value').text('');
		}
		var chfn = function(){
			var val = $(this).attr('value').replace(/^.*[\/\\]/g, '');
			$(this).parents('#cform').find('p.value').text(val);
		}
		$(this).click(clfn).change(chfn);
		return $(this);
	}

//	FORM FOCUS

	$.fn.formfocus = function(){
		if (this.length == 0){
			return $(this);
		}
		var $inp = $(this).find('input.txt').add($('textarea'));
		var bodycl = function(){
			$inp.parent().formfade(1);
		}
		var focfn = function(){
			var $par = $(this).parent().formfade(1);
			$inp.parent().not($par).formfade(0.2);
		}
		var blrfn = function(){
			$inp.parent().formfade(0.2);
		}
		var clfn = function(e){
			e.stopPropagation();
		}
		$('body').click(bodycl);
		$inp.click(clfn).focus(focfn);
		$('#cform button').focus(bodycl);
		$('#cform input.file').focus(blrfn);
		return $(this);
	}

	$.fn.formfade = function(op){
		if (this.length == 0){
			return $(this);
		}
		if ($.browser.msie){
			var op = op * 100;
			if (op == 100){
				$(this).removeClass('off');
			}
			else {
				$(this).addClass('off');
			}
		}
		else {
			$(this).fadeTo(400, op);
		}
		return $(this);
	}

//	INSERT PREVIEWS

	$.fn.insprev = function(){
		if (this.length == 0){
			return $(this);
		}
		var dat = {
			title: $('#portgal h3').text(),
			name: $('#portgal a.overlay').attr('href')
		};
		$.ajax({
			data: dat,
			cache: true,
			type: 'POST',
			dataType: 'html',
			url: '/preview',
			success: function(data) {
				if (data.length == 0){
					return;
				}
				$('#preview').append($(data));
			},
			complete: function(){
				$('#preview').pageview();
			}
		});
		return $(this);
	}

//	PAGE PREVIEW

	$.fn.pageview = function(){
		if (this.length == 0){
			return $(this);
		}

		var $obj = $(this);
		var objwd = $obj.width();
		var objht = $obj.height();
		var setPosition = function(){
			var winwd = $(window).width();
			var winht = $(window).height();
			var mtop = Math.round(winht/2 - objht/2);
			var mleft = Math.round(winwd/2 - objwd/2);
			$obj.css({
				marginTop: mtop,
				marginLeft: mleft
			});
		}
		setPosition();
		$(window).resize(setPosition);

		var $tabs = $obj.children('ul.tabs').children();
		var $views = $obj.find('div.view').hide().eq(0).show().end();
		var tabclfn = function(){
			if ($(this).hasClass('act')){
				return false;
			}
			var num = $tabs.index(this);
			$(this).addClass('act').siblings().removeClass('act');
			var $cv = $views.eq(num).fadeIn(800);
			$views.not($cv).fadeOut(800);
		}
		$tabs.click(tabclfn);

		if ( $.browser.msie){
			var $v = $views.eq(0);
			var src = $v.children('object').remove().attr('data');
			var $frame = $('<iframe frameborder="0" src="' + src + '"></iframe>');
			$v.append($frame);
			$views.children('object').addClass('ie');
		}

		$('body').append('<div id="overlay"></div>');
		var $over = $('#overlay');

		var lockon_old = function(){
			var sc = $('html').attr('scrollTop');
			var mousefn = function(){ return false; }
			var scrollfn = function(){
				$('html').attr('scrollTop', sc);
			}
			$(window).unbind('resize').mousewheel(mousefn).scroll(scrollfn);
			var wd = $(window).width();
			var ht = $(window).height();
			$('html').css({
				height: ht,
				overflowY: 'hidden'
			});
			$('html').css('marginLeft', -($(window).width() - wd));
		}
		var lockoff_old = function(){
			$(window).unbind().resize(setPosition);
			$('html').removeAttr('style').css('overflowY', 'scroll');
		}
		var lockState = false;
		var lockon = function(){
			lockState = true;
		}
		var lockoff = function(){
			lockState = false;
		}
		$obj.hover(lockon, lockoff);
		$obj.mousewheel(function(event, delta){
			if (lockState){
				event.stopPropagation();
				return false;
			}
		});

		var showfn = function(){
			$obj.add($over).fadeIn(800);
			return false;
		}
		var hidefn = function(){
			$obj.add($over).fadeOut(800);
		}
		var hvonfn = function(){
			$(this).children().removeAttr('style').stop().hide().fadeIn(400);
		}
		var hvoffn = function(){
			$(this).children().stop().fadeOut(400);
		}
		$('#portgal a.overlay').click(showfn).hover(hvonfn, hvoffn).children().addClass('on').hide();
		$over.click(hidefn);

		var escfn = function(e){
			if (e.which == 27){
				hidefn();
			}
		}
		$('html').keydown(escfn);

		return $obj;
	}

//	PRICING SLIDER

	$.fn.prslid = function(){
		if (this.length == 0){
			return $(this);
		}
		var loc = {};
		var $handle = $('#handle');
		var $holder = $handle.parent();

		var setup = function(){
			loc.x1 = $holder.attr('offsetLeft');
			loc.y1 = $holder.attr('offsetTop') + $handle.attr('offsetTop');
			loc.x2 = loc.x1 + 360;
			$handle.draggable({
				axis: 'x',
				drag : function(e, ui){
					slfn(ui, 'drag');
				},
				stop : function(e, ui){
					slfn(ui, 'stop');
				},
				containment: [loc.x1, loc.y1, loc.x2, loc.y1]
			});
			var num = $('#cform span.num').text();
			if (num.length != 0){
				$handle.css('left', parseInt(num) * 30);
			}
		}

		var updpr = function(div){
			if (div > 1){
				var price = 150 + (div - 1) * 75;
			}
			else {
				var price = div * 150;
			}
			if (div != 0){
				$('#cform span.num').text(div);
				$('#pages').attr('value', div);
				$('#price').add($('#price_dummy')).attr('value', price);
			}
			else {
				$('#cform span.num').text('');
				$('#pages').attr('value', '');
				$('#price').add($('#price_dummy')).attr('value', '');
			}
		}

		var slfn = function(ui, mode){
			var div = Math.round((ui.position.left - 0.5)/30);
			updpr(div);
			if (mode == 'stop'){
				$handle.animate({
					left: div * 30
				}, 400);
			}
		}

		var clfn = function(e){
			var div = Math.round((e.pageX - loc.x1 - 18)/30);
			if (div > 12){
				div = 12;
			}
			else if (div < 0){
				div = 0;
			}
			$handle.animate({
				left: div * 30
			}, 400);
			updpr(div);
		}

		var msg = function(){
			alert('Please use the slider to indicate the number of pages. Price will be calculated automatically.');
		}

		setup();
		$(window).resize(setup);

		$('#cform span.cov').click(msg);

		$holder.click(clfn);

		return $(this);
	}

//	LOGGING FUNCTIONS FOR TROUBLESHOOTING

	$.fn.log = function(){
		if (typeof window.console !== 'undefined'){
			console.log($(this));
		}
		return $(this);
	}

	$.log = function(inp){
		if (typeof window.console !== 'undefined'){
			console.log(inp);
		}
		return inp;
	}

})(jQuery);

