/**
* swaps image arounds on mouse events down/up and over
*
*@param object		a jquery object
*@param handler		the type of handler mousedown, mouseup, mouseover & mouseout
*/
function swapMenuImages(object, handler) {

	var image = $(object).attr('src');
	var match = image.match(/([^_]+)(.*).(png|gif|jpg)/);
	if(match===null)	return;

	switch(handler) {
		case 'mousedown':
			$(object).attr('src', match[1]+'_on.'+match[3]);
			break;
		case 'mouseup':
		case 'mouseover':
			$(object).attr('src', match[1]+'_over.'+match[3]);
			break;
		case 'mouseout':
			$(object).attr('src', match[1]+'.'+match[3]);
			break;
	}
}


var loadedImages = 0;
function headerPanorama() {

	$('div.headerMenuPanorama').html('<img class=\"panoramaImg\" src=\"'+panorama+'\" width=\"996\" height=\"334\" alt=\"\" border=\"0\" />');
	eventHandlers();

    $('img.panoramaImg').load(function() {
		loadedImages++;		
		if(loadedImages==2) {
	        $('div.headerMenuPanoramaHide').each(function() {
				$(this).css('display', 'block');
			});
		}
    });
}


function headerPanoramaBg() {

	$('div.headerMenuPanorama').html('<img class=\"panoramaImg\" src=\"'+panorama+'\" width=\"996\" height=\"334\" alt=\"\" border=\"0\" />');
	eventHandlers();

    $('img.panoramaImg').load(function() {
		loadedImages++;		
		if(loadedImages==2) {
	        $('div.headerMenuPanoramaHide').each(function() {
				$('div.headerMenuPanoramaOverlay').html('&nbsp;');
				$(this).css('display', 'block');
			});
		}
    });
}


function eventHandlers() {

	if($('ul.headerMenu img').length>0) {
		$('ul.headerMenu img').mousedown(function() {
			swapMenuImages(this, 'mousedown');
		});
		$('ul.headerMenu img').mouseup(function() {
			swapMenuImages(this, 'mouseup');
		});
		$('ul.headerMenu img').mouseout(function() {
			swapMenuImages(this, 'mouseout');
		});
		$('ul.headerMenu img').mouseover(function() {
			swapMenuImages(this, 'mouseover');
		});
	}


	if($('div.buttons img').length>0) {	
		$('div.buttons img').mousedown(function() {
			swapMenuImages(this, 'mousedown');
		});
		$('div.buttons img').mouseup(function() {
			swapMenuImages(this, 'mouseup');
		});
		$('div.buttons img').mouseout(function() {
			swapMenuImages(this, 'mouseout');
		});
		$('div.buttons img').mouseover(function() {
			swapMenuImages(this, 'mouseover');
		});	
	}

	if($('input.button').length>0) {
		$('input.button').mousedown(function() {
			swapMenuImages(this, 'mousedown');
		});
		$('input.button').mouseup(function() {
			swapMenuImages(this, 'mouseup');
		});
		$('input.button').mouseout(function() {
			swapMenuImages(this, 'mouseout');
		});
		$('input.button').mouseover(function() {
			swapMenuImages(this, 'mouseover');
		});
	}

	
	if($('img.button').length>0) {
		$('img.button').mousedown(function() {
			swapMenuImages(this, 'mousedown');
		});
		$('img.button').mouseup(function() {
			swapMenuImages(this, 'mouseup');
		});
		$('img.button').mouseout(function() {
			swapMenuImages(this, 'mouseout');
		});
		$('img.button').mouseover(function() {
			swapMenuImages(this, 'mouseover');
		});
	}

}


var animated = false;
var timeout = false;
var teller = 1;

/**
* Sets the image defined as first image as inline element
* all the other images will be hidden from the view
*
*@param id		the attribute id of the html element thats wrapped around the images
*/
function screenshotSetFirst(id) {

	$('#'+id+' img').each(function() {			
		$(this).css('display', 'none');
	});
	if($('#'+id+' img.first').length>0)
		$('#'+id+' img.first').css('display', 'inline');		
	else	$('#'+id+' img:first').css('display', 'inline');
}


/**
* Starts the movement of the screenshots and continues the movement
* when the 'animated' global variable is not set to false
*
*@param id			the id attribue of the html element thats wrapped around the screenshots
*@param timeout		the milliseconds when the next screenshot should be shown
*/
function screenshotMove(id, timeout) {

	if(timeout===undefined)		timeout = 500;

	//Break out of the timeout loop when its not long animated
	if(animated!=id) {
		screenshotSetFirst(id);
		return false;
	}
	
	var match = 0;
	$('#'+id+' img').each(function() {	
		if(match==1) {
			$(this).css('display', 'inline');
			match = 2;
		} else if(match==0 && $(this).css('display')=='inline') {
			$(this).css('display', 'none');
			match = 1;
		}
	});

	if(match!=2) 	screenshotSetFirst(id);
	if(animated!==false)
		timeout = setTimeout('screenshotMove(\''+id+'\', '+timeout+')', timeout);
}	


/**
* initiaze the movement of the screenshots
*
*@param id			the id attribue of the html element thats wrapped around the screenshots
*@param timeout		the milliseconds when the next screenshot should be shown
*/
function screenshotMoveStart(id, timeout) {	
	if(animated!=id) {
		animated = id;
		clearTimeout(timeout);
		screenshotMove(id, timeout);
	}
};

/**
* Stop the movement of the screenshots when the html element with id 
* if animated.
*
*@param id			the id attribue of the html element thats wrapped around the screenshots
*/
function screenshotMoveStop(id) {
	if(animated==id) {
		animated = false;	
		clearTimeout(timeout);
		screenshotSetFirst(id);
	}
};


$(document).ready(function() {
	
	//Auto submit the select timezone of selection
	if($('#liveShowTimezone select').length>0) {
		$('#liveShowTimezone select').change(function() {
			$("#liveShowTimezone").submit();
		});
	}

	//Creates a popup on any link with the class overlay set
	if($('a.overlay').length>0) {
		$('a.overlay').click(function() { 
			
			//Redirect when the member is already logged in
			/*
			var match = /musclehunksLogin.html/i.exec($(this).attr('href'));
			if(typeof(login)!='undefined' && login===true && match!==null) {
				document.location = 'http://'+window.location.hostname+'/members/';
				return false;
			}
			*/

			//Setting the general width and height
			var width = 750;
			var height = 450;

			//Getting a possible set width and height
			var match = /width=([0-9]+)/i.exec(this.href);
			if(match!==null)
				var width = parseInt(match[1]) + 40;
			var match = /height=([0-9]+)/i.exec(this.href);
			if(match!==null)
				var height = parseInt(match[1]) + 40;
			
			//Removing any variables add the end of the url
			var match = /([^\?]+)/i.exec(this.href);
			var href = match[1];
			
			//Setting the dimensions for the small join page
			if(/join.html$/i.exec(this.href)!==null) {
				width = 700;
				height = 150;
			}

			//Initiliazing the block UI script
			$.blockUI({ 
				message: '<iframe src="'+href+'?smallPage=1" width="'+width+'" height="'+height+'" frameborder="0"></iframe>'
				, css: { width: width+'px'
						, height: height+'px'
						, top:  ($(window).height() - height) /2 + 'px'
						, left: ($(window).width() - width) /2 + 'px'
						, cursor: 'default'
						, border: '1px solid #fff'
						}
				, centerY: true
				, centerX: true 
			}); 

			//Making sure that clicking beside the show message the overlay will close
			$('.blockOverlay').click(function() {$.unblockUI();  });
			return false;

		});
	}
	
	//Initiaze the movement of the screenshots on the video page
	if($('span[class=videoScreenshot]').length>0) {
		$('span[class=videoScreenshot]').hover(
		function() {	screenshotMoveStart(this.id);	}
		, function() {		screenshotMoveStop(this.id);	});
	}

	//Initiaze the movement of the screenshots on the LMS preview
	if($('div[class^=wrapperModelScreenshot]').length>0) {
		$('div[class^=wrapperModelScreenshot]').hover(
		function() {	screenshotMoveStart(this.id);	}
		, function() {		screenshotMoveStop(this.id);	});
	}

	//Initiaze the movement of the screenshots on the join page
	if($('div[id=joinMoveMuscleMen] img').length>0) {
		$('div[id=joinMoveMuscleMen]').hover(
		function() {	screenshotMoveStart(this.id, 1000);	}
		, function() {		screenshotMoveStop(this.id);	});
	}

	//Initiaze the movement of the screenshots on the join page
	if($('div[id=joinMoveLiveShows] img').length>0) {
		$('div[id=joinMoveLiveShows]').hover(
		function() {	screenshotMoveStart(this.id, 800);	}
		, function() {		screenshotMoveStop(this.id);	});
	}

	//Initiaze the movement of the screenshots on the join page
	if($('div[id=joinMovePictures] img').length>0) {
		$('div[id=joinMovePictures]').hover(
		function() {	screenshotMoveStart(this.id, 400);	}
		, function() {		screenshotMoveStop(this.id);	});
	}

	//Initiaze the movement of the screenshots on the join page
	if($('div[id=joinMoveVideos] img').length>0) {
		$('div[id=joinMoveVideos]').hover(
		function() {	screenshotMoveStart(this.id, 800);	}
		, function() {		screenshotMoveStop(this.id);	});
	}

	if($('a[name=liveShow]').length>0) {
  $('a[name=liveShow]').click(function() {
   showSmallBlock('liveshow');
			return false;
		});
	}

	var loadImages = new Array(
	'/buttons/tourPagesSeeTheGalleries_over.png', 
	'/buttons/tourPagesSeeTheGalleries_on.png', 
	'/buttons/tourPagesSeeTheVideos_over.png', 
	'/buttons/tourPagesSeeTheVideos_on.png', 
	'/buttons/tourPagesExperienceMuscleHunks_over.png', 
	'/buttons/tourPagesExperienceMuscleHunks_on.png', 
	'/buttons/tourPagesSeeOurHunksLive_over.jpg', 
	'/buttons/tourPagesSeeOurHunksLive_on.jpg', 
	'/buttons/homeLatestNewsTakeTheTour_over.png', 
	'/buttons/homeLatestNewsTakeTheTour_on.png', 
	'/buttons/homeLatestNewsDvdStore_over.png', 
	'/buttons/homeLatestNewsDvdStore_on.png', 
	'/buttons/homeLatestNewsMembers_on.png', 
	'/buttons/homeLatestNewsMembers_over.png', 
	'/buttons/homeLatestNewsJoinNow_on.gif', 
	'/buttons/homeLatestNewsJoinNow_over.gif', 
	'/buttons/homeLatestNewsSeeHimNakesNow_over.png', 
	'/buttons/homeLatestNewsSeeHimNakesNow_on.png', 
	'/buttons/modelPageLatestShootLiveMuscleShow_over.png', 
	'/buttons/modelPageLatestShootLiveMuscleShow_on.png', 
	'/buttons/indexAccept_on.png', 
	'/buttons/indexMembers_on.gif', 
	'/buttons/indexDecline_on.png', 
	'/buttons/indexAccept_over.png', 
	'/buttons/indexMembers_over.gif', 
	'/buttons/indexDecline_over.png', 
	'/buttons/modelPageLatestShootLiveMuscleShow_on.gif', 
	'/buttons/modelPageLatestShootMoreMhPreviews_on.png', 
	'/buttons/modelPageLatestShootLiveMuscleShow_over.gif', 
	'/buttons/modelPageLatestShootMoreMhPreviews_over.png', 
	'/buttons/modelPageLatestShootLogin_on.png', 
	'/buttons/modelPageLatestShootLogin_over.png', 
	'/buttons/modelPageDvdStore_on.png', 
	'/buttons/modelPageDvdStore_over.png', 
	'/buttons/modelPageLiveMuscleShowSeeHimLiveNow_on.png', 
	'/buttons/modelPageLiveMuscleShowSeeHimLiveNow_over.png', 
	'/buttons/headerMenuLogin_over.png', 
	'/buttons/headerMenuLogin_on.png', 
	'/buttons/headerMenuJoinNow_on.png', 
	'/buttons/headerMenuJoinNow_over.png', 
	'/buttons/headerMenuMhLive_on.png', 
	'/buttons/headerMenuMhLive_over.png', 
	'/buttons/headerMenuModels_over.png', 
	'/buttons/headerMenuModels_on.png', 
	'/buttons/headerMenuHome_over.png', 
	'/buttons/headerMenuHome_on.png', 
	'/buttons/headerMenuNews_on.png', 
	'/buttons/headerMenuNews_over.png', 
	'/buttons/headerMenuDvds_over.png', 
	'/buttons/headerMenuDvds_on.png', 
	'/buttons/headerMenuVod_on.png', 
	'/buttons/headerMenuVod_over.png', 
	'/buttons/headerMenuContact_on.png', 
	'/buttons/headerMenuContact_over.png', 
	'/buttons/customerSupport_on.png', 
	'/buttons/instantCreditCard_on.png', 
	'/buttons/joinByBank_on.png', 
	'/buttons/membersHere_on.png', 
	'/buttons/customerSupport_over.png', 
	'/buttons/instantCreditCard_over.png', 
	'/buttons/joinByBank_over.png', 
	'/buttons/membersHere_over.png', 
	'/buttons/tourPagePhotosBackward_on.png', 
	'/buttons/tourPagePhotosBackward_over.png', 
	'/buttons/tourPagePhotosForward_on.png', 
	'/buttons/tourPagePhotosForward_over.png');
	for(var key in loadImages) {
		$('body').append('<img src="'+loadImages[key]+'" alt="" border="0" class="displayNone" width="0" height="0" />'+"\n");	
	}

});


/* Start Star rating */
$(document).ready(function() {
 $('div.star').click(function() {
  starRating(this, 'click');
		return false;
	});

 $('div.star').mouseover(function() {
  starRating(this, 'over');
	});

 $('div.star').mouseout(function() {
  starRating(this, 'out');
	});
});

function starRating(object, direction) {

 var divId = $(object).parent('div.starsRating').attr('id');
 var position = $(object).css('background-position');
 if(jQuery.data(object, "position")==undefined) {
  $('#'+divId+' div.star').each(function() {
   var position = $(this).css('background-position');
   jQuery.data(this, "position", position);
		});
	}

	if(jQuery.data(object, "position")==undefined) {
  var position = $(object).css('background-position');
  jQuery.data(object, "position", position);
	}
 
 switch(direction) {

  case 'over':   
  	$('#'+divId+' div.star').each(function() {
	   if($(object).offset().left>=$(this).offset().left) {	
     $(this).css('background-position', '0px 20px');
		  }
   });
		 break;

  case 'out':
  	$('#'+divId+' div.star').each(function() {
	   if($(object).offset().left>=$(this).offset().left) {
     $(this).css('background-position', jQuery.data(this, "position"));
		  }
   });
		 break;

  case 'click':
   
			var ratingCurrent = 0;
  	$('#'+divId+' div.star').each(function() {
	   if($(this).hasClass('starRatingGold')) {
     ratingCurrent++;
				}
   });

			var rating = 0;
  	$('#'+divId+' div.star').each(function() {
	   if($(object).offset().left>=$(this).offset().left) {
     rating++;
		  }
   });


  	$('#'+divId+' div.star').each(function() {
  	 $(this).removeData(this, 'position');		  
   });

			var match = /^[A-Z]+([0-9]+)$/i.exec(divId);			
			if((typeof(match[1])==="undefined")) { 
    return;
			}

			$('#'+divId).html('<div class="loading"><img src="/images/small-ajax-loader.gif" alt="" border="0" /></div>');

   var createStarsReturn = false;
   $.get("/memberApi.php", 
			      {'id':match[1], 
				      'display':'rating',
				      'rating':rating},
         function(data){

          if(data.check===false) {

											if(typeof(data.error)!=undefined && data.error===true) {
												createStarsReturn = createStars(divId, ratingCurrent);
            showSmallBlock('error', 'javascript');
	 										return;
											} 

											if(typeof(data.login)!=undefined && data.login===false) {
												createStarsReturn = createStars(divId, ratingCurrent);
            showSmallBlock('join', 'login');
	 										return;
											} 
           

											if(typeof(data.membership)!=undefined && data.membership===false) {
												createStarsReturn = createStars(divId, ratingCurrent);
            showSmallBlock('join', 'membership');
	 										return;
											} 

											if(typeof(data.vod)!=undefined && data.vod===false) {
												createStarsReturn = createStars(divId, ratingCurrent);
            showSmallBlock('join', 'vod');
	 										return;
											} 
										}

										$('#'+divId).html('You\'ve voted!');
										setTimeout(function() {
           createStarsReturn = createStars(divId, data.rating);
										}, 2000);
         }, 'json');

   if(createStarsReturn===false) {
    createStarsReturn = createStars(divId, ratingCurrent);
			}
		 break;

 }
}

function createStars(divId, rating) {

 $('#'+divId).html('');
	for(var i=1; i<=5; i++) {
		if(i<=rating) {
    $('#'+divId).append('<div class="star starRatingGold"></div>');
		} else {
    $('#'+divId).append('<div class="star starRatingGrey"></div>');
		}
	}
}
/* End Star rating */


/* Start favorite content */
$(document).ready(function() {

	$('a.favorite').click(function() {
  favoriteContent(this);
		return false;
	});

});

function favoriteContent(object) {
 
 var divId = $(object).attr('id');
 var match = /^[A-Z]+([0-9]+)$/i.exec(divId);			
	if((typeof(match[1])==="undefined")) { 
  return;
	}
 
	var jsonReturn = false;
	var textCurrent = $('#'+divId).html();
	$('#'+divId).html('<img src="/images/small-ajax-loader.gif" class="loading" alt="" border="0" />');

 $.get("/memberApi.php", 
	      {'id':match[1], 
		      'display':'favorite',
		      'action':'opposite',
		      'favorite':'opposite'},
        function(data){
									
									jsonReturn = true;
         if(data.check===false) {

										if(data.error===true) {
											$('#'+divId).html(textCurrent);
           showSmallBlock('error', 'javascript');
 										return;
										} 

										if(data.login===false) {
											$('#'+divId).html(textCurrent);
           showSmallBlock('join', 'login');
 										return;
										} 
										if(data.membership===false || data.vod===false) {
											$('#'+divId).html(textCurrent);
           showSmallBlock('join', 'membership');
 										return;
										} 
									}

									if(data.store==false) {
          showSmallBlock('error', 'javascript');
										$('#'+divId).html(textCurrent);
										return;
									}

									if(data.favorite=='yes') {
          $('#'+divId).html('delete from favorites');
									} else {
          $('#'+divId).html('add to favorites');
									}
        }, 
								'json');
 
	if(jsonReturn) {
  $('#'+divId).html(textCurrent);
	}
}
/* End favorite content */

function showSmallJoin() {

	showSmallBlock('join', 'membership');

}


function showSmallBlock(display, getVars) {

	if(typeof(display)==undefined) {
  display = 'error';
	} 
 

	switch(display) {

		case 'manOfTheYear':
			var unblock = true;
   var url = '/index.php?smallPage=1&display=manOfTheYearVote'+getVars;
			var width = 900;
			var height = 500;
			break;

		case 'manOfTheYearModel':
			var unblock = true;
   var url = getVars;
			var width = 650;
			var height = 495;
			break;

		case 'checkout':
  	if(typeof(getVars)==undefined) {
    getVars = 'buyShoot';
  	} 
			var unblock = false;
   var url = '/checkout.php?type='+getVars+'&c_smallPage=1';
			var width = 900;
			var height = 500;
			break;
  
		case 'error':
  	if(typeof(getVars)==undefined) {
    getVars = 'javascript';
  	} 
			var unblock = true;
   var url = '/index.php?display=error&type='+getVars+'&smallPage=1';
			var width = 700;
			var height = 150;
			break;

		case 'join':
  	if(typeof(getVars)==undefined) {
    getVars = 'membership';
  	} 
			var unblock = true;
   var url = '/index.php?display=join&type='+getVars+'&smallPage=1';
			var width = 700;
			var height = 150;
			break;

		case 'liveshow':
			var unblock = true;
   var url = '/checkout.php?accessType=liveshow&c_smallPage=1';
			var width = 900;
			var height = 500;
			break;			

	}

	if(typeof(url)==undefined) {
  return;
	} 
		
	//Initiliazing the block UI script
	$.unblockUI();
	$.blockUI({ 
		message: '<iframe src="'+url+'" width="'+width+'" height="'+height+'" frameborder="0"></iframe>',
	 css: { width: width+'px',
		       height: height+'px',
		       top: ($(window).height() - height) /2 + 'px',
		       left: ($(window).width() - width) /2 + 'px',
		       cursor: 'default',
		       border: '1px solid #fff'
   				},
		centerY: true,
		centerX: true 
 }); 

	//Making sure that clicking beside the show message the overlay will close
	if(unblock) {
 	$('.blockOverlay').click(function() {  $.unblockUI();  });
	}
	return false;

}
