/*=================================================================================================================================
Title:				Javascript - lewro.com
Type:					Global
File:					global.js
JS version:		1.0
Author:				Roman Leinwather
E-mail:				roman.leinwather@gmail.com
Website:			http://www.lewro.com
Copyright:		Copyright (c) 2006 - 2010 Roman Leinwather
Updated:			10th Jan 2010
==================================================================================================================================

TABLE OF CONTENT


I.		Topics Switch		
II.		Read More
II.		Portfolio overlay - home page
IV.		Most Popular
V.		Search
VI. 	Header Artwork
VII. 	Projects Tabs & Nav - Home Page
VII. 	Post
IX.		Comment Form
X.	  Post Layout Switch
XI.		Sidebar Close buttons
XII.	Post Controlers
XIII. Textarea Adjustments
XIV.  Portfolio Switch

==================================================================================================================================
*/

/*		I. Topics Switch
=================================================================================================================================*/
$(function (){
	// only on the Home Page 
	if($("#home").length > 0) {	
		$('<ul><li id="news" class="active">News</li><li id="topics">Topics</li></ul>').insertBefore('#search');
	
		$('#topics').click(function(){
			$(this).addClass("active");
			$('#news').removeClass("active");
			$('#tags_list').fadeIn(500);
		});
		$('#news').click(function(){
			$(this).addClass("active");
			$('#topics').removeClass("active");
			$('#tags_list').fadeOut(500);
		});
	}
});

/*		II. Read More
=================================================================================================================================*/
$(function (){
	// only on the Home Page 
	if($("#home").length > 0) {	
		$('.post').hover(
			function(){
				$(this).children('.commentsDescription').stop(true, true).fadeIn(500);		
			},function(){
				$('.commentsDescription').stop(true, true).fadeOut(500);
		});
	}
});


/*		III. Portfolio overlay - home page
=================================================================================================================================*/
$(function (){
	// only on the Home Page 
	if($("#home").length > 0) {	
		$('.home_portfolio_item img').hover(
			function(){
				if($(this).next().css("display") == "none"){
					$(this).next().stop(true,true).fadeIn(500);			
				}else{
				}
		},function(){
				$(this).next().stop(true,true).fadeOut(500);
		});
		
		$('.portfolio_overlay_description').hover(
			function(){
				$(this).stop(true,true).show();			
		}, function(){
		});
	}
});

/*		IV. Most Popular
=================================================================================================================================*/
$(function (){
	$('#most_popular li').hover(
		function(){
			$(this).addClass("on");
	},function(){
			$(this).removeClass("on");	});	
});

$(function (){
	$('#most_popular li').each(
		function(i){
			$i = "<span id=i" +i+"/>"
			$(this).append($i);
	});
});

/*		V. Search
=================================================================================================================================*/
$(function (){
	// only where search box is available 
	if($("#search").length > 0) {	
		$('#search').append('<span id="cancell_search"></span>');
	
		$('#cancell_search').click(function(){
			$('#s').val("");
		});
	}
});

/*		VI. Header Artwork
=================================================================================================================================*/
$(function (){

	// Insert the navigation
	$('#artwork_nav_cover').append('<ul id="art_nav"><li class="first active"></li><li class="second"></li><li class="third"></li><li class="fourth"></li></ul>');

	$images = $('#images img'); 
	$imagesSize = $($images).length;
	
	// Loop through images
	$n=1;
	$TimeOutInterval = 6000;
	$fadeInterval = 1500;

	function loopImages (){
		$actual_image = $images[$n];
		$($images).fadeOut($fadeInterval);
		
		function fadeoutwait (){
			$($actual_image).fadeIn($fadeInterval);
			// get the class of the images so it can be matched with navigation
			$actual_claass = ($($actual_image).attr("class"));

      // Body class handling for bg artwork section bg image
      $('body').removeClass();
      $('body').addClass($actual_claass);

			$actaul_nav_list = '#art_nav .' + $actual_claass;
			// Remove the active class 
			$('#art_nav li').removeClass("active");
			// Highlight the nav item
			$($actaul_nav_list).addClass("active");

		}	
		
		setTimeout(fadeoutwait,$fadeInterval);
		$n++;
		
		// At the end of the loop show the first one again
		if($n == ($imagesSize + 1)){ 
			$('#images img').hide();
      // Bring the default bg for artworkarea
      $('body').removeClass();
			$($images[0]).fadeIn($fadeInterval);
			// Remove the active class
			$('#art_nav li').removeClass("active");
			// Highlight the nav item
			function highlightTheFirstItem(){
				$('#art_nav li:first').addClass("active");
				// Let users interact with Artwork only after the slideswho is over
				$('#art_nav li').css({ "cursor" : "pointer" });
				$('#art_nav').css({"opacity" : 1});
				bringOnclickHandlers();
			}
			setTimeout(highlightTheFirstItem,$fadeInterval);
			}else{
				setTimeout(loopImages,$TimeOutInterval);
		}
	}
	// call the looping animation
	setTimeout(loopImages,$TimeOutInterval);
	
	// Artwork Navigation
	function bringOnclickHandlers (){		

		$('#art_nav li').click(function(){
  		
      // set the image which needst to be shown
  		$current_class_name = $(this).attr("class");
      
      if($(this).hasClass('active')) {
          // no action, clicking on the selected item
      }else{

        // Body class handling for bg artwork section bg image
        $('body').removeClass();
        $('body').addClass($current_class_name);
  
  			$images_to_show = '#images .' + $current_class_name;
  
  			// show / hide needed image
  			$('#images img').hide();
  			$($images_to_show).show();
  
  			// highlight the nav	
  			$('#art_nav li').removeClass("active");
  			$(this).addClass("active");
      }
		});
	}
});


/*		VII. Projects Tabs & Nav - Home Page
=================================================================================================================================*/
$(function(){
	// only on the Home Page 
	if($("#home").length > 0) {	
	
		$('<ul><li id="customer_projects"><a class="active customer_projects_box" href="#customer_projects_box" title="Customer Projects">Customer Projects</a></li><li id="personal_projects"><a href="#personal_projects_box"  class="personal_projects_box" title="Personal Projects">Personal Projects</a></li></ul>').insertBefore('#portfolio_nav');
		porftolioTabNav();
	}
});
	
function porftolioTabNav() {
	if($("#home").length > 0) {
		$('#portfolio_content ul li a').click(
			function(){
				// $content_id = $(this).attr("href") // KLUDGE: Doesn't work in IE7;
				$content_id_1 = $(this).attr("class");
				$content_id = "#" + $content_id_1;
				$('#portfolio_content .project_box').hide();
				$($content_id).show();
				$('#portfolio_content ul li a').removeClass("active");
				$(this).addClass("active");
				return false;
		});
	}
}
 
// Customer Projects Nav
$(function (){
	// only on the Home Page 
	if($("#home").length > 0) {	

		$('.project_nav li').click( 
			function(){
				$current_list = $(this);
	
				// Next & Previous buttons handlings
				if($($current_list).hasClass("left_customer_project_nav")){
					// Get the previous item
					$current_active_item = $('.active_portfolio_item')
					
					if((($current_active_item).prev(".home_portfolio_item")).length == 0){
						// leave it as it is
					}else{
						$('.home_portfolio_item').removeClass("active_portfolio_item");
						$('.home_portfolio_item').hide();
						$($current_active_item).prev(".home_portfolio_item").show();
						$($current_active_item).prev(".home_portfolio_item").addClass("active_portfolio_item");	
						$('.project_nav li').removeClass("active");
						
						// Handle the navigation
						$('.project_nav li').each(
							function(){
								$active_porftolio_item_classes = $('.active_portfolio_item');
								$portfolio_nav_list_class = $(this).attr("class");
								if($($active_porftolio_item_classes).hasClass($portfolio_nav_list_class)){
									$(this).addClass("active");
								}
						});	
					
					}
				}else if ($($current_list).hasClass("right_customer_project_nav")){
					// Get the next item
					$current_active_item = $('.active_portfolio_item')
					
					// If there is no next one item
					if((($current_active_item).next(".home_portfolio_item")).length == 0){
						// leave it as it is
					}else{
						$('.home_portfolio_item').removeClass("active_portfolio_item");
						$('.home_portfolio_item').hide();
						$($current_active_item).next(".home_portfolio_item").show();
						$($current_active_item).next(".home_portfolio_item").addClass("active_portfolio_item");				
						$('.project_nav li').removeClass("active");
	
						// Handle the navigation
						$('.project_nav li').each(
							function(){
								$active_porftolio_item_classes = $('.active_portfolio_item');
								$portfolio_nav_list_class = $(this).attr("class");
								if($($active_porftolio_item_classes).hasClass($portfolio_nav_list_class)){
									$(this).addClass("active");
								}
						});	
	
					}
				// Handling of all the individual buttons
				}else{
					if ($current_list.hasClass("active")){
						// already showing the right one, leave it
					}else{
						$current_class = $(this).attr("class");
						$('.home_portfolio_item').removeClass("active_portfolio_item");
						$('.home_portfolio_item').hide();
						
						$('.home_portfolio_item').each(
							function(){	
								if($(this).hasClass($current_class)){
									// Highlight the nav item
									$('.project_nav li').removeClass("active");
									$($current_list).addClass("active");
									// Show the appropriate portfolio nav
									$(this).addClass("active_portfolio_item");
									$(this).show();
								}else{
								}
						});
					}
				}
		});
	}
});

/*		VIII. Post
=================================================================================================================================*/
$(function(){
	// only on Post Page 
	if($("#post_area").length > 0) {	

		// check images
		if($('.post img').length > 0) {
			$('.post img').each(
				function(){		
					$(this).parent("a").clone().appendTo('#article_images_cover');
					$(this).parent("a").parent("p").remove();
				});
			}else{
				$('<p>There is no image attached to this article</p>').appendTo('#article_images_cover');
		}
		
		// check video
		if($('.post object').length > 0) {
			$('.post object').each(
				function(){		
					$(this).clone().appendTo('#article_images_cover');
					$(this).parent("p").remove();
				});
		}else{
			$('<p>There is no video attached to this article</p>').appendTo('#article_images_cover');
		}
	}
});


/*		IX. Comment Form
=================================================================================================================================*/
$(function(){
	// only on Post Page 
	if($("#post_area").length > 0) {	
		//inputs
		$('form input').focus(
			function(){
				$('form input').removeClass("active");
				$('form textarea').removeClass("active");
				$(this).addClass("active");
		});		
		// textarea
		$('form textarea').focus(
			function(){
				$('form input').removeClass("active");
				$('form textarea').removeClass("active");
				$(this).addClass("active");
		});		
		
		$('.add_comment a').click(
			function(){
				$('#author').addClass("active");

				// scroll to the selected item
				$target = $('#comment_form_cover_main');
				var targetOffset = $target.offset().top;
				
				$('html, body').animate({
						scrollTop: targetOffset
					}, 1000);
				return false;
		});		
	}
});

$(function(){
	if($('#commentform').length == 0){
		// no comment form...
	}else{
		// Remove values on click
		$('#commentform input').focus(function(){
			// Get the ID of the element and covert it to our label
			$id = $(this).attr("id") + ": ";
			
		 	// If there is default value
 			if($(this).val() == $id){
 				// Remove the value 
				$(this).val("");
			}
	 });
 
		// On blur check if the element is empty
	 	// if so use the default value...
	 	$('#commentform input').blur(function(){
	 		$id = $(this).attr("id") + ": ";
	 		if($(this).val() == ""){
				$(this).val($id);
			}
		});
	 	
		// On submit - Check if any of fields has got default values
	 	// if so remove them
		$('#commentform').submit(
			function(){
				
				// remove the error classes when revalidating...
				$('form input').removeClass("error");
				$('form textarea').removeClass("error");
				
				// check if authore is set
				if($('#author').val() == "author: "){
					$('#author').addClass("error");
					return false;
				}	

				// check if email is set & correct format
 				var email = ($('#email').val());
				var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

				if($('#email').val() == "email: "){
					$('#email').addClass("error");
					return false;
				} else if (!filter.test(email)){
					$('#email').addClass("error");
					return false;
				}

				// check if textarea is set
				if($('form textarea').val() == ""){
					$('form textarea').addClass("error");
					return false;
				}	

				// just in case the original values went through get rid of them (should not happen)
				$('#commentform input').each(
					function(){
						if($(this).val() == ($(this).attr("id") + ": ")){
							// remove the default value
							$(this).val("");
						}
				});
	 	}); 
	}
});

/*		X. Post Layout Switch
=================================================================================================================================*/
$(function(){
	// only on the post page...
	if($("#post_area").length > 0) {	

		// when page loads switch the js layout
		$('#comments').removeClass('switchedOf').addClass('switchedOn');

		$('#grid').click(function(){
			layoutGrid($(this));
		});

		$('#lines').click(function(){
			 layoutLines($(this));
		});

 		$('.commentList li .clear').click(function(){
				$target = $(this).parent();
			
			layoutLines($('#lines'));
			
			// scroll to the selected item
			var targetOffset = $target.offset().top;
			
			$('html, body').animate({
					scrollTop: targetOffset
				}, 1000);
		});
		
		function layoutGrid($clicker){
			if($('.switchedOf').length > 0){
				$('#comments').removeClass('switchedOf').addClass('switchedOn');
				$('#comments_switch li').removeClass("active");
				$('#comments_switch li').removeClass("off");
				$($clicker).addClass("active");
				$($clicker).next().addClass("off");
			}
		}

		function layoutLines($clicker){
			if($('.switchedOn').length > 0){
				$('#comments').removeClass('switchedOn').addClass('switchedOf');
				$('#comments_switch li').removeClass("active");
				$('#comments_switch li').removeClass("off");
				$($clicker).addClass("active");
				$($clicker).prev().addClass("off");
			}
		}
	}
});

/*		XI. Sidebar Close buttons
=================================================================================================================================*/
$(function(){
	turnonSection();

	function turnonSection(){
		$('#secondary_content .turn_on').click(function(){
			$(this).next().hide();
			$(this).removeClass("turn_on");
			$(this).addClass("turn_off");
			turnoffSection();
		});
	}
	
	function turnoffSection(){	
		$('#secondary_content .turn_off').click(function(){
			$(this).next().show();
			$(this).removeClass("turn_off");
			$(this).addClass("turn_on");
			turnonSection();
		});
	}

	if($('#article_images p').length > 1){
		$('#secondary_content .turn_on').hide();
	}
});

/*		XII. Post Controlers
=================================================================================================================================*/
$(function(){
	// only on Post Page 
	if($("#post_area").length > 0) {	
		$moveHeight = 370;

		$('#up').click(function(){
			$currentMargin = parseInt($('.entry').css("margin-top").replace('px',''));
			$moveDown = $currentMargin - $moveHeight + "px"; 
			$('.entry').animate({				
				"marginTop" : $moveDown
			},500)	
		});


		$('#down').click(function(){
			$currentMargin = parseInt($('.entry').css("margin-top").replace('px',''));
      
       // only scroll down if we have a negative margin value to increase
       if ($currentMargin < 0) {
  			$moveUp = $currentMargin + $moveHeight + "px";
        $('.entry').animate({				
				  "marginTop" : $moveUp
			 },500)	
      }
		});
	}
});

/*		XIII. Textarea Adjustments
=================================================================================================================================*/
$(function(){
	if($("#contact_form").length > 0) {	
	  $textarea = $('#contact_form form textarea');  
	 	$characterLimit = 300;
		$originalHeight = $($textarea).css("height").replace("px","");
	  $animateTime = 500;
	
	 $textarea.keyup(function(){
	    $currentCharacters = $($textarea).val().length;          
	    $heightRatio = $currentCharacters / $characterLimit;     
	    $roundRatio = Math.round($heightRatio); 
	
	    if($currentCharacters > $characterLimit){
	      $newHeight = (parseInt($originalHeight)) * $roundRatio + "px";
	      $($textarea).stop().animate({height: $newHeight}, $animateTime);
	    }else{
	      $($textarea).stop().css({height: $originalHeight});
	    }
	  });
	}
});

/*		XIII. Layout Switch Portfolio Page
=================================================================================================================================*/
$(function(){
	if($("#portfolio").length > 0) {
		$('#layout_switch #grid').click(function(){
			$('#layout_switch li').removeClass('active').removeClass('off');	
			$(this).addClass('active');
			$(this).next().addClass('off');
			$('#portfolio_items').addClass('grid_layout');
		});

		$('#layout_switch #lines').click(function(){
			$('#layout_switch li').removeClass('active').removeClass('off');	
			$(this).addClass('active');
			$(this).prev().addClass('off');
			$('#portfolio_items').removeClass('grid_layout');
		});
	}
});

/*		XIV. Archive Page
=================================================================================================================================*/
$(function(){
	if($(".my_archive").length > 0) {
		$('.archive_post').hover(
			function(){
				$(this).children('.read_more').stop(true,true).fadeIn(500);
			},function(){
				$(this).children('.read_more').stop(true,true).fadeOut(500);
		});
	}
});

/*		XV. Portfolio Switch
=================================================================================================================================*/
$(function(){
  $('#web_designs').click(function(){
    $('#type_switch li').addClass('off');
  	$('#logo_items').hide();
  	$('#portfolio_items').show();
    $(this).removeClass('off');
    $('#layout_switch').show();
  });	
  $('#brand_identity').click(function(){
    $('#type_switch li').addClass('off');
  	$('#logo_items').show();
  	$('#portfolio_items').hide();
    $(this).removeClass('off');
    $('#layout_switch').hide();
  });	
});

$(function(){
	$('.logo_item:even').addClass('even');
});


