$(document).ready(function() {

	//
	//	HEADER FUNCTIONS
	//
	//	/home/live/_templates/layouts/layout_03/www/common/header.tpl
	//
	
	//
	//	Handles showing/hiding of login form
	//
	$('#quick_login_link').click(function(event) {
	
		//
		//	Stop link from working
		//
		event.preventDefault();
		
		$('#quick_login_block').fadeOut( 300, function() {
		
			$('#quick_login_form').fadeIn( 300, function() {
			
				if( $('#quick_login_form').css('display') != 'block' ) {
				
					$('#quick_login_block').hide();
					
					$('#quick_login_form').show();
				
				}
			
			});
		
		});
		
		
		
	});
	
	$('#quick_login_link-close').click(function(event) {
	
		//
		//	Stop link from working
		//
		event.preventDefault();
		
		$('#quick_login_form').fadeOut( 300, function() {
		
			$('#quick_login_block').fadeIn( 300, function() {
			
				if( $('#quick_login_block').css('display') != 'block' ) {
				
					$('#quick_login_form').hide();
					
					$('#quick_login_block').show();
				
				}
			
			});
		
		});
		
	});
	
	
	//
	//	Handles display of generic text if there's no nickname in guest login
	//	
	if( $('#login_chat_nickname').attr('value') == 'Type a Nickname' ) {
	
		$('#login_chat_nickname').css('color','#ccc');
	
		$('#login_chat_nickname').css('font-style','italic');
	
	}
	
	$('#login_chat_nickname').focus(function(event) {
	
		if( $(this).attr('value') == 'Type a Nickname' ) {
		
			$(this).attr('value','');
			
			$(this).css('color','#000');
		
			$(this).css('font-style','normal');
		
		}
		
		$("#custom_nickname_custom").attr("checked","checked");
	
	});
	
	$('#login_chat_nickname').blur(function(event) {
	
		if( $(this).attr('value') == '' ) {
		
			$(this).attr('value','Type a Nickname');
			
			$(this).css('color','#ccc');
			
			$(this).css('font-style','italic');
		
		}
	
	});
	
	
	//
	//	Handles display of generic text if there's no search in Models Search
	//	
	if( $('#search_text').attr('value') == 'Search Models' ) {
	
		$('#search_text').css('color','#ccc');
	
	}
	
	$('#search_text').focus(function(event) {
	
		if( $(this).attr('value') == 'Search Models' ) {
		
			$(this).attr('value','');
			
			$(this).css('color','#000');
		
		}
	
	});
	
	$('#search_text').blur(function(event) {
	
		if( $(this).attr('value') == '' ) {
		
			$(this).attr('value','Search Models');
			
			$(this).css('color','#ccc');
		
		}
	
	});
	
	if( TOP_NAV == 'live_chat' ) {
	
		if( !is_screen_name ) {
			$('#screen_name_select').hide();
			$('#screen_name_field').show();
		}
		
		var current_screen_name = $('#screen_name_dropdown').attr('selectedIndex');
		
		$('#screen_name_dropdown').change(function(event) {
		
			if( $(this).val() == 'other' ) {
			
				$('#screen_name_select').fadeOut( 300, function() {
				
					$('#screen_name_field').fadeIn( 300 );
				
				});
			
			}
		
		});
		
		$('#screen_name_field-close').click(function(event) {
		
			event.preventDefault();
			
			$('#screen_name_dropdown').attr('selectedIndex', current_screen_name);
			
			$('#screen_name_field').fadeOut( 300, function() {
			
				$('#screen_name_select').fadeIn( 300 );
			
			});
			
		});
	
	}
	
	
	//
	//	MODELS BIOS FUNCTIONS && MY ACCOUNT FUNCTIONS
	//
	//	/home/live/_templates/layouts/layout_03/www/models/bio-gifts.tpl
	//	/home/live/_templates/layouts/layout_03/www/my-account/secure/index.tpl
	//
	$('.gift_details').click(function(event) {
		
		//
		//	Stop link from working
		//
		event.preventDefault();
		
		var details_row	= $(this).attr('name');
		
		if( $(this).html() == 'View Details' ) {
			$('#gift_' + details_row).show();
			$(this).html('Hide Details');
		} else {
			$('#gift_' + details_row).hide();
			$(this).html('View Details');
		}
	
	});
	
	$('#toggle_details').click(function(event) {
	
		//
		//	Stop link from working
		//
		event.preventDefault();
		
		if( $(this).html() == 'Expand All Details' ) {
		
			$('.details_row').show();
			$(this).html('Hide All Details');
			$('.gift_details').html('Hide Details');
		
		} else {
		
			$('.details_row').hide();
			$(this).html('Expand All Details');
			$('.gift_details').html('View Details');
		
		}
	
	});
	
	
	//
	//	MY ACCOUNT FUNCTIONS
	//
	//
	
	//
	//	EDIT RECORD
	//	
	//	Replace the values with form fields that 
	//	let the user edit the information.
	//
	//	/home/live/_templates/layouts/layout_03/www/my-account/secure/index-show_payment.tpl
	//
	$('.edit_record').click( function(event) {
	
		event.preventDefault();
	
		var account_last_digits		= $(this).attr('id').slice(5);
		
		var account_edit_block		= "#account_edit_" + account_last_digits;
		
		var account_view_block		= "#account_view_" + account_last_digits;
		
		var edit_block				= "#edit_" + account_last_digits;
		
		var cancel_block			= "#cancel_" + account_last_digits;
		
		$( account_edit_block ).show();
		
		$( account_view_block ).hide();
		
		$( cancel_block ).show();
		
		$( edit_block ).hide();
	
	});
	
	//
	//	SCREEN NAME BIO BIRTHDAYS
	//
	//	/home/live/_templates/layouts/layout_03/www/my-account/secure/index-view_bio.tpl
	//
	if( !$("#no_birthday").attr('checked') ) {
		$("#birthday_div").show();
	}
	$("#no_birthday").click(function() {
		$("#birthday_div").toggle();
	});
	
	
	//
	//	CANCEL EDIT
	//	
	//	Hide the form elements and revert back to 
	//	simply displaying the values.
	//
	$('.cancel_edit').click( function(event) {
	
		event.preventDefault();
	
		var account_last_digits		= $(this).attr('id').slice(7);
		
		var account_edit_block		= "#account_edit_" + account_last_digits;
		
		var account_view_block		= "#account_view_" + account_last_digits;
		
		var edit_block				= "#edit_" + account_last_digits;
		
		var cancel_block			= "#cancel_" + account_last_digits;
		
		$( account_edit_block ).hide();
		
		$( account_view_block ).show();
		
		$( cancel_block ).hide();
		
		$( edit_block ).show();
	
	});
	
	
	//
	//	SEARCH FUNCTIONS
	//
	//	/home/live/_templates/layouts/layout_03/www/search/index.tpl
	//
	$('#toggle_adv_search').click(function(event) {
	
		//
		//      Stop link from working
		//
		event.preventDefault();
		
		if( $('#advanced_search_div').css('display') == 'none' ) {
		
			$(this).html('Hide Advanced Search');
		
		} else {
		
			$(this).html('Show Advanced Search');
		
		}
		
		$('#advanced_search_div').toggle();
	
	});
	
	
	//
	//	EXTERNAL/AJAX FUNCTIONS
	//
	
	//
	//	Add-to-favorites and Notify-me AJAX functionality
	//
	$('.external').click(function(event) {
	
		//
		//	Default vars
		//
		var remove_favorite 	= 'remove-favorites-icon.gif';
		var add_favorite		= 'favorites-icon.gif';
		var remove_notify		= 'stop-notify-icon.gif';
		var add_notify			= 'notify-me-icon.gif';
		
		//
		//	Override images if necessary
		//		
		if( typeof( override_remove_favorite ) != 'undefined' 	) remove_favorite 	= override_remove_favorite;
		if( typeof( override_add_favorite ) != 'undefined' 		) add_favorite		= override_add_favorite;
		if( typeof( override_remove_notify ) != 'undefined' 	) remove_notify		= override_remove_notify;
		if( typeof( override_add_notify ) != 'undefined' 		) add_notify		= override_add_notify;
		
		//
		//	Stop link from working
		//
		event.preventDefault();
		
		//
		//	Grab necessary data from HTML
		//
		var DATA 		= $(this).attr('name');
		var PARTS 		= DATA.split('::');
		var icon		= PARTS[0];
		var action		= PARTS[1];
		var id			= PARTS[2];
		var name		= PARTS[3];
		
		//
		//	Define AJAX variables
		//
		var url			= '/external.php';
		var data		= 'a=' + action + '&id=' + id + '&name=' + name + '&' + COMMON_RAW;
		
		//
		//	Define Local Reference
		//	For some reason, after the AJAX call, $(this) no longer exists...
		//
		var THIS = $(this);
		
		//
		//	Run AJAX request
		//
		$.ajax({
		
			type: 	'GET',
			url: 	url,
			data: 	data,
			success: function(result) {
			
				if( result == 1 ) {
				
					if( action == 'add_favorite' ) {
						
						var icon_src	= '/images/skins/' + SKIN.sitekey + '/layout_03/' + remove_favorite;
						var icon_alt	= 'Delete ' + name + ' from Favorites';
						var icon_text	= 'Delete ' + name + ' from Favorites';
						var icon_name	= icon + '::' + 'delete_favorite' + '::' + id + '::' + name;
						var icon_id		= 'add_to_favorites_link';
						var icon_label	= 'Remove Favorite';
					
					} else if( action == 'delete_favorite' ) {
						
						var icon_src	= '/images/skins/' + SKIN.sitekey + '/layout_03/' + add_favorite;
						var icon_alt	= 'Add ' + name + ' to Favorites';
						var icon_text	= 'Add ' + name + ' to Favorites';
						var icon_name	= icon + '::' + 'add_favorite' + '::' + id + '::' + name;
						var icon_id		= 'add_to_favorites_link';
						var icon_label	= 'Add to Favorites';
					
					} else if( action == 'add_notify' ) {
					
						var icon_src	= '/images/skins/' + SKIN.sitekey + '/layout_03/' + remove_notify;
						var icon_alt	= 'Stop ' + name + ' Online Notification';
						var icon_text	= 'Stop ' + name + ' Online Notification';
						var icon_name	= icon + '::' + 'delete_notify' + '::' + id + '::' + name;
						var icon_id		= 'notify_me_link';
						var icon_label	= 'Stop Notification';
						var icon_class	= 'notify_icon';
					
					} else if( action == 'delete_notify' ) {
					
						var icon_src	= '/images/skins/' + SKIN.sitekey + '/layout_03/' + add_notify;
						var icon_alt	= 'Notify Me When  ' + name + ' is Online';
						var icon_text	= 'Notify Me When  ' + name + ' is Online';
						var icon_name	= icon + '::' + 'add_notify' + '::' + id + '::' + name;
						var icon_id		= 'notify_me_link';
						var icon_label	= 'Notify Me';
						var icon_class	= 'stop_notify_icon';
					
					} else if( action == 'add_fav_gallery' ) {
					
						var icon_src	= '/images/skins/' + SKIN.sitekey + '/layout_03/' + remove_favorite;
						var icon_alt	= 'Delete ' + name + ' gallery from Favorites';
						var icon_text	= 'Delete ' + name + ' gallery from Favorites';
						var icon_name	= icon + '::' + 'delete_favorite' + '::' + id + '::' + name;
						var icon_id		= 'add_to_favorites_link';
						var icon_label	= 'Remove gallery from Favorites';
					
					} else if( action == 'delete_fav_gallery' ) {
						
						var icon_src	= '/images/skins/' + SKIN.sitekey + '/layout_03/' + add_favorite;
						var icon_alt	= 'Add ' + name + ' gallery to Favorites';
						var icon_text	= 'Add ' + name + ' gallery to Favorites';
						var icon_name	= icon + '::' + 'add_favorite' + '::' + id + '::' + name;
						var icon_id		= 'add_to_favorites_link';
						var icon_label	= 'Add gallery to Favorites';
					
					}  else if( action == 'add_fav_video' ) {
					
						var icon_src	= '/images/skins/' + SKIN.sitekey + '/layout_03/' + remove_favorite;
						var icon_alt	= 'Delete ' + name + ' clip from Favorites';
						var icon_text	= 'Delete ' + name + ' clip from Favorites';
						var icon_name	= icon + '::' + 'delete_favorite' + '::' + id + '::' + name;
						var icon_id		= 'add_to_favorites_link';
						var icon_label	= 'Remove clip from Favorites';
					
					} else if( action == 'delete_fav_video' ) {
						
						var icon_src	= '/images/skins/' + SKIN.sitekey + '/layout_03/' + add_favorite;
						var icon_alt	= 'Add ' + name + ' clip to Favorites';
						var icon_text	= 'Add ' + name + ' clip to Favorites';
						var icon_name	= icon + '::' + 'add_favorite' + '::' + id + '::' + name;
						var icon_id		= 'add_to_favorites_link';
						var icon_label	= 'Add clip to Favorites';
					
					}
					
					$('.' + icon).attr( 'src', icon_src );
					$('.' + icon).attr( 'alt', icon_alt );
					$(THIS).attr( 'title', icon_text );
					$(THIS).attr( 'name', icon_name );
					
					if( TOP_NAV == 'live_chat' ) {
					
						$('#' + icon_id + ' .label').html( icon_label );
					
					}
				
				} else {
				
					var error_text = '';
					
					if( action == 'add_favorite' || action == 'delete_favorite' ) {
					
						error_text = 'Only VIP Members can add Models to their Favorites list.';
					
					} else if( action == 'add_notify' || action == 'delete_notify' ) {
					
						error_text = 'Only VIP Members can be notified when a model logs in.';
					
					} else if( action == 'add_fav_gallery' || action == 'delete_fav_gallery' ) {
					
						error_text = 'Only VIP Members can add Model Photo Galleries to their Favorites list.';
					
					} else if( action == 'add_fav_video' || action == 'delete_fav_video' ) {
					
						error_text = 'Only VIP Members can add VOD/Private Show Archive Videos to their Favorites list.';
					
					}
					
					$.fn.colorbox({href:"/vip/preview.php?small=1&id=" + id + "&error_text=" + escape( error_text ), open:true});
				
				}
			
			}
		
		});
	
	});
	

});


//
//  Used to Refresh the Rooms List
//
function refreshPage() {
	
	window.location.reload();

}


//
//	FORUMS FUNCTIONS
//
//	/home/live/_templates/layouts/layout_03/www/forums/post.tpl
//
function quoteMessage() {

	var currentText = document.postForm.message.value;
	
	var newText = originalMessage + "\n\n" + currentText;
	
	document.postForm.message.value = newText;

}

function insertEmoticon( iconString ) {

	var currentText = document.postForm.message.value;
	
	var newText = currentText + " " + iconString + " ";
	
	document.postForm.message.value = newText;
	
	document.postForm.message.focus();

}


//
//	VIP FUNCTIONS
//
//	/home/live/_templates/layouts/layout_03/www/vip/members/daily-vod-view.tpl
//

//
//	Change the text associated with one of the rating fields.
//
function changeRatingText( element_id, text ) {

	if( document.getElementById ) {
		
		ratingObject		= document.getElementById( element_id + "RatingText" );
		
	} else {
		
		ratingObject		= eval( "document." + element_id + "RatingText" );
		
	}
	
	ratingObject.innerHTML = text;

}

//
//	Empty a Text Input Field
//
function emptyTextInput( field_name ) {

	if( document.getElementById ) {
		
		fieldObject		= document.getElementById( field_name );
		
	} else {
		
		fieldObject		= eval( "document." + field_name );
		
	}
	
	fieldObject.value = '';

}

//
//	Change Rating Image
//
function changeRatingImage( element_id, image_number, state ) {

	if( document.getElementById ) {
		
		starObject1		= document.getElementById( element_id + 'RatingStar1' );
		starObject2		= document.getElementById( element_id + 'RatingStar2' );
		starObject3		= document.getElementById( element_id + 'RatingStar3' );
		starObject4		= document.getElementById( element_id + 'RatingStar4' );
		starObject5		= document.getElementById( element_id + 'RatingStar5' );
		
	} else {
		
		starObject1		= eval( "document." + element_id + "RatingStar1" );
		starObject2		= eval( "document." + element_id + "RatingStar2" );
		starObject3		= eval( "document." + element_id + "RatingStar3" );
		starObject4		= eval( "document." + element_id + "RatingStar4" );
		starObject5		= eval( "document." + element_id + "RatingStar5" );
		
	}
	
	if( state == "on" ) {
	
		var image_src = '/vod/images/rating-on.gif';
	
	} else {
	
		var image_src = '/vod/images/rating-off.gif';
	
	}
	
	switch( image_number ) {
	
		case 1:
			starObject1.src = image_src;
			break;
		
		case 2:
			starObject1.src = image_src;
			starObject2.src = image_src;
			break;
			
		case 3:
			starObject1.src = image_src;
			starObject2.src = image_src;
			starObject3.src = image_src;
			break;
			
		case 4:
			starObject1.src = image_src;
			starObject2.src = image_src;
			starObject3.src = image_src;
			starObject4.src = image_src;
			break;
			
		case 5:
			starObject1.src = image_src;
			starObject2.src = image_src;
			starObject3.src = image_src;
			starObject4.src = image_src;
			starObject5.src = image_src;
			break;
		
	}

}

var http_request 	= false;
var my_target		= false;	// Target HTML element ID for response

function makeRequest( url, parameters, target_element ) {

	//	
	//	Define Necessary Variables
	//
	parameters 		= parameters.replace( /,/g, "&" );
	
	my_target 		= target_element;
	
	http_request 	= false;
	
	// Mozilla, Safari,...
	if (window.XMLHttpRequest) {
	
		http_request = new XMLHttpRequest();
		
		if (http_request.overrideMimeType) {
			//	set type accordingly to anticipated content type
			//	http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	
	//
	//	Internet Explorer
	//
	} else if( window.ActiveXObject ) {
	
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	
	}
	
	if( !http_request ) {
	
		alert('Cannot create XMLHTTP instance');
		return false;
	
	}
	
	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);

}

function alertContents() {

	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			result = http_request.responseText;
			document.getElementById( my_target ).innerHTML = result;            
		} else {
			alert('There was a problem with the request.');
		}
	}

}


//
//	MODELS BIOS FUNCTIONS
//

//
//	Photo Viewer
//
//	/home/live/_templates/layouts/layout_03/www/models/bio-photos.tpl
//
function RotatePhotoViewer(iDelta) {

	intPhotoViewer += iDelta;
	while(intPhotoViewer < 1)  intPhotoViewer += totalPhotos;
	while(intPhotoViewer > totalPhotos )  intPhotoViewer -= totalPhotos;
	LoadPhotoViewer();

}

function SetPhotoViewer(iIndex) {

	intPhotoViewer = iIndex;
	LoadPhotoViewer();

}

function LoadPhotoViewer() {

	if( document.images ) {
	
		document.images['imgPhotoView'].src = arrPhotoViewer[intPhotoViewer-1];
	
	}

	if( document.getElementById('spnPhotoViewCount') ) {
	
		document.getElementById('spnPhotoViewCount').innerHTML = '' + intPhotoViewer;
	
	}

}


//
//	MY ACCOUNT FUNCTIONS
//
//	/home/live/_templates/layouts/layout_03/www/my-account/secure/index.tpl
//
function SubmitMonth() {
	
	$(document).ready(function() {
	
		var month, year;
	
		month = $("#frm_month").val();
		year  = $("#frm_year").val();
	
		$("#month").attr('value', month);
		$("#year").attr('value', year);
	
		$("#show_month_form").submit();
	
	});
}

//
//  Load New Window Centered (all toolbars)
//
function newWindow( url, target, extra ) {
	var win	= window.open( url, target, extra );
	win.focus();
}




//
//	jQuery Cookie Function
//
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


//
// image rotator
//
(function($) {
		  
	$.fn.jRotator = function(path, width, height, speed) {
		
		//
		// default configuration options
		//
		var cfg = {
			
			rotation_delay		: 500,	// in milliseconds
			recording_id		: 0,
			direct_path			: '',
			current_image_num	: 1,	// current image to display set to 0 if we want to delay.	
			interval_id			: 0,	// Used to later clear interval.
			preload_pic			: new Image( width, height ),	//Used to preload the next image
			last_image_num		: 10,
			base_path			: ''
			
		};

		//external option
		cfg = $.extend(cfg, {base_path:path, rotation_delay:speed} );
		
		//
		// Handles the actual cycleing of the images.
		//
		function cycleImages() {
			
			if( cfg.current_image_num == cfg.last_image_num )	cfg.current_image_num = 1;
			else												cfg.current_image_num++;
			
			$("#r_"+cfg.recording_id).attr("src", cfg.direct_path + '' + cfg.current_image_num + '.jpg');
			
			// Preload the next image.
			cfg.preload_pic.src = cfg.direct_path + '' + ( cfg.current_image_num + 1 ) +'.jpg';
		
		}
		
		
		//
		// Private function for handling mouse 'hovering'.
		// a.name contains the image location thats difrent for each image.
		// img.id contains the item id
		// img.rel contains storyboard_num 
		//
		var handleStart = function(e) {
			
			recording_id		= $("img", this).attr('id');
			cfg.recording_id	= recording_id.replace("r_", "");
			
			cfg.direct_path		= cfg.base_path + cfg.recording_id.substr(0,3) + "/" + cfg.recording_id.substr(3,4) + "/";
			
			cfg.last_image_num	= $(this).attr('rel');
			
			//
			// Make sure that num_storyboard has images before setting the Interval
			//
			if( cfg.last_image_num != '' && cfg.last_image_num != 0 ) {
				
				cfg.interval_id		= setInterval( cycleImages, cfg.rotation_delay );
			
			}
			
		};
		
		
		//
		// Private function for handling mouse 'out'
		//
		var handleStop = function(e) {
		
				clearInterval( cfg.interval_id );
				
				cfg.current_image_num	= 1;
				
				cfg.current_recording	= 0;
				
				cfg.interval_id			= 0;
		
		}
		
		
		//
		// bind the function to the two event listeners ( start / stop )
		//		
		return this.hover(handleStart, handleStop); 
		
	};
	
})(jQuery);