/*
 * Title  : fvReVolusion (Volusion plugin for Power Reviews)
 * Author : Kwi Hyun Choi <masterpc@gmail.com>
 * Version: 1.0
 * Last Released: 8/5/2009
 *
 * Any comment or question to http://www.4eva.net/guestbook
 *
 * fvReVolusion is copyright(c) 2009 Kwi Hyun Choi.
 * -------------------------------------------------------------------------------
 * INSTALL:
 *   <script type="text/javascript" src="/v/revolusion/jquery-1.3.2.min.js"></script>
 *   <script type="text/javascript" src="/v/revolusion/fvRevolusion.js"></script>
 *   <script type="text/javascript" src="/v/revolusion/fvSettings.js"></script>
 *------------------------------------------------------------------------------*/

/*
 * SOURCE - DO NOT MODIFY IF YOU DO NOT KNOW WHAT YOU ARE DOING
 *------------------------------------------------------------------------------*/
function fvBasicWriter()
{
	this._id = null;
	this._className = null;
	this._foundElm = null;
	this._elm = null;
	this._style = '';

	this.init = function( id, pc, elm )
	{
		if( pc != null && elm != null )
		{
			if( fvSettings.DEBUG == true )
			{
				this._id = id + '_' + pc + Math.floor( Math.random() * 1001 )
			}
			else
			{
				this._id = id + '_' + pc;
			}

			this._className = id;
			this._foundElm = elm;
		}
		else
		{
			this._id = id;
		}

		this.createElement();
	};
	this.insertDiv = function( elm ) {};
	this.createElement = function()
	{
		if( this._className == null )
			this._elm = '<div id="' + this._id + '" class="' + this._id + '" style="' + this._style + '"></div>';
		else
			this._elm = '<div id="' + this._id + '" class="' + this._className + '" style="' + this._style + '"></div>';

		this.insertDiv( this._elm );
	};
	this.write = function( html )
	{
		$( html ).appendTo( "#" + this._id );
	};
	this.getElementById = function( id ){ return document.getElementById( id ); };
};

function fvIndexSnippetWriterBelowPrice()
{
	//this._style = 'float:left; width:300px';

	this.insertDiv = function( elm )
	{
		if(this._foundElm == null)
			return;

		$( elm ).appendTo( $( this._foundElm ).parents( 'td' ) );
	};
};
fvIndexSnippetWriterBelowPrice.prototype = new fvBasicWriter();

function fvCategorySnippetWriterBelowPrice()
{
	//this._style = 'float:left';

	this.insertDiv = function( elm )
	{
		if(this._foundElm == null)
			return;

		$( '<div style="clear:both;margin:0;"></div>' ).appendTo( $( this._foundElm ).parents( 'td' ) );
		$( elm ).appendTo( $( this._foundElm ).parents( 'td' ) );

		// might be for child
		//$( elm ).insertBefore( $( this._foundElm ).parents( 'td' ).children( 'img' ) );
	};
};
fvCategorySnippetWriterBelowPrice.prototype = new fvBasicWriter();


function fvProductSnippetWriterBelowImage()
{
	this._style = 'width:220px;';

	this.insertDiv = function( elm )
	{
		// $( "#product_photo_zoom_url" ).after( elm );
		$( elm ).insertAfter( $( "#product_photo_zoom_url" ) );
	};
};
fvProductSnippetWriterBelowImage.prototype = new fvBasicWriter();

function fvReviewWriterOnExtInfo()
{
	this.getReviewReadURL = function( pc )
	{
		return '/ProductDetails.asp?ProductCode=' + pc + '&Show=ExtInfo';
	};

	this.insertDiv = function( elm )
	{
		var tables = $( '.colors_descriptionbox' );
		if( tables == null || tables.length <= 0 )
			return;

		var reviewTable = tables[0];

		$( elm ).appendTo( $( reviewTable.rows[1].cells[1] ) );
	};
};
fvReviewWriterOnExtInfo.prototype = new fvBasicWriter();

function fvReviewWriterBelowDescription()
{
	this.getReviewReadURL = function( pc )
	{
		return '#PReviews';
	};

	this.insertDiv = function( elm )
	{
		var imgs = $( '#content_area img' );

		var targetElm = null;
		for( var i=imgs.length-1; i>=0; i-- )
		{
			if( imgs[i].src.indexOf( 'clear1x1.gif' ) != -1 && imgs[i].width == 5 && imgs[i].height == 5 )
			{
				targetElm = imgs[i];
				break;
			}
		}

		$( elm ).insertBefore( $( targetElm ).prevAll( 'b' ) );
		$( '<a name="PReviews"></a>' ).insertBefore( '#' + this._id );
		$( '<br />' ).insertAfter( '#' + this._id );
	};
};
fvReviewWriterBelowDescription.prototype = new fvBasicWriter();


var fvRevolusion = {
	_url : window.location.href.toLowerCase(),
	_pageType : null,

	parsePageType:function()
	{
		if( this._url.indexOf( '/productdetails.asp' ) != -1 || this._url.lastIndexOf( '-p/' ) != -1 || this._url.lastIndexOf( '_p/' ) != -1 )
		{
			this._pageType = 'P';
		}
		else if( this._url.indexOf( '/searchresults.asp' ) != -1 || this._url.lastIndexOf( '-s/' ) != -1 || this._url.lastIndexOf( '_s/' ) != -1 )
		{
			this._pageType = 'S';
		}
		else if( location.pathname == '/' ||location.pathname.toLowerCase().indexOf( '/default.asp' ) != -1 )
		{
			this._pageType = 'I';
		}
	},

	getProductCode:function( url )
	{
		if( fvSettings.DEBUG == true )
			return fvSettings.DEBUG_PRODUCT_CODE;

		url = url.toLowerCase();

		var productCode = "";
		var xparams = "";
		if( url.indexOf( '?' ) != -1 )
			xparams = url.substr( url.indexOf( '?' )+1 );

		if( url.indexOf( "/productdetails.asp" ) != -1 )
		{
			var params = xparams.split("&");

			for (var i=0; i<params.length; i++)
			{
				var values = params[i].split("=");

				if (values[0] == "productcode")
				{
					productCode = unescape( values[1] );

					break;
				}
			}
		}
		else if( url.indexOf("-p/") != -1 || url.indexOf("_p/") != -1 )
		{
			var pos = url.lastIndexOf( "/" ) + 1;
			var workStr = url.substr( pos );

			productCode = unescape( workStr.substr( 0, workStr.lastIndexOf( "." ) ) );
		}

		return productCode.toUpperCase();
	},

	displayOnIndex:function()
	{
		var urls = $( '#content_area a.smalltext' );
		var targets = $( '#content_area .pricecolor.colors_productprice' );
		var currIdx = 0;

		for( var i=0; i<urls.length && i<targets.length; i++ )
		{
			var productCode = fvRevolusion.getProductCode( urls[i].href );
			if( productCode.length <= 0 )
				continue;

			var imgs = $( urls[i] ).children( 'img' );
			if( imgs == null || imgs.length <= 0 )
				continue;

			if( imgs[0].src.indexOf( 'Bullet_MoreInfo.gif' ) == -1 )
				continue;

			fvSettings.SNIPPET_ON_INDEX.init( 'pr_snippet_category', productCode, targets[currIdx++] );

			POWERREVIEWS.display.snippet( fvSettings.SNIPPET_ON_INDEX, { pr_page_id:productCode, pr_snippet_min_reviews:fvSettings.SNIPPET_MIN_REVIEWS } );
		}
	},

	displayOnCategory:function()
	{
		var urls = $( '#MainForm a' );
		var targets = $( '#MainForm .pricecolor.colors_productprice' );
		var currIdx = 0;

		for( var i=0; i<urls.length && i<targets.length; i++ )
		{
			var productCode = fvRevolusion.getProductCode( urls[i].href );
			if( productCode.length <= 0 )
				continue;

			var imgs = $( urls[i] ).children( 'img' );
			if( imgs == null || imgs.length <= 0 )
				continue;

			if( imgs[0].src.indexOf( 'Bullet_MoreInfo.gif' ) != -1 )
				continue;

			// NECESSARY ONLY FOR DEBUG MODE
			if( imgs[0].src.indexOf( 'btn_addtocart_small.gif' ) != -1 )
				continue;

			fvSettings.SNIPPET_ON_CATEGORY.init( 'pr_snippet_category', productCode, targets[currIdx++] );

			POWERREVIEWS.display.snippet( fvSettings.SNIPPET_ON_CATEGORY, { pr_page_id:productCode, pr_snippet_min_reviews:fvSettings.SNIPPET_MIN_REVIEWS } );
		}
	},

	displayOnProduct:function()
	{
		var productCode = fvRevolusion.getProductCode( fvRevolusion._url );
		if( productCode.length <= 0 )
			return;

		var pr_write_review = '/SearchResults.asp?Cat=' + fvSettings.CATEGORY_NO_WRITE + '&pr_page_id=' + productCode;
		var pr_read_review = fvSettings.REVIEW_ON_PRODUCT.getReviewReadURL( productCode );

		fvSettings.REVIEW_ON_PRODUCT.init( 'pr_review_summary' );
		if( fvSettings.REVIEW_ON_PRODUCT instanceof fvReviewWriterOnExtInfo )
		{
			if( fvRevolusion._url.lastIndexOf( '&show=extinfo' ) != -1 )
			{
				POWERREVIEWS.display.engine( fvSettings.REVIEW_ON_PRODUCT, { pr_page_id:productCode, pr_read_review:pr_read_review, pr_write_review:pr_write_review } );
			}
		}
		else
		{
			POWERREVIEWS.display.engine( fvSettings.REVIEW_ON_PRODUCT, { pr_page_id:productCode, pr_read_review:pr_read_review, pr_write_review:pr_write_review } );
		}

		fvSettings.SNIPPET_ON_PRODUCT.init( 'pr_snippet_product' );

		POWERREVIEWS.display.snippet( fvSettings.SNIPPET_ON_PRODUCT, { pr_page_id:productCode, pr_read_review:pr_read_review, pr_write_review:pr_write_review } );
	},

	main:function()
	{
		if( fvSettings.MERCHANT_ID == null || fvSettings.MERCHANT_ID.length <= 0 )
			return;
		if( fvSettings.MERCHANT_CSS == null || fvSettings.MERCHANT_CSS.length <= 0 )
			return;
		if( fvSettings.CATEGORY_NO_WRITE == null || fvSettings.CATEGORY_NO_WRITE.length <= 0 )
			return;

		this.parsePageType();
		if( fvSettings.DEBUG == true )
			this._pageType = fvSettings.DEBUG_TARGET;

		if( this._pageType )
		{
			document.write( '<scr'+'ipt type="text/javascript" src="http://cdn.powerreviews.com/repos/' + fvSettings.MERCHANT_ID + '/pr/pwr/engine/js/full.js'+'"></scr'+'ipt>' );
			document.write( '<link id="prBaseStylesheet" type="text/css" rel="stylesheet" href="'+'http://cdn.powerreviews.com/repos/' + fvSettings.MERCHANT_ID + '/pr/pwr/engine/pr_styles_review.css" />');
			document.write( '<link id="prMerchantOverrideStylesheet" type="text/css" rel="stylesheet" href="' + fvSettings.MERCHANT_CSS + '" />');

			if( this._pageType == 'P' )
				$( document ).ready( fvRevolusion.displayOnProduct );
			else if( this._pageType == 'S' )
				$( document ).ready( fvRevolusion.displayOnCategory );
			else if( this._pageType == 'I' )
				$( document ).ready( fvRevolusion.displayOnIndex );
		}
	}
};
