//Ajax Post Page
post_page = ThemeAjax.ajaxurl;

// Check if we can use the slide and orientation change methods
if ((navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPod') != -1) && (navigator.userAgent.indexOf('Opera') == -1)) 
	{
		var jQT = new jQuery.jQTouch({
			statusBar: 'black-translucent',
			slideSelector: 'a.slide',
			preloadImages: ['../layers/sprite.png', '../layers/menu-bg.png', '../layers/alt-button-bg.png']
		});
	}
	
// Slider
function feature_slide(newid, moveby)
	{	
		jQuery(".post-count span").html(newid);
		new_left_attr = -(newid-1)*moveby;		
		jQuery("#feature-slider").css("left", new_left_attr+"px");
	}
	
//CSS3 Animations
function touchfadeIn(element){
	element.removeClass("out").addClass("in");
	setTimeout(function(){
		element.addClass("current");
	}, 300);		
}
function touchfadeOut(element){
	element.addClass("out").removeClass("in");
	setTimeout(function(){
		element.removeClass("current");
	}, 300);	
}

//Ajax for retrieving the next posts
function load_posts(){
	offset = (jQuery(".post-list").children("li").length-2);
	
	// Semi-hide load button
	jQuery(".load-more a").animate({opacity: 0.4});
	
	// Show the posts		
	touchfadeIn(jQuery(".post-list").children("li"));
	
	// Fetch new posts and populate the list
	jQuery.get(post_page, {action: 'ocmx_load-posts', cat: jQuery(this).attr("rel"), offset: jQuery(".post-list").children("li").length}, 
		function(data)
				{
					// Make sure the posts that have already loaded don't do anything funny
					jQuery(".post-list").children("li").removeClass("slide");
					
					// Remove the "Load More Posts" button								
					save_btn = jQuery(".load-more").remove();
					
					// Add the new posts to the li
					setTimeout(function(){
						original_html = jQuery(".post-list").html();
						new_html = original_html+data;
						jQuery(".post-list").html(new_html);

						// Put back the "Load More Posts" button at the end of the last story
						offset = (jQuery(".post-list").children("li").length-1);			
						
						save_btn.insertAfter(jQuery(".post-list").children("li:eq("+offset+")"));
						
						//Reset the load more button, this is actually for browsers which don't support jQuery().live() function
						jQuery(".load-more a").unbind("click").bind("click", function(){load_posts()}).stop();					
						jQuery(".load-more a").animate({opacity: 1});
					}, 300);
					// Set the new Image sizes
					set_image_sizes();
				}
			);
	return false;
}

function set_image_sizes(){
	body_width = jQuery('body').width();
	resize_w = (body_width-30);
		
	jQuery(".post-thumbnail img, .thumbnail img, .post-image img, .copy img").each(function(){
		
		orig_w = jQuery(this).width();
		orig_h = jQuery(this).height();
		height_ratio = (orig_h/orig_w);
		resize_w = (body_width-30);
		resize_h = (resize_w*height_ratio);
		
		if(!isNaN(resize_h))
			{jQuery(this).css("maxWidth", (resize_w)+"px").css("maxHeight", resize_h+"px");}
		
	});
	
	jQuery(".wp-caption").css("maxWidth", (resize_w-10)+"px");
	jQuery(".wp-caption img").each(function(){	
		orig_w = jQuery(this).width();
		if(orig_w >= (resize_w))
			{jQuery(this).attr("width", (body_width-45));}
	});
	
	jQuery("object").attr("width", (resize_w));	
	jQuery("iframe").attr("width", (resize_w));		
}
jQuery(document).ready(function(){
		// If we support javascript (which we clearly do!) Set the menu href's to be hashes
		jQuery("#menu-types a, .drop-down").attr("href", "#");
});
jQuery(window).load(function()
	{
			
		body_width = jQuery('body').width();
		jQuery.current_feature = 1;
		
		// Set post image width
		set_image_sizes();
		jQuery("object").attr("width", (body_width-20));


		if (jQT) 
			{
				// Set the Slider Attributes
				jQuery(".slider, .slider ul li").css("width", (body_width+"px"));
				jQuery(".slider").css("height", (jQuery(".slider img").height()+20)+"px");
					
				jQuery('#feature-slider').live("swipe", function(evt, data) {
					// Set the sliding width and max count
					move_by = (jQuery('body').width()+20);
					max_clicks = jQuery("#feature-count").html();
					
					// Check which direction we're moving in
					if(data.direction == "left" && jQuery.current_feature == max_clicks)
						newid = 1;
					else if(data.direction == "left")
						newid = ((jQuery.current_feature/1)+1);
					else if (data.direction == "right" && jQuery.current_feature == 1)
						newid = max_clicks;				
					else
						newid = ((jQuery.current_feature/1)-1);
						
					// Slide frame and set current slide
					feature_slide(newid, move_by)
					jQuery.current_feature = newid;
					return false;
				});
				
				jQuery('body').live("turn", function(event, info){
					//Set the body width
					body_width = jQuery('body').width();
					
					// Set post images and search box
					set_image_sizes();
										
					// Set the Slider Attributes
					move_by = (body_width+20);
					jQuery(".slider, .slider ul li").css("width", body_width+"px");
					jQuery(".slider").css("height", (jQuery(".slider img").height()+20)+"px");
					
					feature_slide(jQuery.current_feature, move_by)
				});
			}
		
		jQuery(".drop-down").click(function(){										  
			if(!jQuery("#menu-types").hasClass("in"))
				{
					//Find the first menu item					
					jQuery("#menu-types").children("li:eq(0)").addClass("active");
					
					// Set the Current Menus
					current_menu = jQuery("#menu-types").children("li:eq(0)").children("a").attr("rel");
										
					//Transition
					touchfadeIn(jQuery("#menu-types"));
					touchfadeIn(jQuery(current_menu));
					
					jQuery.openid = current_menu;
					
					//Change the container to send us no-where
					jQuery("#content-container").bind("click", function(){
																		
						// Set the Current Menus
						current_menu = jQuery("#menu-types").children(".active").children("a").attr("rel");
						
						//Transition
						touchfadeOut(jQuery("#menu-types"));
						touchfadeIn(jQuery(current_menu));
						
						setTimeout(function(){jQuery("#menu-types").children(".active").removeClass("active");}, 350);
						
						// Return bodily functions
						jQuery("#content-container").unbind("click");
						
						return false;
					});
				}
			else
				{						
					// Sub Menu Id
					current_menu = jQuery("#menu-types").children(".active").children("a").attr("rel");
					
					//Transition
					touchfadeOut(jQuery("#menu-types"));
					touchfadeOut(jQuery(current_menu));
					
					setTimeout(function(){jQuery("#menu-types").children(".active").removeClass("active");}, 350);
					jQuery("#content-container").unbind("click");
				}
			return false;		
		});
		jQuery("#menu-types li").click(function(){	
			//Set the menu Id
			useid = jQuery(this).children("a").attr("rel");			
			
			if(jQuery(this).hasClass("active"))
				{		
					//Remove active class for the button and hide sub menu
					jQuery(".active").removeClass("active");
					touchfadeOut(jQuery(useid));
				}
			else
				{
					// Apply Active class to the menu
					jQuery(".active").removeClass("active");
					jQuery(this).addClass("active");
					
					if(useid == "#search")
						document.getElementById("s").focus();
						
					//Transition
					touchfadeOut(jQuery(jQuery.openid));					
					touchfadeIn(jQuery(useid));
					
					jQuery.openid = useid;
				}
			return false;
		});
		
		jQuery(".load-more a").click(function(){		
			load_posts();
			return false;
		});
	});




