var au_curperson;
var au_people;
$(function() {
	$.ajaxSetup({cache:false});	   
	$( ".draggable" ).draggable();
	$( ".accordion" ).accordion();

	$(".fadeLink").hover(
					
					function(){
							if(!$(this).hasClass('current')){
								$(this).animate({ color:'#d40000' }, 300);
							}
						},
					function(){
							if(!$(this).hasClass('current')){
								$(this).animate({ color:'#000000' }, 300);
							}
						}
			);
	var slides=new Array();
	slides=$(".slidePanelCanvas .slidePanelslide");
	var curslide=0;
	if(slides.length>1){
		parWidth=$(slides[0]).parent().width();
		for(x=0;x<slides.length;x++){
			//alert(Math.floor(x));
			$(slides[x]).css('position','absolute');
			$(slides[x]).css('left',function(){return Math.floor(x*parWidth);});	
		}
		$(".slidePanel").prepend('<a href="#" class="pav_prevLink"></a><a href="#" class="pav_nextLink"></a>');
		$(".slidePanel .pav_prevLink").add(".slidePanel .pav_nextLink").css("top",function(){return ($(this).parent().height()/2)- ($(this).height()/2)});
		$(".slidePanelCanvas").scrollTo(slides[curslide]);
		
		$(".slidePanel .pav_prevLink").click(
			function(event) {
				event.preventDefault();	
				curslide--;
				if(curslide>=slides.length){
					curslide=0;
				}
				if(curslide<0){
					curslide=slides.length-1;
				}
				$(this).parent().children(".slidePanelCanvas").scrollTo(
						slides[curslide],
						250,
						{axis:'xy',easing:'swing'}
						);
			});
		$(".slidePanel .pav_prevLink").hover(
						  	function(){
								clearInterval(slideshowInt);
								
							},
							function(){
								slideshowInt=setInterval(
									function(){
										$(".slidePanel .pav_nextLink").trigger('click');
									},
									speed
								);	
							}
					  );
		$(".slidePanel .pav_nextLink").click(
			function(event) {
				event.preventDefault();	
				curslide++;
				if(curslide>=slides.length){
					curslide=0;
				}
				if(curslide<0){
					curslide=slides.length-1;
				}
				$(this).parent().children(".slidePanelCanvas").scrollTo(
						slides[curslide],
						250,
						{axis:'xy',easing:'swing'}
						);
			});
		$(".slidePanel .pav_nextLink").hover(
						  	function(){
								clearInterval(slideshowInt);
								
							},
							function(){
								slideshowInt=setInterval(
									function(){
										$(".slidePanel .pav_nextLink").trigger('click');
									},
									speed
								);	
							}
					  );
		
		
		
		$(".slidePanelCanvas").click(
						
						function(event) {
								if($(slides[curslide]).attr('rel')==''){//PREVENT ROTATE ON MOVIES UNLESS CLICK ON ARROWS...
									event.preventDefault();	 
									var offset=$(this).offset();
									
									if(event.pageX-offset.left>$(this).width()/2){
										curslide++;
										
									}
									else{
										curslide--;
									}
									if(curslide>=slides.length){
											curslide=0;
										}
										if(curslide<0){
											curslide=slides.length-1;
										}
		
									$(this).scrollTo(
											slides[curslide],
											250,
											{
												axis:'xy',
												easing:'swing'
											}
										);
								}
								else{
									if($(slides[curslide]).attr('rel')!='mov'){
										var rel=$(slides[curslide]).attr('rel').toUpperCase();
										if(rel.indexOf('WWW.PAVONE.NET')>-1||rel.indexOf('/PAVONE.NET')>-1||rel.indexOf('THINKITSO.COM')>-1){
											//alert(rel);
											if(rel.indexOf('.MP3')>-1||rel.indexOf('.MOV')>-1||rel.indexOf('.FLV')>-1||rel.indexOf('.WMV')>-1||rel.indexOf('.AVI')>-1||rel.indexOf('.MPG')>-1){
												//if Audio or Video file on our sites:
												openVideo($(slides[curslide]).attr('rel'));
											}
											else{
												location.href=$(slides[curslide]).attr('rel');
											}
										}
										else{
											if(rel.indexOf('YOUTUBE.COM/WATCH?V=')>-1){
												//if youtube video:
												openVideo($(slides[curslide]).attr('rel'));
											}
											else{
												window.open($(slides[curslide]).attr('rel'));
											}
										}
									}
								}
							}
				);
		
		
		var speed=10000; //slideshow speed in milliseconds.
		var slideshowInt=setInterval(
					function(){
						$(".slidePanel .pav_nextLink").trigger('click');
					},
					speed
					);
		$(".slidePanelCanvas").hover(
						  	function(){
								clearInterval(slideshowInt);
								
							},
							function(){
								slideshowInt=setInterval(
									function(){
										$(".slidePanel .pav_nextLink").trigger('click');
									},
									speed
								);	
							}
					  );
		
	}
	//end if(slides.length>1){
	//SETUP MODAL:	
	$('<div id="dialog-modal"><div id="dialog-modalContent"></div></div>').appendTo('body');
	//$(body).add("<p id='new'>new paragraph</p>");
	$("a.modal").click(
				function(event) {
						event.preventDefault();	 
						openAjaxPage($(this).attr('href')+"?mw=1");
					}
		);
	
	$("a.lightbox").click(
				function(event) {
						event.preventDefault();	 
						lightboxLoad($(this).attr('href'));
					}
		);
	//end MODAL CALL
	
	
	//ABOUT US PICS:	
	/*Requirements:  Source page needs
		<div class="au_people_data hide">
			<span class="au_person">
				<img src='http://newpavone.thinkitso.com/wp-content/themes/twentyten/images/image1.jpg'/>
				<h4 class="au_name">Amy Beamer</h4>
				<h4 class="au_title">CFO</h4>
				<p class="au_description">
					Some text that is assoicated with Amy.
				</p>
			</span>	
			...
		</div>
	
	*Explanation:  .aboutuspics images get stored in an array, then it is replaced by a set of images below.
					Every [speed] milliseconds a random one of those images is replaced by a random image from the original set.
	*/
		var maxPeeps=8; //Number of people to show at once.
		var au_speed=2000; //milliseconds to replace.
		
		au_curperson=maxPeeps;
		
		au_people=$(".au_people_data .au_person");
		
		
		for(i=0;i<maxPeeps;i++){			
			newPerson="<a href='#' class='au_headshot_"+i+" au_headshot' accesskey="+i+" rev='"+i+"' id='au_headshot_"+i+"'>";
			//accesskey= 0 to (maxPeeps-1) for reference position of text box. 
			//rev= 0-(au_people.length()) for reference of which au_people is in display.
			newPerson+="<img src='"+$(au_people[i]).children("img").attr("src")+"'/>";
			newPerson+="</a>";
			$(".aboutuspics").append(newPerson);
		
		}
		var au_int_replaceimg=setInterval(au_swap,au_speed);
		
		$(".au_headshot").click(function(event){
									event.preventDefault();	
									
								});
		$(".au_headshot").hover(function(){
								//on over:	
									clearInterval(au_int_replaceimg);
									ind=$(this).attr("rev");
									$("#au_tooltip .au_tt_name").html(
																$(au_people[ind]).children(".au_name").html()
																);
									$("#au_tooltip .au_tt_title").html(
																$(au_people[ind]).children(".au_title").html()
																);
									$("#au_tooltip .au_tt_description").html(
																$(au_people[ind]).children(".au_description").html()
																);
									$("#au_tooltip").toggleClass("au_tooltip_"+$(this).attr("accesskey"));
									$("#au_tooltip").show();
									
								},
								function(){
								//on out:	
									au_int_replaceimg=setInterval(au_swap,au_speed);
									$("#au_tooltip").toggleClass("au_tooltip_"+$(this).attr("accesskey"));
									$("#au_tooltip").hide();
								});
		

	//END ABOUT US PICS
	//HW:
	/**//*
	var hwtimer;
	$('.hollywood').hover(
						  function(){
								hwtimer=setTimeout(function(){openVideo('http://www.youtube.com/watch_popup?v=CQzUsTFqtW0&vq=medium#t=69')},8000);
								   },
							function(){
								clearTimeout(hwtimer);
								
							}
						  );
	
		*/
	//END HW
	//EXPERIENCE PAGE:
		$('.experienceThumbImg').append(function(){return "<div class='expThumbImgText' style=''><div>"+$(this).children('img').attr('alt')+"<br/><span class='clicktext'>READ THE CASE STUDY</span></div></div>";});
		
		$('.experienceThumbImg').hover(
			function(){
				$(this).children('.expThumbImgText').fadeTo(200, 0.8);
			},
			function(){
				$(this).children('.expThumbImgText').fadeTo(200, 0.0);
			}
		)	
		$('.experienceThumbImg').children('.expThumbImgText').fadeTo(200, 0.0);
	//END EXPERIENCE PAGE
	
});
function hwtimercall(){
		
}

function openAjaxPage(ajaxLink){
	$("#dialog-modalContent").load(
								   ajaxLink,
								   function(){
										FB.XFBML.parse(document.getElementById("dialog-modalContent"));   
								   }
							); 
	$("#dialog-modal").dialog({
		height: $(window).height()-100,
		width: $(window).width()-100,
		modal: true,
		resizable: true,
		show: 'clip',
		hide: 'clip',
		position:'center'
	});
}
function lightboxLoad(imgLink){
	$('#dialog-modalContent').replaceWith('<div id="dialog-modalContent"><p><img src="'+imgLink+'"/></p></div>');
	$("#dialog-modal").dialog({
		height: $(window).height()-100,
		width: $(window).width()-100,
		modal: true,
		resizable: false,
		show: 'clip',
		hide: 'clip'
	});
}

function dialogClose(){
	$("#dialog-modal").dialog("close");
}
function openVideo(vid){
	
	$('#dialog-modalContent').load(
		   "/wp-content/plugins/pav_homeslider/videoPlayer.php?vi="+vid,
		   function(){
				$("#dialog-modal").dialog({
					width:702,
					height:420,
					modal: true,
					resizable: false,
					show: 'clip',
					hide: 'clip'
				});  
		   });

}
function au_swap(){
	//alert("yo");
	var auhs=$(".au_headshot");
	//alert(Math.floor(Math.random()*auhs.length));
	updImg=auhs[Math.floor(Math.random()*auhs.length)];
	//alert(updImg);
	$(updImg).fadeTo(300,0, function(){
									$(updImg).children("img").attr('src',$(au_people[au_curperson]).children("img").attr('src'));
									$(updImg).children("img").attr('alt',$(au_people[au_curperson]).children("img").attr('alt'));
									$(updImg).attr('rev',au_curperson);
									 }).fadeTo(300,1);
	
	au_curperson++;
	if(au_curperson>=au_people.length){
		au_curperson=0;	
	}
	//Math.random(auhs.length())
		
}

function slideAdvance(){
	
}
