//VideoViewer constructor
function VideoViewer( oParams ) {
	
	var oVideoPlayer = document.getElementById( 'ciaoplayer' ).getElementsByTagName( 'embed' );
	
	this.oVideoOpinion = document.getElementById( 'videoOpinion' );
	this.oParams = new Object();
	this.oParams = oParams;
	
	if( !oVideoPlayer[ 0 ] || !oVideoPlayer[ 0 ].width ) { //!oVideoPlayer[0].width - for Opera
		this.oVideoPlayer = document.getElementById( 'ciaoplayer' );
	} else {
		this.oVideoPlayer = oVideoPlayer[ 0 ];
	}
}

//VideoViewer class
VideoViewer.prototype = {
	
	bIsBingLayout : false,

	videoWatched : function() {
		
		this.hidePlayer();
		if ( this.bIsBingLayout == false ) {
			this.fillRighFormSite();
		}
		
		oVideoTracking = new VideoTracking( this.oParams.iVideoId );
		oVideoTracking.storeWatchedVideo();
	},
	
	hidePlayer : function() {
		
		oPosition = findElementPosition( this.oVideoPlayer );
		
		this.oVideoOpinion.style.position = 'absolute';
		if ( this.bIsBingLayout ) {
			this.oVideoOpinion.style.top = '0px';
			this.oVideoOpinion.style.left = '0px';
		} else {
			this.oVideoOpinion.style.top = oPosition.curtop + 'px';
			this.oVideoOpinion.style.left = oPosition.curleft + 'px';
		}

		this.oVideoOpinion.style.width = this.oVideoPlayer.width + 'px';
		this.oVideoOpinion.style.height = parseInt( this.oVideoPlayer.height ) + 'px';
		this.oVideoOpinion.style.display = 'block';
	},
	
	showPlayer : function() {
		this.oVideoOpinion.style.display = 'none';
		this.oVideoPlayer.style.visibility = 'visible';
		if ( this.bIsBingLayout == false ) {
			this.moveFromRightFormSite();
		}
	},
	
	fillRighFormSite : function() {
		oRightFormDiv = document.getElementById( 'videoreview_right_space' );
		
		oDescriptionDiv = document.getElementById( 'videoreview_description' );
		oLastCommentDiv = document.getElementById( 'videoreview_lastcomment_container' );
		oOffersDiv = document.getElementById( 'videoreview_offers' );
		
		oRightFormDiv.appendChild( oDescriptionDiv );
		
		if( oLastCommentDiv ) {
			oRightFormDiv.appendChild( oLastCommentDiv );
		}
		
		oRightFormDiv.appendChild( oOffersDiv );
	},
	
	moveFromRightFormSite : function() {
		oTargetTd = document.getElementById( 'vieoreview_right_part' );
		
		oDescriptionDiv = document.getElementById( 'videoreview_description' );
		oOffersDiv = document.getElementById( 'videoreview_offers' );
		oLastCommentDiv = document.getElementById( 'videoreview_lastcomment_container' );
		
		sTargetTdContent = oTargetTd.innerHTML;
		oTargetTd.innerHTML = '';
		
		oTargetTd.appendChild( oDescriptionDiv );
		oTargetTd.appendChild( oOffersDiv );
		
		if( oLastCommentDiv ) {
			oTargetTd.appendChild( oLastCommentDiv );
		}
		
		oTargetTd.innerHTML += sTargetTdContent;
	}
}

//VideoTracking constructor and callbacks
function VideoTracking( iVideoId ) {
	
	this.oAjax = new AjaxClient();
	this.iVideoId = iVideoId;
	
	this.cbStoreWatchedVideoSuccess = function( oResult ) {},
	
	this.cbError = function( oResult ) {}
}

//VideoTracking class
VideoTracking.prototype = {
	
	storeWatchedVideo : function() {
		
		var oParams	= new Object();
		oParams.iVideoId = this.iVideoId;
		
		this.oAjax.onSuccess = this.cbStoreWatchedVideoSuccess;
		this.oAjax.onError = this.cbError;
				
		this.oAjax.call( 'ajax.php', 'POST', 'VideoReviewTrackingAjaxHandler', 'storeVideoWatched', oParams );
	}
	
}

//VideoRating constructor and callbacks
function VideoRating( oParams ) {
	this.oAjax = new AjaxClient();
	this.oParams = new Object();
	this.oParams = oParams;
	
	var self = this;//reference to this visible in callback functions
	
	this.cbRateSuccess = function( oResult ) {
		var oImg = document.getElementById( 'videoreview_ratingaverage' );
		var oCountSpan = document.getElementById( 'videoreview_ratingcount' );
		
		if( oResult.oMemberAction ) {
			VideoRating.prototype.showMemberActions( oResult.oMemberAction );
		}
		if( oResult.oFavourites ) {
			VideoRating.prototype.showFavouritesLink( oResult.oFavourites );
		}		
		self.showThxCommunique();
		
		var sImgFile = Math.round( oResult.rating * 2 ) * 5;
		oImg.src = self.sAverageImgPath + sImgFile + '.gif';
		oCountSpan.innerHTML = oResult.rating_count;
	};
	
	this.cbError = function( sCommunique ) {}
}

//VideoViewer class
VideoRating.prototype = {
	
	oStringTable : new Object(),
	iThxShowingTime : 3000,
	sAverageImgPath : null,
	iNote : null,
	
	getNoteFromCookie : function() {
		if ( this.oParams.bLoggedIn ) {
			return getCookie( 'iRateVideoId' + this.oParams.iVideoId );
		}
	},
	
	setAverageImgPath : function( sPath ) {
		this.sAverageImgPath = sPath;
	},
	
	setNote : function( iNote ) {
		this.iNote = iNote;
	},
	
	addToStringTable : function( oStringTable ) {
		for( var sProperty in oStringTable ) {
			this.oStringTable[ sProperty ] = oStringTable[ sProperty ];
		}
	},
	
	changeBarText : function( iNote ) {
		oText = document.getElementById( 'videoreview_ratingtext' );
		oBar = document.getElementById( 'videoreview_ratingbar' );
		
		var aTexts = new Array(
			this.oStringTable.video_rating_not_helpful,
			this.oStringTable.video_rating_somewhat_helpful,
			this.oStringTable.video_rating_helpful,
			this.oStringTable.video_rating_very_helpful,
			this.oStringTable.video_rating_exceptional
		);
	
		if( iNote > 0 && iNote <= 5 ) {
			
			if ( this.oParams.bLoggedIn ) {
				var iIndex = iNote - 1;
				
				oText.innerHTML = aTexts[ iIndex ];
				oBar.title = aTexts[ iIndex ] ;
			} else {
				oText.innerHTML = this.oStringTable.loggedin_action;
				oBar.title = oText.innerHTML;
			}
		
		} else {		
			oText.innerHTML = '';		
			oBar.title = '';		
		}
	},
	
	showThxCommunique : function() {
		var oThxDiv = document.getElementById( 'videoreview_ratingthx' );
		oThxDiv.style.display = 'inline';
		oText = document.getElementById( 'videoreview_ratingtext' );
		oText.style.display = 'none';
		
		iTimeoutId = setTimeout( this.hideThxCommunique, this.iThxShowingTime );
	},
	
	hideThxCommunique : function() {
		var oThxDiv = document.getElementById( 'videoreview_ratingthx' );
		oThxDiv.style.display = 'none';
		oText = document.getElementById( 'videoreview_ratingtext' );
		oText.style.display = 'inline';
	},
	
	showMemberActions : function( oMemberActionData ) {
		oActionsDiv = document.getElementById( 'member_actions' );
		
		if ( !oActionsDiv ) {
			return;
		}
		
		while ( oActionsDiv.childNodes.length > 0 ) {
			oActionsDiv.removeChild( oActionsDiv.childNodes[ 0 ] );
		}
		
		for ( var sProperty in oMemberActionData ) {
			
			oImg = document.createElement( 'img' );
			oImg.src = oMemberActionData[ sProperty ].sImage;
			
			oSeparator = document.createTextNode( ' ' );
			
			oA = document.createElement( 'a' );
			oA.href = oMemberActionData[ sProperty ].sLink;
			
			oText = document.createTextNode( oMemberActionData[ sProperty ].sText );
			
			oA.appendChild( oText );
			
			oActionsDiv.appendChild( oImg );
			oActionsDiv.appendChild( oSeparator );
			oActionsDiv.appendChild( oA );
		}
		oActionsDiv.style.display = 'block';
	},
	
	showFavouritesLink : function ( oFavourites ) {
		var oActionsDiv = $( '#member_actions' );
		if ( !oActionsDiv ) {
			return;
		}		
		var oActionsDivHtml =  oActionsDiv.html();
		oActionsDiv.html( oActionsDivHtml + oFavourites );
	},
	
	rate : function() {
		setCookie( 'iRateVideoId' + this.oParams.iVideoId, this.iNote , 20);
		
		var oParams	= new Object();
		oParams.iVideoId = this.oParams.iVideoId;
		oParams.iProductId = this.oParams.iProductId;
		oParams.iRating = this.iNote;
		oParams.sReferrer = this.oParams.oMemberAction.sReferrer;
		oParams.sActualPath = this.oParams.oMemberAction.sActualPath;
	
		this.oAjax.onSuccess 	= this.cbRateSuccess;
		/*this.oAjax.onLoading 	= cbError;*/
		this.oAjax.onError 	= this.cbError;
		
		this.oAjax.call( 'ajax.php', 'POST', 'VideoReviewRatingAjaxHandler', 'rate', oParams );
	}
}

//VideoComment constructor and callbacks
function VideoComment( oParams ) {
	
	this.oAjax = new AjaxClient();
	this.oParams = new Object();
	this.oParams = oParams;
	
	var self = this;//reference to this visible in callback functions
	
	this.cbCommentSuccess = function( oResult ) {
		if( oResult.oMemberAction ) {
			VideoRating.prototype.showMemberActions( oResult.oMemberAction );
		}
		if( oResult.oFavourites ) {
			VideoRating.prototype.showFavouritesLink( oResult.oFavourites );
		}
		
		switch( self.oParams.iPageType ) {
			case 2: // video review overview page with BING layout
				self.cbCommentSuccessOverviewPageBing( oResult );
				break;
			case 1: //video review comments page
				self.cbCommentSuccessCommentsPage( oResult );
				break;
			case 0: //video review overview page
			default:
				self.cbCommentSuccessOverviewPage( oResult );
				break;
		}	
		
	}

	this.cbCommentSuccessOverviewPageBing = function( oResult ) {

		var oSrcText = null;
		var oDestText = null;

		// move latest comments down on the list and remove last one
		var iCommentsToMove = ( self.iCommentsPerPage - 1 );
		if ( self.iCurrentCommentsOnPage < self.iCommentsPerPage ) {
			iCommentsToMove = self.iCurrentCommentsOnPage;
		}
		for ( i = iCommentsToMove; i > 0; i-- ) {

			oDestText = new VideoCommentsList( 'video_review_comment_', i );
			oSrcText = new VideoCommentsList( 'video_review_comment_', i - 1 );

			oDestText.oMemberImg.src = oSrcText.oMemberImg.src;
			oDestText.oMemberStatus.innerHTML = oSrcText.oMemberStatus.innerHTML;
			oDestText.oMemberLoginLink.href = oSrcText.oMemberLoginLink.href;
			oDestText.oMemberLoginLink.innerHTML = oSrcText.oMemberLoginLink.innerHTML;
			oDestText.oDate.innerHTML = oSrcText.oDate.innerHTML;
			oDestText.oText.innerHTML = oSrcText.oText.innerHTML;
		}

		// set data with new comment
		oDestText = new VideoCommentsList( 'video_review_comment_', 0 );
		oDestText.oMemberImg.src = oResult.oMember.sImgPath;
		oDestText.oMemberStatus.innerHTML = oResult.oMember.sCommunityLevelImgTag;
		oDestText.oMemberLoginLink.href = oResult.oMember.sProfileUrl;
		oDestText.oMemberLoginLink.innerHTML = oResult.oMember.sLogin;
		oDestText.oDate.innerHTML = oResult.oComment.sCreated;
		oDestText.oText.innerHTML = oResult.oComment.sContent;

		if ( self.iCurrentCommentsOnPage == 0 ) {
			document.getElementById( 'video_review_comment_module' ).style.display = 'block';
			document.getElementById( 'video_review_comment_box_0' ).style.display = 'block';
		} else if ( self.iCurrentCommentsOnPage < self.iCommentsPerPage ) {
			document.getElementById( 'video_review_comment_box_' + self.iCurrentCommentsOnPage ).style.display = 'block';
		}

		// clear and hide box with write comment module
		document.getElementById( 'videoreview_commenttext' ).value = '';
		document.getElementById( 'video_review_comment_write_thx' ).style.display = 'block';
		document.getElementById( 'video_review_comment_write_box' ).style.display = 'none';
	}

	this.cbCommentSuccessOverviewPage = function( oResult ) {
		var oContainerDiv = document.getElementById( 'videoreview_lastcomment_container' );
		var oCountSpan = document.getElementById( 'videoreview_commentcount' );
		var oUserA = document.getElementById( 'videoreview_lastcomment_user' );
		var oUserImg = document.getElementById( 'videoreview_lastcomment_user_img' );
		var oDateSpan = document.getElementById( 'videoreview_lastcomment_date' );
		var oTextSpan = document.getElementById( 'videoreview_lastcomment_text' );
		var oCommentDiv = document.getElementById( 'videoreview_commentcontainer' );
		var oTextArea = document.getElementById( 'videoreview_commenttext' );
		var oThxDiv = document.getElementById( 'videoreview_commentthx' );
		
		oContainerDiv.style.display = 'block';
		
		if ( oCountSpan ) {
			oCountSpan.innerHTML = parseInt( oCountSpan.innerHTML ) + 1;
		}
		oUserA.innerHTML = oResult.oMember.sLogin;
		oUserA.href = oResult.oMember.sProfileUrl;
		oUserImg.src = oResult.oMember.sImgPath;
		oDateSpan.innerHTML = oResult.oComment.sCreated;
		oTextSpan.innerHTML = oResult.oComment.sContent;
		oTextArea.value = '';
		oCommentDiv.style.display = 'none';
		oThxDiv.style.display = 'block';
	}
	
	this.cbCommentSuccessCommentsPage = function( oResult ) {
		
		var oTextArea = document.getElementById( 'videoreview_commenttext' );
		var oCommentDiv = document.getElementById( 'videoreview_commentcontainer' );
		var oThxDiv = document.getElementById( 'videoreview_commentthx' );
		var oCountSpan = document.getElementById( 'videoreview_commentcount' );
		
		var oNavTopSpan = document.getElementById( 'nav_top_total' );
		var oNavBottomSpan = document.getElementById( 'nav_bottom_total' );
		
		var iTotal = parseInt( oCountSpan.innerHTML ) + 1;
		
		oTextArea.value = '';
		oCommentDiv.style.display = 'none';
		oThxDiv.style.display = 'block';
		oCountSpan.innerHTML = iTotal;
		oNavTopSpan.innerHTML = iTotal;
		oNavBottomSpan.innerHTML = iTotal;
		
		if( self.oParams.iStartIndex == 0 ) {
			var oContainerTbody = document.getElementById( 'videoreviews_container' );
				
			oFirstTr =  document.createElement( 'tr' );
			
			oContentTd = document.createElement( 'td' );
			oMemberTd = document.createElement( 'td' );
			oDateTd = document.createElement( 'td' );
			
			oContentTd.className = 'VRCommentCol';
			oMemberTd.className = 'VRMemberCol';
			oDateTd.className = 'VRCreatedCol';
			
			oContentTd.innerHTML = oResult.oComment.sContent;
			
			oUserA = document.createElement( 'a' );
			oUserA.href = oResult.oMember.sProfileUrl;
			oUserA.innerHTML = oResult.oMember.sLogin;
			
			oMemberTd.appendChild( oUserA );
			
			oDateTd.innerHTML = oResult.oComment.sCreated;
			
			oFirstTr.appendChild( oContentTd );
			oFirstTr.appendChild( oMemberTd );
			oFirstTr.appendChild( oDateTd );
			
			oContainerTbody.insertBefore( oFirstTr, oContainerTbody.firstChild );
			
			if( iTotal > self.iCommentsPerPage ) {
				aTrList = oContainerTbody.getElementsByTagName( 'tr' );
				oLastTr = aTrList[ aTrList.length - 1 ];
				oContainerTbody.removeChild( oLastTr );
			}
		}
	}
	
	this.cbError = function( sCommunique ) {}
}

// VideoCommentsList class - to set values on comment list
function VideoCommentsList( sPrefixId, iIndex ) {

	this.oMemberImg = document.getElementById( sPrefixId + 'member_img_' + iIndex );
	this.oMemberStatus = document.getElementById( sPrefixId + 'member_status_' + iIndex );
	this.oMemberLoginLink = document.getElementById( sPrefixId + 'member_login_link_' + iIndex );
	this.oDate = document.getElementById( sPrefixId + 'date_' + iIndex );
	this.oText = document.getElementById( sPrefixId + 'text_' + iIndex );
}
// VideoCommentsList prototype
VideoCommentsList.prototype = {
	oMemberImg : null,
	oMemberStatus : null,
	oMemberLoginLink : null,
	oDate : null,
	oText : null
}

//VideoComment class
VideoComment.prototype = {
	
	oStringTable : new Object(),
	iMaxLength : null,
	iCommentsPerPage : null,
	iCurrentCommentsOnPage : null,
	
	setMaxLength : function( iMaxLength ) {
		this.iMaxLength = iMaxLength;
	},
	
	setCommentsPerPage : function( iCommentsPerPage ) {
		this.iCommentsPerPage = iCommentsPerPage;
	},
	
	setCurrentCommentsOnPage : function( iCurrentCommentsOnPage ) {
		this.iCurrentCommentsOnPage = iCurrentCommentsOnPage;
	},
	
	addToStringTable : function( oStringTable ) {
		for( var sProperty in oStringTable ) {
			this.oStringTable[ sProperty ] = oStringTable[ sProperty ];
		}
	},
	
	checkLength : function() {
		var oTextArea = document.getElementById( 'videoreview_commenttext' );
		
		if( oTextArea.value.length >  this.iMaxLength ) {
			oTextArea.value = oTextArea.value.substr( 0, this.iMaxLength );
		}
		
	},
	
	isNotEmpty : function() {
		var oTextArea = document.getElementById( 'videoreview_commenttext' );
		var bResult = false;
			
		if( oTextArea.value.length > 0 ) {
			bResult = true;
		} else if( this.oStringTable.cannot_post_empty_comments ) {
			alert( this.oStringTable.cannot_post_empty_comments );
		}
		return bResult;
	},
	
	comment : function() {
		var oTextArea = document.getElementById( 'videoreview_commenttext' );
		
		var oParams	= new Object();
		oParams.iVideoId = this.oParams.iVideoId;
		oParams.sText = oTextArea.value;
		oParams.sReferrer = this.oParams.oMemberAction.sReferrer;
		oParams.sActualPath = this.oParams.oMemberAction.sActualPath;
		oParams.bIsBingLayout = VideoViewer.bIsBingLayout;
		
		this.oAjax.onSuccess 	= this.cbCommentSuccess;
		/*this.oAjax.onLoading 	= cbError;*/
		this.oAjax.onError 	= this.cbError;
		
		this.oAjax.call( 'ajax.php', 'POST', 'VideoReviewCommentAjaxHandler', 'storeComment', oParams );
	}
}
