(function($){
	$.fn.tail = function(options){
		var settings = {
			className:'',
			offsetX:15,
			offsetY:5
		};
		if(options){settings = $.extend({},settings,options)};
		
		var tail = $("<div id='prt_tail' class="+settings.className+" />").appendTo('body');
		
		return this.each(function(){
			var prt = $.prt.get(this);
			var prt_j = prt.j();
			var content = prt_j.next('.prt_tail_content');
			
			//如果没有内容则不做
			if(!content.length || !content.html().length ){return;}
			
			prt_j.hover(function(){
				tail.html(content.html()).show();
			},function(){
				tail.hide();
			}).mousemove(function(e){
				tail.css({
					top:(e.pageY + settings.offsetY)+'px',
					left:(e.pageX + settings.offsetX)+'px'		
				});	
			});
		});
	};	
})(jQuery);

$(function(){
	var ie6 = '\v'=='v' && !window.XMLHttpRequest;
	var panel = $('#panel');
	var main = $('#main');
	var scroll_timer;
	var panel_ul = panel.find('ul');
	var active_class = 'active';
	
	$('.gallery').each(function(){
		var gallery = $(this);
		//title area
		var title = $('<h3 class="png" />').appendTo(gallery);
		//navi bar area
		var navi  = $('<ul class="gallery_navi clear" />').insertAfter(gallery);
		var no = 0;
		var timer;
		var link = gallery.find('.view_code');
		//bind event
		gallery.find('img').each(function(i){
			no++;
			var img = $(this);
			var siblings;
			var text = img[0].alt;
			//add the first img's title
			if(i===0){
				title.html(text);
				link[0].href=img[0].title;
			}else{
				img.hide();
			}
			//create navi bar
			$('<li><span title="'+text+'"></span></li>').appendTo(navi).click(function(){
				clearTimeout(timer);
				siblings = img.siblings('img').css({'z-index':0}).stop(true, true);
				title.html(img[0].alt);
				link[0].href=img[0].title;
				img.css('z-index','10').fadeIn(500,function(){
					siblings.hide();
				});
				$(this).addClass(active_class).siblings('li').removeClass(active_class);
			}).addClass(function(){
				return i===0 ? active_class : '';
			});
			
		});
		//centered the navi
		navi.css({marginLeft:(510-no*25-5)*.5+'px'});
		
		if(!gallery.hasClass('auto_play')){return;}
		var delay = 5000;
		timer = setTimeout(function(){
			var next_img = gallery.find('img:visible').next('img');
			if(!next_img.length){
				next_img = gallery.find('img').eq(0);
			}
			
			var siblings = next_img.siblings('img').css({'z-index':0}).stop(true, true);
			title.html(next_img[0].alt);
			link[0].href=next_img[0].title;
			next_img.css('z-index','10').fadeIn(500,function(){
				siblings.hide();
			});
			navi.find('li').eq(next_img.index()).addClass(active_class).siblings('li').removeClass(active_class);
			timer = setTimeout(arguments.callee, delay);
		}, delay);
	});
	
	if(ie6){
		$('body').addClass('ie6');
		PNG.fix('.png');
		$(window).scroll(function(){
			panel.hide();
			clearTimeout(scroll_timer);
			scroll_timer = setTimeout(function(){
				panel.css({top:$(document).scrollTop()+180}).show();
			},300);
		});
	}else{
		panel.css({
			left:main.offset().left+35+'px'
		}).show();
		
		$(window).resize(function(){
			panel.css({
				left:main.offset().left+35+'px'
			});
		});		
	}

	panel.find('a').click(function(){
		if(!this.title) return;
		$.scrollTo('#'+this.title, 300);
	});
	
	//order page
	var form = $('#order_form');
	var price = $('.price_holder');
	var order_options = [
		'non_web_font',
		'custome_form',
		'layout_width',
		'menu',
		'carousel',
		'accordion',
		'tab',
		'slideshow',
		'lightbox'
	];
	
	function updatePrice(){
		// package price
		var pages = $('#pages').val(),
			days  = $('#days').val(),
			doc   = $('[name="package"]:checked').val(),
			first = Number($('#package_'+doc+'_price').text().match(/\d+/)[0]),
			other = Math.ceil(first/2),
			package_cost = 1*first + (pages-1)*other +(5-days)*(pages*10+10),
			additional_cost = 0;
		
		// option price
		for (var i=0, l=order_options.length; i<l; i++) {
			additional_cost = additional_cost + ($('#options_'+order_options[i]).is(':checked') ? Number($('#option_'+order_options[i]+'_price').text().match(/\d+/)[0]) : 0);
		}
		
		// update display
		$('#package_cost').text('$'+package_cost);
		$('#additional_cost').text('$'+additional_cost);
		price.text('$'+ (package_cost + additional_cost));
	}
	
	if(form.length){
		var submit_error = $('#submit_error');
		$.prt.init({
			basePath:LIBPATH,
			skin:'green'	
		}).render({
			context:form,
			text:[{selector:'#name, #email, #tel', width:190},{selector:'#link', width:400}],
			radio:[{
				selector:'input[type="radio"]',
				click:updatePrice
			}],
			checkbox:[{
				selector:'input[name="options[]"]',
				click:updatePrice,
				width:400
			}],
			select:[{
				selector:'#pages, #days',
				width:190,
				change:updatePrice
			}],
			file:[{selector:'#upload', width:400}],
			textarea:[{selector:'#content', width:400}],
			complete:function(){
				$('#tel').tail();
			}
		});
		
		$('#order_btn').click(function(){
			if(form.dooCheck({
				rulesById:{
					name:[{rule:'required'}],
					email:[{rule:'required'},{rule:'email'}],
					tel:[{rule:'required'}]
				},
				extend:{
					file:function(){
						return ($.trim($('#upload').val())!='' || $.trim($('#link').val())!='') ? true : 'This field is required.';
					}
				}
			})){
				submit_error.hide();
				form.submit();
			}else{
				submit_error.show();
			}
		});
		
		$('.closed').click(function(){
			$(this).toggleClass('opened').next().toggle();
		});
		
		updatePrice();
	}
	
	//contact page
	var cform = $('#contact_form');
	if(cform.length){
		$.prt.init({
			basePath:LIBPATH,
			skin:'green'	
		}).render({
			context:cform,
			text:[{selector:'#name, #email, #tel', width:190}],
			textarea:[{selector:'#content', width:400}],
			complete:function(){
				$('#tel').tail();
			}
		});
		
		$('#submit_btn').click(function(){
			if(cform.dooCheck({
				rulesById:{
					name:[{rule:'required'}],
					email:[{rule:'required'},{rule:'email'}],
					tel:[{rule:'required'}],
					content:[{rule:'required'}]
				}
			})){
				cform.submit();
			}
		});
	}
	
	$("a[rel='external']").click(function(){
		window.open(this.href);
		return false;
	});
	
	//work page
	$('.trial_link').click(function(e){
		
		try{
			_gaq.push(['_trackEvent', 'Trials', 'ViewTrial', 'View Trial Offer Section']);
		}catch(e){}
		
		$.scrollTo('#trial', 300);

		return false;
		
	});
	
	var trial = $('#trial');
	if(trial.length){
		var trial_top = trial.offset().top;
		var doc = $(document);
		var view_line = trial_top - 250;
		var done = false;
		doc.bind('scroll', function(){
			if(!done && doc.scrollTop() > view_line){
				done = true;
				try{
					_gaq.push(['_trackEvent', 'Trials', 'ViewTrial', 'View Trial Offer Section']);
				}catch(e){}
			}
		});		
	}
	
	$('.addthis_button').bind('click', function(){
        try{
			_gaq.push(['_trackEvent', 'Trials', 'Bookmark', 'Add Bookmark Us']);
		}catch(e){}
	});

	
	
	
	

});
