$(function() {
	//ajax example
	//$.post('?test', {email:'email',user:'user'}, function(response) {
	//	alert(response);
	//});
	
	//ajax example
	//$.get('?get_featured_rant', function(response) {
		//alert(response);
	//});
	
	//each example
	//$("#frm_manage_users input[@type=checkbox]").each(function(){
		//alert($(this).val());
	//});

	$(".home-comics").mouseover(HomeComicsOver).mouseout(HomeComicsOut);	
	
	$(".toggle-comic-image").click(ToggleComicImage);
	
	//lightbox
	if($('#tourpage_comic_main_picture').length > 0)
		$('#tourpage_comic_main_picture a').lightBox();
	
	if($('.outerSlidedeck').length > 0){
		$('.outerSlidedeck').slidedeck().vertical({
			before: function(deck){
				//remove previous classes
				for(i = 1; i <= 8; i++)
					$(".verticalSlideNav li").removeClass("nav" + i + "-active");
				
				// add the current slide number after animating
				var item_id = parseInt(deck.current) + 1;
				$("li.nav_" + item_id).addClass("nav" + item_id + "-active");				
			},
			complete: function(deck){
				//remove previous classes
				for(i = 1; i <= 8; i++)
					$(".verticalSlideNav li").removeClass("nav" + i + "-active");
				
				// add the current slide number after animating
				var item_id = parseInt(deck.current) + 1;
				$("li.nav_" + item_id).addClass("nav" + item_id + "-active");				
			}
		});
		
		//add active class to first element
		$(".verticalSlideNav li:first").addClass("nav1-active");
	}
	
	//add onclick event to home page slides
	$(".slidesVertical img").click(function(){
		$('.outerSlidedeck').slidedeck().vertical().next();											
	});
		
	
	$("ul.topnav li span").click(function() {
		//Following events are applied to the subnav itself (moving subnav up and down)  
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
  
		$(this).parent().hover(function() {  
		}, function(){  
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up  
		});  
  
		//Following events are applied to the trigger (Hover events for the trigger)  
		}).hover(function() {  
			$(this).addClass("subhover"); //On hover over, add class "subhover"  
		}, function(){  //On Hover Out  
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"  
	});  
	
	$("ul.topnav li .download-box").click(function() {
		//Following events are applied to the subnav itself (moving subnav up and down)  
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
  
		$(this).parent().hover(function() {  
		}, function(){  
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up  
		});  
  
		//Following events are applied to the trigger (Hover events for the trigger)  
		}).hover(function() {  
			$(this).addClass("subhover"); //On hover over, add class "subhover"  
		}, function(){  //On Hover Out  
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"  
	});  
	
	$(".download-item .subnav li").click(SelectChapter);
	
	$("select[name=download]").change(SelectChapter3);
})

function DialogSampleRequest(){
	$("#container").html('');
	$("#container").dialog("destroy");
			
	$("#container").dialog({
		title: "Sample Request",
		resizable: false,
		modal: true,
        autoOpen: false,
        height: "auto",
		position: "top",
		width: 800,
        open: function() {
        	$("#container").load(HTTP + 'sample-request/');
		}
	});	
	
	$("#container").dialog("open");		
}

function DialogWhatsThis(){
	$("#container").html('');
	$("#container").dialog("destroy");
	
	var item_name = 'XXX';
	if( ($("#name").length > 0) && ($("#name").val() != '') )
		item_name = $("#name").val();
	
	$("#container").dialog({
		title: "What's this?",
		resizable: false,
		modal: true,
        autoOpen: false,
        height: 80,
		position: "center",
		width: 300,
        open: function() {
        	$("#container").html('<div align="center">Your pen name will be written on your illustration, like so \'Request by ' + item_name + '\'.</div>');
		}
	});	
	
	$("#container").dialog("open");		
}

function SelectChapter3(){
	//update link
	$(this).parent().parent().find(".download-button a").attr("href", $(this).val());
}

function SelectChapter(){
	//close sliding panel
	$(this).parent().slideUp('slow');	

	//update label
	$(this).parent().parent().find(".download-box").html($(this).text());
	
	//update link
	$(this).parent().parent().find(".download-button a").attr("href", $(this).find("a").attr("href"));
}

function ToggleComicImage(){
	var item_id = $(this).attr("id").substr(7);	
	
	$("#tourpage_comic_main_picture a").addClass("invisible");
	
	$("#main-" + item_id).removeClass("invisible");
}

function HomeComicsOver(){
	//apply mouse over effect to both image and link
	var item_id = $(this).attr("id").substr(2);
	$(".hc-" + item_id).addClass("selected");
}

function HomeComicsOut(){
	//apply mouse out effect to both image and link
	var item_id = $(this).attr("id").substr(2);
	$(".hc-" + item_id).removeClass("selected");
}


