/**
 * @author kyasui
 */
jQuery.noConflict(); 

var core ={
		init: jQuery(document).ready(function() {
					jQuery(window).load(
		    			function() {
       				 		jQuery('.slideShow').cycle({
								fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
								timeout: 5000,
								speed: 2000
							});
							
							

    		   		 	}
					);
			
			jQuery("a.fancyImg").fancybox({
					'overlayColor' 		: '#000',
					'overlayOpacity'	: '.8',
					'transitionIn'		: 'fade',
					'transitionOut'		: 'fade',				
					'speedIn'	 		: '5',
					'speedOut'			: '5',
					'showNavArrows'		: 'true'
			});
			
			jQuery("img.galleryImg").lazyload({
  		  		effect      : "fadeIn"
			});
			
			core.initPagination();
			
			jQuery('input#name').watermark('i.e. Joe Smith');
			jQuery('input#location').watermark('i.e. Syracuse, NY');
			jQuery('input#phone').watermark('i.e. 555-5555');
			jQuery('input#email').watermark('i.e. joe@smith.com');
			jQuery('textarea#service').watermark('i.e. I want a tree installation on a five acre development.');
			
			jQuery('.slideShow').cycle({
				fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
				timeout: 5000,
				speed: 2000
			});
			
			jQuery('#list1').accordion({autoheight: false});
			
			jQuery('#submitBtn').unbind('click').click(function(e) {
				e.preventDefault();
												
				var valid = '';
				var isr = ' is required.';
 				var name = jQuery("input#name").val();
 				var location = jQuery("input#location").val();
			    var phone = jQuery("input#phone").val();
		  		var email = jQuery("input#email").val();
		  		  		
		  		var service = jQuery("textArea#service").val();
		  		
		  		
					 	 	
				
							
				if (name.length<1) {
					valid += '\n'+ "Name is required.";
				}
				if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
					valid += '\n'+ "Email is required.";
				}
				if (location.length<1) {
					valid += '\n'+ "Location is required.";
				}

				if (valid!='') {
					alert(valid);
				}
				else {
					var dataString = 'name='+ name + '&location=' + location + '&phone=' + phone + '&email=' + email  + '&service=' + service;
					
					jQuery.ajax({
					    type: "POST",
					    url: "email.php",
					    data: dataString,
					  	success: function() {
					    	jQuery('.form').html("<div id='message'></div>");
						    jQuery('#message').html("<h2>Success!</h2>")
						    .append("<p>Thanks for contacting us. <br />We will be in touch soon.</p>")
					        .hide()
						    .fadeIn(500); 				   
						    jQuery('.profilePic').hide();
						    jQuery('.profilePic2').show();
						}
				 	 }); 
				}
				return false;
			});   
 		}),
		
		initPagination: function () {
			//how much items per page to show
			var show_per_page = 3; 
			//getting the amount of elements inside content div
			var number_of_items = jQuery('#newsEntries').children().size();
			//calculate the number of pages we are going to have
			var number_of_pages = Math.ceil(number_of_items/show_per_page);
	
			//set the value of our hidden input fields
			jQuery('#current_page').val(0);
			jQuery('#show_per_page').val(show_per_page);
	
			//now when we got all we need for the navigation let's make it '
	
			/* 
			what are we going to have in the navigation?
			- link to previous page
			- links to specific pages
			- link to next page
			*/
			var navigation_html = '<a class="previous_link" href="javascript:core.previous();">Prev</a>';
			var current_link = 0;
			while(number_of_pages > current_link){
				navigation_html += '<a class="page_link" href="javascript:core.go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
				current_link++;
			}
		
			navigation_html += '<a class="next_link" href="javascript:core.next();">Next</a>';
	
			jQuery('.page_navigation').html(navigation_html);
	
			//add active_page class to the first page link
			jQuery('.page_navigation .page_link:first').addClass('active_page');
	
			//hide all the elements inside content div
			jQuery('#newsEntries').children().css('display', 'none');
	
			//and show the first n (show_per_page) elements
			jQuery('#newsEntries').children().slice(0, show_per_page).css('display', 'block');
		},
		
		previous: function (){
				//alert('prev');
				new_page = parseInt(jQuery('#current_page').val()) - 1;
				//if there is an item before the current active link run the function
				if(jQuery('.active_page').prev('.page_link').length==true){
					core.go_to_page(new_page);
				}
	
		},

		next: function (){
				//alert('next');
				new_page = parseInt(jQuery('#current_page').val()) + 1;
				//if there is an item after the current active link run the function
				if(jQuery('.active_page').next('.page_link').length==true){
					core.go_to_page(new_page);
				}
	
		},
		
		go_to_page:	function (page_num){
				//get the number of items shown per page
				var show_per_page = parseInt(jQuery('#show_per_page').val());
	
				//get the element number where to start the slice from
				start_from = page_num * show_per_page;
	
				//get the element number where to end the slice
				end_on = start_from + show_per_page;
	
				//hide all children elements of content div, get specific items and show them
				jQuery('#newsEntries').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');
		
				/*get the page link that has longdesc attribute of the current page and add active_page class to it
				and remove that class from previously active page link*/
				jQuery('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');
	
				//update the current page input field
				jQuery('#current_page').val(page_num);
		}
	}		

