/*********************
 //* jQuery Multi Level CSS Menu #2- By Dynamic Drive: http://www.dynamicdrive.com/
 //* Last update: Nov 7th, 08': Limit # of queued animations to minmize animation stuttering
 //* Menu avaiable at DD CSS Library: http://www.dynamicdrive.com/style/
 *********************/
var arrowimages = {
    down: ['', ''],
    right: ['', '']
}
var jqueryslidemenu = {

    animateduration: {
        over: 200,
        out: 25
    }, //duration of slide in/ out animation, in milliseconds
    buildmenu: function(menuid, arrowsvar){
        jQuery(document).ready(function($){
            jQuery("#nav a").removeAttr("title");
            var $mainmenu = jQuery("#" + menuid + ">ul")
            var $headers = $mainmenu.find("ul").parent()
            $headers.each(function(i){
                var $curobj = jQuery(this)
                var $subul = jQuery(this).find('ul:eq(0)')
                this._dimensions = {
                    w: this.offsetWidth,
                    h: this.offsetHeight,
                    subulw: $subul.outerWidth(),
                    subulh: $subul.outerHeight()
                }
                this.istopheader = $curobj.parents("ul").length == 1 ? true : false
                $subul.css({
                    top: this.istopheader ? this._dimensions.h + "px" : 0
                })
                
                var $targetul = jQuery(this).children("ul:eq(0)")
                this._offsets = {
                    left: jQuery(this).offset().left,
                    top: jQuery(this).offset().top
                }
                var menuleft = this.istopheader ? 0 : this._dimensions.w
                menuleft = (this._offsets.left + menuleft + this._dimensions.subulw > jQuery(window).width()) ? (this.istopheader ? -this._dimensions.subulw + this._dimensions.w : -this._dimensions.w) + 12 : menuleft
                if ($targetul.queue().length <= 1) //if 1 or less queued animations
                    if (menuleft == 0) {
                        $targetul.css({
                            left: menuleft + "px",
                            width: this._dimensions.subulw + 'px'
                        }).removeClass("menu_flip")
                    }
                if (menuleft != 0 && this.istopheader) {
                    $targetul.css({
                        left: menuleft + "px",
                        width: this._dimensions.subulw + 'px'
                    }).addClass("menu_flip")
                }
                else {
                    $targetul.css({
                        left: menuleft + "px",
                        width: this._dimensions.subulw + 'px'
                    }).removeClass("menu_flip")
                }
                
                $curobj.hover(function(e){
                    var $targetul = jQuery(this).children("ul:eq(0)")
                    this._offsets = {
                        left: jQuery(this).offset().left,
                        top: jQuery(this).offset().top
                    }
                    var menuleft = this.istopheader ? 0 : this._dimensions.w
                    menuleft = (this._offsets.left + menuleft + this._dimensions.subulw > jQuery(window).width()) ? (this.istopheader ? -this._dimensions.subulw + this._dimensions.w : -this._dimensions.w) + 12 : menuleft
                    if ($targetul.queue().length <= 1) //if 1 or less queued animations
                        if (menuleft == 0) {
                            $targetul.css({
                                left: menuleft + "px",
                                width: this._dimensions.subulw + 'px'
                            }).removeClass("menu_flip").slideDown(jqueryslidemenu.animateduration.over)
                        }
                    if (menuleft != 0 && this.istopheader) {
                        $targetul.css({
                            left: menuleft + "px",
                            width: this._dimensions.subulw + 'px'
                        }).addClass("menu_flip").slideDown(jqueryslidemenu.animateduration.over)
                    }
                    else {
                        $targetul.css({
                            left: menuleft + "px",
                            width: this._dimensions.subulw + 'px'
                        }).removeClass("menu_flip").slideDown(jqueryslidemenu.animateduration.over)
                    }
                }, function(e){
                    var $targetul = jQuery(this).children("ul:eq(0)")
                    $targetul.slideUp(jqueryslidemenu.animateduration.out)
                }) //end hover
            }) //end $headers.each()
            $mainmenu.find("ul").css({
                display: 'none',
                visibility: 'visible'
            })
        }) //end document.ready
    }
}

// Making Slide Menu
jqueryslidemenu.buildmenu("nav", arrowimages)

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);
}

// START Initiating
jQuery(document).ready(function(){

    /***************************************************
     ZOOM PORTFOLIO HOVER - COLUMN
     ***************************************************/
    jQuery("#folio a, #entries a, #sidebar a").find("span.framed").hover(function(){
        jQuery(this).stop().fadeTo(500, 0.5);
    }, function(){
        jQuery(this).stop().fadeTo(500, 1);
    });
    
    /***************************************************
     JQUERY TOGGLE
     ***************************************************/
    jQuery(".toggle-body").hide();
    jQuery(".toggle-head").click(function(){
        var tb = jQuery(this).next(".toggle-body");
        
        if (tb.is(':hidden')) {
            tb.prev('.toggle-head').children('h3').addClass('minus');
            tb.slideDown('slow');
            
        }
        else {
            tb.slideUp(200, function(){
                 tb.prev('.toggle-head').children('h3').removeClass('minus');
            });
        }
    });
	
	
    /***************************************************
     TAB
     ***************************************************/	
	jQuery(".tabs").tabs("#tabbed-content > div");
	
    /***************************************************
     FADE ON HOVER
     ***************************************************/
    function fade(){
        jQuery('.fade').hover(function(){
            jQuery(this).stop().animate({
                opacity: 0.5
            }, 400);
        }, function(){
            jQuery(this).stop().animate({
                opacity: 1
            }, 400);
        });
        
    }
    
    function invers_fade(){
    
        jQuery('.ifade').css({
            'opacity': '0.5'
        });      
        
        jQuery('.ifade').hover(function(){
            jQuery(this).stop().animate({
                opacity: 1
            }, 400);
        }, function(){
            jQuery(this).stop().animate({
                opacity: 0.5
            }, 400);
        });
        
    }

	jQuery('.flickr_badge_image').addClass('fade');

	fade();
    invers_fade();
	
        
    
    /***************************************************
     Smooth Scrolling
	 http://github.com/kswedberg/jquery-smooth-scroll
     ***************************************************/
   function enable_smooth_scroll() {
    function filterPath(string) {
        return string;
                //.replace(/^\//,'')
               // .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                //.replace(/\/$/,'');
    }

    var locationPath = filterPath(location.pathname);
    
    var scrollElement = 'html, body';
    jQuery('html, body').each(function () {
        var initScrollTop = jQuery(this).attr('scrollTop');
        jQuery(this).attr('scrollTop', initScrollTop + 1);
        if (jQuery(this).attr('scrollTop') == initScrollTop + 1) {
            scrollElement = this.nodeName.toLowerCase();
            jQuery(this).attr('scrollTop', initScrollTop);
            return false;
        }    
    });
    
    jQuery('a[href*=#header]').each(function() {
        var thisPath = filterPath(this.pathname) || locationPath;
        if  (   locationPath == thisPath
                && (location.hostname == this.hostname || !this.hostname)
                && this.hash.replace(/#/, '')
            ) {
                if (jQuery(this.hash).length) {
                    jQuery(this).click(function(event) {
                        var targetOffset = jQuery(this.hash).offset().top;
                        var target = this.hash;
                        event.preventDefault();
                        jQuery(scrollElement).animate(
                            {scrollTop: targetOffset},
                            500,
                            function() {
                                location.hash = target;
                        });
                    });
                }
        }
    });
}

    enable_smooth_scroll();
    /***************************************************
     Contact Form
     ***************************************************/
    jQuery('form#contactform').submit(function(){
    
        var hasError = false;
        
		  var captcha = jQuery("#captcha").val();
        if (jQuery.trim(captcha) == "" ||  captcha != 17) {
            jQuery("#captcha").prev('label').addClass('error');
            jQuery("#captcha").focus();
            hasError = true;
        }
        else {
            jQuery("#captcha").prev('label').removeClass('error');
        }
		
		
		
		  var comment = jQuery("#form_message").val();
        if (jQuery.trim(comment) == "") {
            jQuery("#form_message").prev('label').addClass('error');
            jQuery("#form_message").focus();
            hasError = true;
        }
        else {
            jQuery("#form_message").prev('label').removeClass('error');
        }
		
		
		 var email = jQuery("input#email").val();
        if (jQuery.trim(email) == "" || !isValidEmailAddress(email)) {
            jQuery("input#email").prev('label').addClass('error');
            jQuery("input#email").focus();
            hasError = true;
        }
        else {
            jQuery("input#email").prev('label').removeClass('error');
        }
        
		
        var name = jQuery("input#fullname").val();
        if (jQuery.trim(name) == "") {
            jQuery("input#fullname").prev('label').addClass('error');
            jQuery("input#fullname").focus();
            hasError = true;
            
        }
        else {
            jQuery("input#fullname").prev('label').removeClass('error');
        }
        
        if (!hasError) {
            jQuery('form#contactform .ibutton').fadeOut('normal', function(){
                jQuery('.loading').css({
                    display: "block"
                });
                
            });
            
            $.post(jQuery("#contactform").attr('action'), jQuery("#contactform").serialize(), function(data){
                jQuery('.log').html(data);
                jQuery('.loading').remove();
                jQuery('#contactform').slideUp('slow');
            });
            
        }
        
        return false;
        
    });
    
    /***************************************************
     SlideShow
     ***************************************************/
	 function cyclePause() {
		jQuery('#slideshow').hover(
		function(){
			jQuery('.slides').cycle('pause')
		},
		function(){
			jQuery('.slides').cycle('resume')
		});
	}

	 cyclePause();
	 jQuery('.slides').cycle({
     fx: 'scrollRight' , // USE THE FOLLOWING EFFECTS: 'scrollRight', 'fade', 'scrollLeft', 'curtainX', 'scrollUp', 'scrollDown', 'wipe', 'blindX', 'blindY','uncover', 'shuffle'
     speed: 2000,
	 timeout: 6000,
	 pager: '.pagination',
	 pauseOnPagerHover: true,
     pagerAnchorBuilder: function(idx, slide){
            // return selector string for existing anchor 
            return '.pagination li:eq(' + idx + ') a';
        }
    });
    
    /***************************************************
     Misc
     ***************************************************/
    jQuery('.opacity').css('opacity', '0.8');
	jQuery('#footer #footer-pages li:last-child').css('border', 'none');
	jQuery('#entries .post:last').addClass('last');
       
    /***************************************************
     Twitter
     ***************************************************/	   
	 
	 var YourTwitterId = 'theHighThemes';
	 
		getTwitters('teaser-twitter', {
		id: YourTwitterId, 
		clearContents: false, // leave the original message in place
		count: 1, 
		withFriends: true,
		ignoreReplies: true,
		newwindow: true,
		template: '%text% <span class="twitterTime"><a href="http://twitter.com/%user_screen_name%/statuses/%id%">%time%</a></span> '
		});
   
		getTwitters('recent-tweets', {
		id: YourTwitterId,
		clearContents: false, // leave the original message in place
		count: 3,
		withFriends: true,
		ignoreReplies: true,
		newwindow: true,
		template: '<span class="twitterStatus">%text%</span> <span class="twitterTime"><a href="http://twitter.com/%user_screen_name%/statuses/%id%">%time%</a></span><hr />'
		});
}); // End Initiating


