/***
 Search Input Auto Clearing Functionality
***/

function searchclear() {
    var searchbar = jQuery('#header #search .input'),
        searchvalue = searchbar.val(); //caching elements
    searchbar.focus(function() {
        if(jQuery(this).val()==searchvalue){
            jQuery(this).val('');  //only clear value if value is the default value
        }   
    }).blur(function() {
        if(!jQuery(this).val()){
            jQuery(this).val(searchvalue); //return to original value when not focused on and empty
        }
    });
}

function initiatemenu() {
    var menuitem = jQuery('#header #menu li'); //caching elements
    menuitem.each(function(){
    	if($(this).children('ul')){
	        $(this).hover(function(){
	        	$(this).children('ul').slideDown();
	        },
	        function(){
	        	$(this).children('ul').slideUp('fast');
	        	var n = $(this).children('ul').queue("fx");
	        	if(n.length>2){
	        		$(this).children('ul').clearQueue();	
	        	}
	        });
    	}
    	if($('a', this).height()>=24){
    		$(this).addClass('long');	
    	}
    });
    menuitem.last().addClass('last');
    $('a', menuitem).hover(function(){
		$(this).css('color','#000');
		Cufon.replace($(this), { fontFamily: '67Med' });
	},function () {
		$(this).css('color','#BE2031').parent().children('ul li ul li a').css('color','#fff');
		Cufon.replace($(this), { fontFamily: '67Med' });
	});
}

function initiategallery() {
    var gallery = jQuery('#gallery'),
    	gallerycontrols = jQuery('#controls li'); //caching elements
	gallerycontrols.each(function(index){
		if(index==0){
			$(this).click(function(){
				$('.page.active',gallery).prev().addClass('active').animate({
				    width: 'show'
				  }).siblings().removeClass('active').animate({
				    width: 'hide'
				  });
				if(($('.page.active',gallery).index())==0){
					$(this).removeClass('active');
				}else if($('.page.active',gallery).index()>0){ 
					gallerycontrols.last().addClass('active');
				} 
				if($('.page.active',gallery).index()<$('.page',gallery).length){ 
					gallerycontrols.last().addClass('active');

				} 
			});	
		}
		if(index==2){
			if(($('.page.active',gallery).index()+1)!=$('.page',gallery).length){
					$(this).addClass('active');
				} 
			$(this).click(function(){
				if(($('.page.active',gallery).index()+1)<=$('.page',gallery).length){
					$('.page.active',gallery).next().addClass('active').show().siblings().removeClass('active').animate({
					    width: 'hide'
					  });
				}else{
					$('.page',gallery).eq(0).addClass('active').show().siblings().removeClass('active').animate({
					    width: 'hide'
					  });
				}
			  	if(($('.page.active',gallery).index()+1)!=$('.page',gallery).length){
					$(this).addClass('active');
				} 
				if(($('.page.active',gallery).index()+1)==$('.page',gallery).length){
					$(this).removeClass('active');
				}else if($('.page.active',gallery).index()>0){ 
					gallerycontrols.first().addClass('active');
				} 
			});	
		}
	});
	gallery.width(($('.page',gallery).length)*728);
	for(x=2;x<=$('.page',gallery).length;x++){
			gallerycontrols.eq(2).before('<li class="left">'+x+'</li>');
	}
	var gallerycontrolsnew = jQuery('#controls li:not(.arrow)');
	gallerycontrolsnew.each(function(index){
		$(this).click(function(){
						if(index>$('.page.active',gallery).eq()){
				$('.page:eq('+index+')',gallery).addClass('active').show().siblings().removeClass('active').animate({
					width: 'hide'
				});
			}
			
		});
	});
}

function initiateoverlaygallery() {
	var gallery = $('#gallery'),
		overlaygallery = $('#overlay'); //caching elements
	$('.page .item',gallery).each(function(index){
		$(this).click(function(){
			var data = {
					action: 'gallery_overlay',
					id: $(this).attr("class").split("-")[1] //page id for image that just got clicked.
				},
				jqxhr = $.post("/website042011/wp-admin/admin-ajax.php", data,
						function(response){
							overlaygallery.html(response).show().animate({
								right: '0px'
							}, 1500);
							overlaygalleryrotation();
						}).error(function() {
							   alert('error'); 
						});
			 
		});
	});
}

function contactemail() {
	var contact = $('#contact'),
		submitbutton = $('#contact .submit'),
		info = ''; //caching elements
	submitbutton.click(function(){
		if($('input',contact).val()){
			info = {
					action: 'contact_us_email',
					name: $('.name',contact).val(),
					email: $('.email',contact).val(),
					subject: $('.subject',contact).val(),
					message: $('.message',contact).val()
				};
			$.post("/website042011/wp-admin/admin-ajax.php", info,
				function(response){
					alert(response);
				}).error(function() {
					alert("error"); 
				});
		}else{
			$('input',contact).each(function(){
				if(!$(this).val()){
					info += $(this).attr('class')+', ';
				}
			});
			alert(info+'is blank');	
		}
		return false;
	});
	
}

function overlaygalleryrotation() {
	var gallery = $('#gallery'),
		overlaygallery = $('#overlay'),
		overlaysubcontrols = $('#overlay .sub-controls .item'),
		overlaycontrols = $('#overlay #overlay-gallery #overlay-controls li'),
		overlaymainimage = $('#overlay .main'); //caching elements
	$('.header img', overlaygallery).click(function(){
		overlaygallery.animate({
			right: '-846px'
		},function() {
    		overlaygallery.hide();
  		});
	});
	overlaysubcontrols.live('click', function() {
		$(this).addClass('active').siblings().removeClass('active');
		overlaymainimage.html($(this).html());
	});
	overlaycontrols.each(function(index){
		if(index==1){
			if((overlaysubcontrols.parent().find('.active').index()+1)<overlaysubcontrols.length){
				$(this).addClass('active');
			} 
			$(this).click(function(){
				overlaysubcontrols.parent().find('.active').next().addClass('active').siblings().removeClass('active');
				overlaymainimage.html(overlaysubcontrols.parent().find('.active').html());
				if((overlaysubcontrols.parent().find('.active').index()+1)==overlaysubcontrols.length){
					$(this).removeClass('active');
				} 
				if(overlaysubcontrols.parent().find('.active').index()>0){
					overlaycontrols.first().addClass('active');
				}
			});
		}else{
			$(this).click(function(){
				overlaysubcontrols.parent().find('.active').prev().addClass('active').siblings().removeClass('active');
				overlaymainimage.html(overlaysubcontrols.parent().find('.active').html());
				if(overlaysubcontrols.parent().find('.active').index()==0){
					$(this).removeClass('active');
				} 
				if((overlaysubcontrols.parent().find('.active').index()+1)<overlaysubcontrols.length){
					overlaycontrols.last().addClass('active');
				}
			});
		}
	});	
}

function initiaterotator() {
	var rotator = $('#rotator #navigation'),
		rotatorcontrols = $('#rotator #rotator-controls li'),
		current = ''
		animation = false; //caching elements
	rotator.width((($('li',rotator).length+1)*62)+658);
	$('li:eq(1)', rotator).addClass('active');
	rotatorcontrols.each(function(index){
		if(index==0){
            $(this).click(function(){
                if(!animation){
                    if((rotator.find('.active').index()+1)<$('.item',rotator).length){
                        current = rotator.find('.active').index();
                        //animation = true;
                        rotator.css({
                        left: "0px"
                    }).find('.active').removeClass('active').prev().addClass('active');
                        rotator.prepend($('li', rotator).eq(current+2));
                    }else{
                      $('.item',rotator).eq(0).addClass('active').siblings().removeClass('active');   
                    }
                }
            }); 
        }else {
            $(this).click(function(){
                if(!animation){
                    if((rotator.find('.active').index()+1)<$('.item',rotator).length){
                        current = rotator.find('.active').index();
                        //animation = true;
                        rotator.css({
                        left: "-62px"
                    }).find('.active').removeClass('active').next().addClass('active');
                        rotator.append($('li', rotator).eq(current-2));
                    }else{
                      $('.item',rotator).eq(0).addClass('active').siblings().removeClass('active');   
                    }
                }
            });
        }
	});
}

$(document).ready(function(){
	if($('video')){
		$('video').mediaelementplayer({
			features: ['playpause','progress','current','duration','volume','fullscreen']	
		});
		
	}
	
	Cufon.replace('#header #altmenu #alt-navigation li a', { fontFamily: '67Med' });
	Cufon.replace('#footer li a', { fontFamily: '55Roman' });
	Cufon.replace('#footer li span', { fontFamily: '55Roman' });
	Cufon.replace('h2', { fontFamily: '67Med' });
	Cufon.replace('#menu li a', { fontFamily: '67Med' });
	Cufon.replace('#header #menu #subnav li a', { fontFamily: '77Bold' });
	searchclear();
	initiatemenu();
	initiategallery();
	initiateoverlaygallery();
	initiaterotator();
	contactemail();
	$('#content.video #media').hide();
	$('#content.video .video-play').click(function(){
		$('#mep_0 .mejs-inner .mejs-controls .mejs-time-rail').width($('#mep_0 .mejs-inner .mejs-controls .mejs-time-rail .mejs-time-total').width()+10);
		$(this).hide().siblings('#text').hide();
		$('#content.video #media').show();
	});
	$('#content.video .video-close').click(function(){
		$(this).parent('#media').hide();
		$('#content.video #text').show().siblings('.video-play').show();
	});
});


