/*** onready.js ****************************************************************
 *                                                                             *
 *	website: lp33.tv                                                           *
 *	author: matt antone                                                        *
 *	email: mattantone@lp33.tv                                                  *
 *  jQuery file and version: jquery-1.2.6.min.js                               *
 *  jQuery UI file and version: jquery-ui-personalized-1.6rc2.packed.js        *
 *                                                                             *
 *	This file should only be used to execute javascript functions for lp33.tv  *
 *	This file gets executed as if it were the "onload" in a body tag           *
 *                                                                             *
 *******************************************************************************/
 
$(document).ready(function(){
	initDialog();
	applyContentBehaviors();
	applyFormBehaviors();
	applyBehaviors();
 });

/**
 * Fucntion: applyContentBehaviors()
 * Description: Applies behaviors to all content. Used on page load and after content has loaded via AJAX
 **/
 function applyContentBehaviors(){
	// init play song behavior on all elements with a class of .play on all pages
	$('.play').unbind().initPlaySongBehavior();

 	if($('#PhotoList a')[0]){
 		$(function() {
        	$('#PhotoList a').lightBox({
        		fixedNavigation: true,
        		imageBtnClose: '/themes/lp33_beta/Assets/Images/lightbox-btn-close.gif',
        		imageBtnPrev: '/themes/lp33_beta/Assets/Images/lightbox-btn-prev.gif',
        		imageBtnNext: '/themes/lp33_beta/Assets/Images/lightbox-btn-next.gif'
        	});
 		});
 	}

	// Setup lightbox behaviors
	if($('.profile_artist_photos')[0] || $('.profile_photos')[0]){
 	    $('#PhotoList a').lightBox();
 	}

	$('#List-Top-5').initAccordion();
	$('#Marketing-Messages').initAnimateList();
	$('#Featured-Artist-Block').initAnimateList();
	
	
 	// Setup Directory Buttons
 	if($('#search-alpha a')[0]){
 		$('#search-alpha a').unbind().click(function(){
 			$('#search-alpha li').removeClass('selected');
 			$(this).parents('li:first').addClass('selected');
 			this.callback = function(){
 				applyContentBehaviors();
 			}
 			$('#Directory .results').getContent($(this).attr('href')+'&ajax=true',$.context(this).callback('callback'));
 			return false;
 		});
 	}
 	
 	//Pagination on the search results page
 	if($('#WholePage .pagination a')[0]){
 		$('#WholePage .pagination a').unbind().click(function(){
 			this.callback = function(){
					applyBehaviors();
					applyContentBehaviors();
 			}
 			$('#WholePage .results').getContent($(this).attr('href')+'&ajax=true',$.context(this).callback('callback'));
 			return false;
 		});
		$('#WholePage .page-list a').unbind().click(function(){
 			this.callback = function(){
					applyBehaviors();
					applyContentBehaviors();
 			}
 			$('#WholePage').getContent($(this).attr('href')+'&ajax=true',$.context(this).callback('callback'));
 			return false;
 		});	 		
		
 	}

 	//Pagination for featured artist secondary
 	if($('#Featured-Artist-Block-Secondary .pagination a')[0]){
 		$('#Featured-Artist-Block-Secondary .pagination a').unbind().click(function(){
 			this.callback = function(){
					applyBehaviors();
					applyContentBehaviors();
 			}
 			$('#Featured-Artist-Block-Secondary').getContent($(this).attr('href')+'&ajax=true',$.context(this).callback('callback'));
 			return false;
 		});	 		
 	}

	$('.search-results ul.results>li').hover(function(){
			var curLi = this;
			$('.more-info',curLi).show();
		},
		function(){
			$('.more-info',this).hide();
	});

	$('.close').unbind().click(function(){
		$(this).parents('.more-info').hide();
		return false;
	});
	
	$('.results .listen').unbind().click(function(){
		$.playSong($(this).attr('id'));
		return false;
	});
	
	$('.featured-form .channel .checkbox').unbind().click(function(){
		if($(this).attr('checked')){
			$(this).siblings('.position').show();	
		}else{
			$(this).siblings('.position').children('select').children('option:first').attr('selected','true');
			$(this).siblings('.position').hide();	
		}

	});
	
	$('.menu_44,.menu_43,.menu_42,').click(function(){
		document.location = '/index.php?page=content&pid=41';
		return false;
	});
	
	$('.menu_45').click(function(){
		document.location = '/corporate';
		return false;
	});
 	
 	if($('#Page-Menu a')[0] || $('.make_ajax_links a')[0]){
 			$('#Page-Menu a,.make_ajax_links a').unbind().click(function(){
	 			//test to see if we are outside the menu
	 			if(!$(this).parents('ul#Page-Menu:eq(0)')[0]){
	 				//we're not in the menu
	 				var menuItem = $('#Page-Menu li.'+$(this).attr('class')).attr('class');
	 				$('#Page-Menu').children('li').removeClass('selected').addClass('deselected');
		 			$('#Page-Menu li.'+menuItem).removeClass('deselected').addClass('selected');
	 			}else{
	 				//we're in the menu
		 			$(this).parents('ul:eq(0)').children('li').removeClass('selected').addClass('deselected');
		 			$(this).parents('li:first').removeClass('deselected').addClass('selected');
	 			}
	 			$('#WholePage').getContent($(this).attr('href'));
	 			return false;
	 		});
 	}
 	
 	if($('#errorLine')[0]){
 		$('#errorLine').displayError();	
 	}
 	// init the form wizard "stop" button
	$('form.wizard .button:eq(0)').click(function(){
		document.location = '/index.php?page=my_handshakes';
		return false;
	});
 	
 	
}

function applyFormBehaviors(){
 	if($('#Contact-Form')[0]){
 		$('#Contact-Form').unbind().submit(function(){
 			$(this).ajaxSubmit({
				type: 'POST',
				data: {ajax: 'true'},
				dataType: "text/xml",
				success: function(responseXML){
					if($(responseXML)){
						$('#Contact-Form').before('<span class="complete" style="padding: 4px;margin: 10px 0px;display: block;">'+responseXML+'</span><a href="#" onclick="$(this).remove();$(\'.error\').remove();$(\'#Contact-Form\').show()"><br/>Send Another</a>');
						$('#Contact-Form').hide();
					}
					return false;
				},
				failure: function(xml){
					if($('.systemMessage',xml).html()[0]){
						$('#Contact-Form').before('<span class="error"><button onclick="$(\'#Dialog\').dialog(\'close\')">Close Dialog</button>AJAX Error</span>');
						return false;
					}
				},
				complete: function( data, status ) { 
					applyBehaviors();
				}
 			});
 			return false;
 		});
 	}
 	if($('select[name=country]')[0]){
 		$('select[name=country]').attr('onchange','').unbind().change(function(){
 			if($(this).val()>0){
	 			$.ajax({
					type: 'POST',
	 				url: 'index.php',
	 				beforeSend: function(){
	 					if($('select[name=state]')[0]){
	 						$('select[name=state]').empty().append('<option>Loading...</option>');	
	 					}
	 				
	 				},
	 				data:{
	 					handler: 'geo',
	 					action: 'states',
	 					allow_empty: 0,
	 					country: $(this).val()
	 				},
	 				dataType: 'xml',
	 				success: function(responseXML){
	 					var options = '<option>-- Pick One --</option>';
	 					$('states state',responseXML).each(function(){
	 						options = options+'<option value="'+$('id',this).text()+'">'+$('name',this).text()+'</option>';
	 					});
	 					if($('select[name=state]')[0]){
	 						$('select[name=state]').empty().append(options);	
	 					}
	 				}
	 			});
 			}
 			return false;
 		});
 	}
 	
 	if($('select[name=state]')[0]){
 		$('select[name=state]').attr('onchange','').unbind().change(function(){
 			if($(this).val()>0){
	 			$.ajax({
					type: 'POST',
	 				url: 'index.php',
	 				data:{
	 					handler: 'geo',
	 					action: 'cities',
	 					allow_empty: 0,
	 					country: $('select[name=country]').val(),
	 					state: $(this).val()
	 				},
	 				dataType: 'xml',
	 				success: function(responseXML){
	 					var options = '<option>-- Pick One --</option>';
	 					$('cities city',responseXML).each(function(){
	 						options = options+'<option value="'+$('id',this).text()+'">'+$('name',this).text()+'</option>';
	 					});
	 					if($('select[name=city]')[0]){
	 						$('select[name=city]').empty().append(options);	
	 					}
	 				}
	 			});
 			}
 			return false;
 		});
 	}

}


function applyBehaviors(){
	IEbrowser = false;
	//if IE
	if(!jQuery.support.leadingWhitespace){
		IEbrowser = true;
		version = jQuery.browser.version;
		if($('body.artist_home')[0] && version < 7){
			$('#Dialog').empty().append('<div style="padding: 10px !important;margin: 10px !important;"><h2>Alert</h2><p>You are currently using Internet Explorer 6.0. For the best experience at LP33, we suggest upgrading your browser.  We recommend the latest version of <a href="http://www.mozilla.com/en-US/firefox/" target="_blank">Firefox</a>.</p><button onclick="$(\'#Dialog\').dialog(\'close\')">Close</button></div>').dialog('open');
		}
	}else{
		version = jQuery.browser.version;
	}
	// For dimming of the screen
	$("div#dim_screen").unbind().click(function() { document.awolplayer.undimStageJS(); scaleScreenDown() });	
	$(window).bind('resize', function() { newDims() });

	//Make the quick profile search clear and populate
	$('.form-text').focus(function(){$(this).attr('value','');});

	/**************************************
	 * Page & Content Specific javascript *
	 **************************************/
	 // Pages are defined by the ID in the body tag
	 // To add page specific javascript test what page you are on or for a specific element, $('#bodyidtag')[0] where bodyidtag is the page.
	 	
	 	// FEATURED ARTISTS	 	
	 	if($('body.artist_home')[0] || $('body.artist_home_rock')[0] || $('body.artist_home_pop')[0] || $('body.artist_home_fuk')[0] || $('body.artist_home_americana')[0] || $('body.artist_home_electronic')[0] || $('body.artist_home_sxsw')[0] || $('body.artist_home_underground')[0]){
			$.initChannelButtons();
	 	}
	 		 	
	 	if($('#Artist-Menu li a')[0] || $('#Profile-Menu li a')[0] || $('.addfriend')[0] || $('.sendmessage')[0] || $('.faveem')[0] || $('.addcomment')[0]){
			var language					= new Object();
			language.fave					= new Object();
			language.fave.pos				= new Array();
			language.fave.pos['menutext']	= "Unfave 'em";
			language.fave.pos['message']	= "This member has been successfully added to your faves.";
			language.fave.neg				= new Object();
			language.fave.neg['menutext']	= "Fave 'em";
			language.fave.neg['message']	= "This member has been successfully removed from your faves.";
	
			language.block					= new Object();
			language.block.pos				= new Object();
			language.block.pos['menutext']	= "Unblock 'em";
			language.block.pos['message']	= 'This member has been successfully blocked from your account.';
			language.block.neg				= new Object();
			language.block.neg['menutext']	= "Block 'em";
			language.block.neg['message']	= 'This member has been successfully unblocked from your account.';
			
			language.friend					= new Object();
			language.friend.pos				= new Object();
			language.friend.pos['menutext']	= "Add to Friends";
			language.friend.pos['message']	= 'A friend request has been sent to this member.';
			language.friend.neg				= new Object();
			language.friend.neg['menutext']	= "Add to friends";
			language.friend.neg['message']	= 'This member has been successfully removed from your friends.';

			$('#Artist-Menu li a,#Profile-Menu li a,.addfriend,.sendmessage,.faveem,.addcomment').unbind().click(function(){
		 		//$('#Content .systemMessage').remove();
		 		var curAnchor = this;
		 		var message = $(this).attr('message');
		 		var form = $(this).attr('form');
		 		var altText = $(this).attr('altText');
		 		$.ajax({
					type: "GET",
					url: $(this).attr('href')+"&ajax=true",
					//dataType: "xml",
					timeout: 20000, 
					success: function(xml){		 		
		 				if(form){
							$('#Dialog').empty().append($('h2',xml)).append($('.form',xml).html()+'<br style="clear"/>').dialog('open');
							if($('#Dialog form')[0]){
								$('#Dialog form').append('<input type="hidden" name="ajax" value="true"/>');
								$('#Dialog form').bind('submit',function(){
									$(this).ajaxSubmit({
									type: 'GET',
									dataType: "html",
									success: function(data){
										if($('#error',data)[0]){
											$('#Dialog #errorLine').remove();
											$('#Dialog').prepend('<div id="errorLine">'+$('#error',data).parents('div:eq(0)').html()+'</div>');
											if($('#Dialog .complete')[0]){
												$('#Dialog #errorLine').dialogTimer();
											}
											
										}
										return false;
									},
									failure: function(xml){
									},
									complete: function( data, status ) { 
		            				}
									});
									return false;
								})
							}
							$('#Dialog .formbutton input').each(function(){
								//////$.log($(this));
								$(this).unbind();
								switch($(this).attr('value')){
									case 'Cancel':
										$(this).unbind().click(function(){$('#Dialog').dialog('close');return false;});
										break;
									case 'Send':
										$('#Dialog .error').remove();
										break;
								}
								return false;
							});
						}else{
							
							if($('.systemMessage span',xml)[0]){
								$('#Content').prepend($('.systemMessage',xml));
								$(curAnchor).empty().append(language[$(curAnchor).attr('name')][newObject]['menutext']);
								$(curAnchor).attr('status',newStatus);
							}else{
								//Total HACK fix later
								curLanguage = language[$(curAnchor).attr('name')];
								if($(curAnchor).attr('status')==1){
									newObject = 'neg';
									newStatus = 0;
								}else{
									newObject = 'pos';
									newStatus = 1;
								}
								$('#errorLine').remove();
								$('#Footer').before('<div id="errorLine"><div id="error"><div id="errorCont"><span class="complete">'+language[$(curAnchor).attr('name')][newObject]['message']+'</span></div></div></div>');
								$(curAnchor).empty().append(language[$(curAnchor).attr('name')][newObject]['menutext']);
								$(curAnchor).attr('status',newStatus);
								if($(curAnchor).attr('name') == 'friend' && $(curAnchor).attr('status')==1){
									$(curAnchor).attr('form',true);
								}
								$('#errorLine').show().displayError();
							}
							return false;					
						}
					},
					failure: function(){},
					complete: function(){
					}
				});
				
					return false;
	 		});
	 	}
}