var $j = jQuery.noConflict();




$j(function(){
	
	$j(document).ready(function () {
		//var photoList= $j('.portfolio-slideshow-hidden div'); //.contents();
		//console.log(photoList);
  		//photoList.append('.slideshow-wrapper');

  		//$j('.slideshow-wrapper').append(photoList);
  		
  		if ($j('body').hasClass('photos-section') ){
  			setTimeout ( "copyHiddenImages()", 500 );
	  		initSlideShow();
	  		//console.log('porfolio loads!!!!');  			
  		}

	});
	


	$j('#menu-item-3').hover(function() {
		$j('.sub-ref').stop(true, true).fadeOut();
		$j('.sub-char').stop(true, true).fadeIn();
	});

	$j('#menu-item-4').hover(function() {
		$j('.sub-char').stop(true, true).fadeOut();
		$j('.sub-ref').stop(true, true).fadeIn();
	});

		

	$j('#menu-item-4').mouseleave(function() {
		$j('.sub-ref').stop().fadeOut();
		if ( $j("body").hasClass("char-section") ){
			$j('.sub-char').stop().fadeIn();					
		}else{
			$j('.sub-char').stop().fadeOut();				
		}
	});
		
	$j('#menu-item-3').mouseleave(function() {
		$j('.sub-char').stop().fadeOut();					
		if ( $j("body").hasClass("ref-section") ){
			$j('.sub-ref').stop().fadeIn();
		}else{
			$j('.sub-ref').stop().fadeOut();					
		}
	});

	
	
	$j('div.slideshow-nav a.slideshow-next').click(function() {
		//console.log('button fires!');
		
		$j('.portfolio-slideshow').stop(true, true).show();
	});

});
		
		
		
	

		$j(window).load(function() {
			//initSlideShow();
			$j('.facebook').stop(true, true).show();
			
			
			
		});
		
		
		///////////
		
		function initSlideShow(){
			//console.log("INIT");
			$j('div.slideshow-wrapper').css('visibility','visible');$j(function() {
						var index = 0, hash = window.location.hash;
						if (hash) {
							index = /\d+/.exec(hash)[0];
							index = (parseInt(index) || 1) - 1; // slides are zero-based
						}
							
						$j('#portfolio-slideshow1').cycle({
								fx: 'fade',
								speed: 400,
								timeout: 0,
								next: '#slideshow-wrapper1 a.slideshow-next',
								startingSlide: index,
								prev: '#slideshow-wrapper1 a.slideshow-prev',
								before:     onBefore,
								after:     onAfter,
								pager:  '#slides1',
								manualTrump: false,
								cleartypeNoBg: true,
								pagerAnchorBuilder: function(idx, slide) {
								// return sel string for existing anchor
								return '#slides1  li:eq(' + (idx) + ') a'; }
						});
					
				
						$j('.slideshow-nav1 a.pause').click(function() { 
							$j('#portfolio-slideshow1').cycle('pause');
							$j('.slideshow-nav1 a.pause').hide();
							$j('.slideshow-nav1 a.play').show();
						});
					
						$j('.slideshow-nav1 a.play').click(function() { 
							$j('#portfolio-slideshow1').cycle('resume');
							$j('.slideshow-nav1 a.play').hide();
							$j('.slideshow-nav1 a.pause').show();
						});
						
						function onBefore(curr,next,opts) {
							$j("p.slideshow-caption, p.slideshow-title, p.slideshow-description", this).css("visibility", "hidden");
						}
						
						function onAfter(curr,next,opts) {
							
							var $jht = $j("img",this).attr("height");
							if ($j("p.slideshow-caption", this).length ) { 
								var $joht = $j("p.slideshow-caption", this).outerHeight('true');
							} else {
				    			var $joht = 0;
							}
							if ($j("p.slideshow-description", this).length ) {
								var $jpht = $j("p.slideshow-description", this).outerHeight('true');
							} else {
							var $jpht = 0;
				    			
							}
							if ($j("p.slideshow-title", this).length ) { 
								var $jqht = $j("p.slideshow-title", this).outerHeight('true'); 
							} else {
				    			var $jqht = 0;
							} 
							$j('#portfolio-slideshow1').css("height", $jht + $joht + $jpht + $jqht);
										
							$j("p.slideshow-caption, p.slideshow-title, p.slideshow-description", this).css("visibility", "visible");
							var caption = $j('#slideshow-info1').html(caption);
						}
						
			}); 
					
		}		
		
		
			
		
function initform(){
    var options = { 
        //target:        '#form_output',   // target element(s) to be updated with server response 
      	beforeSubmit:  validate,  // pre-submit callback 
        success:    showResponse,     // post-submit callback 
 		
 		// Define the error method.
		error: showFail

        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind to the form's submit event 
    $('#email_submit').submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options); 
 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
}; 


function copyHiddenImages(){
	console.log("COPY!");
	var photoList = $j('.slideshow-hidden').contents();
	$j('.portfolio-slideshow').append(photoList);
	console.log("---> "+ photoList + " - ");
	initSlideShow();
}
	
// pre-submit callback
function validate(formData, jqForm, options) { 


//alert('Validate'+ $(".entry").val());

	var email= $(".entry").val();
	if(isValidEmailAddress(email)){
		//alert("OKAY!");
	} else {
		//alert("NOT OKAY!");
		
		$(".error_entry").fadeIn();
		$(".error_backend").fadeOut();

		$(".success").fadeOut();	
		
	 return false; 
	}
	

    // formData is an array of objects representing the name and value of each field 
    // that will be sent to the server;  it takes the following form: 
    // 
    // [ 
    //     { name:  username, value: valueOfUsernameInput }, 
    //     { name:  password, value: valueOfPasswordInput } 
    // ] 
    // 
    // To validate, we can examine the contents of this array to see if the 
    // username and password fields have values.  If either value evaluates 
    // to false then we return false from this method. 
 
    /*for (var i=0; i < formData.length; i++) { 
        if (!formData[i].value) { 
            alert('Please enter a value for email address'); 
            return false; 
        } 
    }*/
    
     
    
    //alert('Both fields contain values.'); 
}

 
 
	function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
	}
 
 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    //alert('About to submit: \n\n' + queryString); 
 	
 	// TODO: Put in some validation
 	
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form) 

{ 
	$(".error_entry").fadeOut();
	$(".error_backend").fadeOut();

	$(".success").fadeIn();
	$(".entry").val("Enter your email address");
 
 
    

    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    //    '\n\nThe output div should have already been updated with the responseText.'); 
} 


// failure 
/*
// Correct function listens for success from backend
function showFail()  { 


	$(".error_entry").fadeOut();
	$(".success").fadeOut();

	$(".error_backend").fadeIn();

    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
} 
*/

// failure - Shows success no matter what gets returned from the backend
function showFail()  { 
	$(".error_entry").fadeOut();
	$(".success").fadeIn();
	$(".error_backend").fadeOut();
} 







