$(document).ready(function(){


	// run these functions after loading
	function init(){
		$("#home .content").fadeIn("slow");
		$(".player").fadeIn("fast");
	
	}


	//back to home page
	$('#logo').bind("click",
		function(){
    		$('#nav li:first').click();
	});


	//promo link - sits at bottom of playlist
	$('#playlist .promo').click(
		function(){
			window.open('http://www.publicassemblynyc.com/events/view/1130');
	});



	/*email form*/            
	$('#email').focus(function(){
       if ($(this).val() == 'join mailing list') {
           $(this).val('');
       }
    }).blur(function(){
	       if ($(this).val() == '') {
	           $(this).val('join mailing list');
	       }
    });


	 
	/* music player */	
	
   $("#jquery_jplayer").jPlayer({
		ready: function () {
			$("#trackname").html($("#track-01").html());
			$(this).setFile('/i/music_player/01_changes.mp3').play();
			//demoInstanceInfo($(this), $("#jplayer_info"));
		}
	})
	.jPlayerId("play", "play")
	.jPlayerId("pause", "pause")
	.jPlayerId("stop", "stop")
	.jPlayerId("volumeMin", "vmute")
	.jPlayerId("volumeMax", "vmax")
	.jPlayerId("volumeBar", "vhalf")
	.onProgressChange( function(lp,ppr,ppa,pt,tt) {
 		$("#pcent").text(parseInt(ppa)+"%");
	});


	/* expand playlist events */
	$(".expand_list").toggle(
    	function(){
    		$(this).html('<img src="i/controls/b_expand.png" alt="b_expand" width="15" height="16"/>');
    		$("#playlist").show().addClass('open');
    	},
		function () {
			$(this).html('<img src="i/controls/b_expand_off.png" alt="b_expand" width="15" height="16"/>');
    		$("#playlist").hide().removeClass('open')
		});
	
	/* hover function 
	$(".expand_list").hover(
		function(){
			$(this).html('<img src="i/controls/b_expand_hover.png" alt="b_expand" width="15" height="16"/>');
			console.log('bind hover on being called');
		},
		function (){
			$(this).html('<img src="i/controls/b_expand_off.png" alt="b_expand" width="15" height="16"/>');
			console.log('bind hover off being called');
		});  //end expand
	*/
	



	//set array of tracks
	
	/*
	var tracks = $('#playlist li').length;
	
	alert(tracks);
	
	function playtrack(){
		$("#track-01").click(function() {
	
			$("#trackname").html($(this).html());
			$("#jquery_jplayer").setFile('/i/music_player/01_sticks_and_stones.mp3').play();
			return false;
		});
	}
	
	
	
	$(document.body).click(function () {
      $("div").each(function (i) {
        if (this.style.color != "blue") {
          this.style.color = "blue";
        } else {
          this.style.color = "";
        }
      });
    });	
	*/
	
	var a_playlist = {
		track1: '01_sticks_and_stones.mp3',
		track2: '02_king.mp3',
		track3: '03_rotgut.mp3',
		track4: '04_96_degrees.mp3',
		track5: '05_miracles.mp3',
		track6: '06_now_and_then.mp3',
		track7: '07_feel_the_funk.mp3',
		track8: '08_what_it_is.mp3',
		track9: '09_summertime.mp3'
		}


	//tracks
	$("#track-01").click(function() {

		$("#trackname").html($(this).html());
		$("#jquery_jplayer").setFile('/i/music_player/promo/01_changes.mp3').play();
		return false;
	});

	$("#track-02").click(function() {

		$("#trackname").html($(this).html());
		$("#jquery_jplayer").setFile('/i/music_player/promo/02_on_and_on.mp3').play();
		return false;
	});

	$("#track-03").click(function() {

		$("#trackname").html($(this).html());
		$("#jquery_jplayer").setFile('/i/music_player/promo/03_now_and_then.mp3').play();
		return false;
	});
	

	
	//if user clicks on youtube pause player
	$(".yt_player").click(function(){
		$("#jquery_jplayer").pause();
	});



	      


	/* 
		scroller effect 
	
	*/

	var $panels = $('#container #scroll #background > div');
	var $container = $('#container #background');
	
	//console.log($panels);
	
	var horizontal = true;
	
	if(horizontal){
		$panels.css({
			'float' : 'left',
			'position' : 'relative'  //ie fix to ensure overflow is hidden
		});
		
		//calculate a new width for the container (so it holds all panels)
		$container.css('width',$panels[0].offsetWidth * $panels.length);
		
		//console.log($container.width());
	}

	// collect the scroll object, at the same time apply the hidden overflow
	// to remove the default scrollbars that will appear
	var $scroll = $('#wrapper #scroll').css('overflow', 'hidden');

	
	//handle nav selection
	function selectNav(){
		$(this)
			.parents('ul')
				.find('li')
					.removeClass('selected')
				.end()
			.end()
			.addClass('selected');
	
	}
	
	$('#nav').find('li').click(selectNav);
	
	//call before scroll
	function before_scroll_activates(){
		$('.content').fadeOut('slow');
	}
	
	
	// go find the navigation link that has this target and select the nav
	function trigger(data) {
		/*	console.log('data: ' + data.id); */
		var el = data.id;
				
		//selectNav.call(el);
				
		//fade in content
		$("#" + data.id + " .content").fadeIn('slow');
	  
	}
	
	if (window.location.hash) {
	  trigger({ id : window.location.hash.substr(1) });
	  //console.log(trigger({ id : window.location.hash.substr(1) }))
	} else {
		//change so it loads 'home' screen		
		$('#nav li:first').click();
	}


	// offset is used to move to *exactly* the right place, since I'm using
	// padding on my example, I need to subtract the amount of padding to
	// the offset.  Try removing this to get a good idea of the effect
	var offset = parseInt((horizontal ? $container.css('paddingTop') : $container.css('paddingLeft')) || 0) * -1;
	

	var scrollOptions = {
	  target: $scroll, // the element that has the overflow
	  items: $panels,
	  navigation: '#nav li',
	  axis: 'xy',
	  onBefore: before_scroll_activates,
	  onAfter: trigger, // our final callback	  
	  offset: offset,	  
	  duration: 500,	  
	  easing: 'swing'
	};
	
	// apply serialScroll to the slider - we chose this plugin because it 
	// supports// the indexed next and previous scroll along with hooking 
	// in to our navigation.
	$('#container').serialScroll(scrollOptions);
	
	// now apply localScroll to hook any other arbitrary links to trigger 
	// the effect
	$.localScroll(scrollOptions);
	
	// finally, if the URL has a hash, move the slider in to position, 
	// setting the duration to 1 because I don't want it to scroll in the
	// very first page load.  We don't always need this, but it ensures
	// the positioning is absolutely spot on when the pages loads.
	scrollOptions.duration = 6;
	$.localScroll.hash(scrollOptions);






	/* share links */
	
		shareArticle = function(network){
				
			function share_popup(url, name, params) {
				var win = window.open(url, name, params);
			}		
	
			var openers = {
			
				facebook : function() {
						share_popup('http://www.facebook.com/pages/Conchita-Campos/84136596102', 'facebook', 'toolbar=0,status=0,height=600,width=800,scrollbars=yes,resizable=yes');
				},
				
				/*
				digg : function () {
						share_popup('http://digg.com/remote-submit?phase=2&url=' + URL + '&title=' + title + '&bodytext=' + description, 'digg', 'toolbar=0,status=0,height=450,width=650,scrollbars=yes,resizable=yes');
				},
				
				delicious : function () {
						share_popup('http://del.icio.us/post?v=4&partner=CBSsports&noui&jump=close&url=' + URL + '&title=' + title + '&bodytext=' + description, 'delicious', 'toolbar=0,status=0,height=400,width=700,scrollbars=yes,resizable=no');
				},
				*/
				
				reverbnation: function(){
					share_popup('http://www.reverbnation.com/conchitacampos', 'reverbnation', 'toolbar=0,status=0,height=600,width=800,scrollbars=yes,resizable=yes');
				},
	
				myspace: function () {
					share_popup('http://www.myspace.com/conchitacampos', 'myspace', 'toolbar=0,status=0,height=800,width=600,scrollbars=yes,resizable=yes');
				},
				
				youtube: function(){
					share_popup('http://www.youtube.com/user/ConchitaCampos','youtube','toolbar=0,status=0,height=800,width=600,scrollbars=yes,resizable=yes');
				},
	
				yahoobuzz : function () {
						share_popup( 'http://buzz.yahoo.com/buzz?publisherurn=CBSsports&targetUrl=' + URL, 'yahoobuzz', 'scrollbars=yes,resizable=yes');
				},
		
				mixx : function () {
						share_popup( 
						   'http://mini.mixx.com/submit/story'
						   + '?page_url='    + URL
						   + '&title='       + title
						   + '&partner='     + 'CBSsports'
						   ,
						   'mixx',
						   'toolbar=0,status=0,height=550,width=700,scrollbars=yes,resizable=no'
						);
				},
		
				linkedin : function () {
					share_popup(
					 'http://www.linkedin.com/shareArticle?mini=true'
					   + '&url='         + URL
					   + '&title='       + title
					   + '&summary='     + description
					   + '&source='      + 'CBS Sports'
					   ,
					   'Linkedin',
					   'toolbar=0,status=0,height=550,width=700,scrollbars=yes,resizable=no'
					);
				},
				
				reddit : function(){
					share_popup('http://www.reddit.com/submit?url=' + URL,'reddit','scrollbars=yes,resizable=yes');
				
				},
				
				googlebookmark : function(){
					share_popup('http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk=' + URL + '&title=' + title,
					'googlebookmarks',
					'toolbar=0,status=0,height=420,width=550,scrollbars=yes,resizable=no'
					)
				},
				
				stumbleupon : function(){
					share_popup('http://www.stumbleupon.com/submit?url=' + URL,
					'stumbleupon',
					'toolbar=0,status=0,height=420,width=550,scrollbars=yes,resizable=no'
					)
				},
				
				twitter : function(){
					share_popup('http://twitter.com/concheets');
				}
			
			}
		
			openers[network]();
		
		}		
	

		/* use this to help with share button */
		jQuery("#social li").click(function(){		

			//pass var to appropriate function based on li attribute name
			shareArticle(jQuery(this).attr('name'));
									

		});
		//end share article	
		
		


		//Form
	    var formOptions = { 
	        target:        	'#thankyou',   // target element(s) to be updated with server response 
	        beforeSubmit:  	showRequest,  // pre-submit callback 
	        success:       	showResponse,  // post-submit callback 
		    url:			'/inc/mailinglist.php',         // override for form's 'action' attribute 
	        type:      		'post',        // '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 form using 'ajaxForm' 
	    $('#mailing_list').ajaxForm(formOptions); 
		
		

		// pre-submit callback 
		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); 
		 
		    // 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)  { 
		    // for normal html responses, the first argument to the success callback 
		    // is the XMLHttpRequest object's responseText property 
		 
		    // if the ajaxForm 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 ajaxForm 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.'); 

			$('#thankyou').show();
		   //$('#mailing_list').hide();
		   
		} 		
		
		
		
		
		
		
		
		
		
		//call init
		init();

});
            
