/************************************************
 *
 *  File     :  assets/js/main.js
 *  Version  :  v2
 *  Author   :  Andrej Simeonov, Adam Leon, Transmitter Studios 2007-2009
 *
 ************************************************/

$(document).ready(function(){

// Set external links - XHTML Compatibility reasons
    $("a[rel='external']").attr("target","_blank");

/* Image hover opacity


	$(".entry img").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads

	$(".entry img").hover(function(){
		$(this).fadeTo("fast", 1.0); // This should set the opacity to 100% on hover
	},function(){
   		$(this).fadeTo("slow", 0.6); // This should set the opacity back to 60% on mouseout
	});
*/


// Form validation 
    $(".required").after('<strong class="astrix">*</strong>');

    $("#contactForm").submit(function(e) {

        var trigger = "0";
        var emailre = /^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i;
        $(".validation").remove();
        $("input").blur();
        $("#message").html("");
        
        $(".required",this).each ( function() {
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");
            
            if($(this).attr("value") == '') {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Required Field</div>');
                trigger = "1";
            }
            else {
                $(listItem).removeClass("invalid");
            }
        });

        $(".required" + ".email",this).each ( function() {
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");

            if((!$(this).attr("value").match(emailre)) && ($(this).attr("value") != '')) {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Invalid Email</div>');
                trigger = "1";
            }
        });        
        
        if(trigger == "1") {
            $(".validation").fadeIn();
            e.preventDefault();
        }
        else {
            e.preventDefault();
            var inputs = [];
          
            $(':input',this).each(function() {
                inputs.push(this.name + '=' + escape(this.value));
            })
          
            jQuery.ajax({
                data: inputs.join('&'),
                url: this.action,
                timeout: 2000,
                error: function() {
                    //console.log("Failed to submit");
                },
            success: function(r) { 
              $('#message').append(r).fadeIn();
              $('form').get(0).reset();
            }
          })        
        
        }

        $(".invalid > .required:first").focus();
    });	

    $("#subForm").submit(function(e) {

        var trigger = "0";
        var emailre = /^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i;
        $(".validation").remove();
        $("input").blur();
        $("#message").html("");
        
        $(".required",this).each ( function() {
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");
            
            if($(this).attr("value") == '') {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Required Field</div>');
                trigger = "1";
            }
            else {
                $(listItem).removeClass("invalid");
            }
        });

        $(".required" + ".email",this).each ( function() {
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");

            if((!$(this).attr("value").match(emailre)) && ($(this).attr("value") != '')) {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Invalid Email</div>');
                trigger = "1";
            }
        });        
        
        if(trigger == "1") {
            $(".validation").fadeIn();
            e.preventDefault();
        }
        else {
            e.preventDefault();
            var inputs = [];
          
            $(':input',this).each(function() {
                inputs.push(this.name + '=' + escape(this.value));
            })
          
            jQuery.ajax({
                data: inputs.join('&'),
                url: this.action,
                timeout: 2000,
                error: function() {
                    //console.log("Failed to submit");
                },
            success: function(r) { 
              window.location.href='mailinglist_confirm.html';
              $('form').get(0).reset();
            }
          })        
        
        }

        $(".invalid > .required:first").focus();
    });	


});
// CLEAR FORM

      $.fn.clearForm = function() {
        return this.each(function() {
          var type = this.type, tag = this.tagName.toLowerCase();
          if (tag == 'form')
            return $(':input',this).clearForm();
          if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
          else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
          else if (tag == 'select')
            this.selectedIndex = -1;
        });
      };
  
 
// "show/hide" functionality 
   
/*   jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
}; 
  
  $('div#listForm').hide();
  
  $('a.full-toggle').click(function() {
    $('div#listForm').fadeToggle(400);
    return false;
  });

        });
*/

// Function to re-init certain components for the portfolio slides
jQuery.initfolio = function() {
    $(".thumbs > li > a").click( function(e) {
        e.preventDefault();
 		//$("#projects > ul").tabs('rotate', null);
        $(".thumbs > li").removeClass("current");
        var url = $(this).attr("href");
        var theAnchor = $(this);
        theAnchor.blur();

        if($(this).parent().hasClass('video')) {
            theAnchor.parent().parent().parent().siblings(".central").fadeOut(function() {
                $(this).html('<object data="'+url+'" type="application/x-shockwave-flash" width="462" height="282"><param name="movie" value="'+url+'" /></object>');
				//$(this).html('<object id="portPlayer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="462" height="282"><param name="movie" value="'+url+'" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="'+url+'" width="462" height="282"><!--<![endif]--><div><h1>Alternative content</h1><p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div><!--[if !IE]>--></object><!--<![endif]--></object>');
				//$(this).html('<div id="myContent"><h1>Alternative content</h1><p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>');
				$(this).fadeIn();
                theAnchor.parents("li").addClass("current");
            });
        }
        
        else {
                 theAnchor.parent().parent().parent().siblings(".central").fadeOut(function() {
                 $(this).html('<img src="'+url+'">');
                 $("this > img").fadeIn();
                 $(this).fadeIn();
                 theAnchor.parents("li").addClass("current");
            });
          }
        });
    }


// Folio init
    jQuery.initfolio();

  
