/**
 * @author		Chris Deemer
 * @date		5/20/2009
 * @description	-this script handles validation of product selections in the add to bag process
 * 				-implementation on specific pages requires instantiation of Product "class" with DOM obj as param which is the closest common parent of product options
 * 				-implementation also requires binding onclick event to the add to bag button and/or select box to check for validation
 */

/*TO BE REMOVED: start of sample implementation on PDP - implementation of the PDP object should be in separate file for each unique implementation */
var prod1;
$().ready(function(){
	prod1 = new fos.ProductError($('.productOptions').get(0), [$('#addToBag img').get(0)]);
});
/* end of sample*/

if (typeof(fos) == "undefined") {
    var fos = {};
}


//start product
fos.ProductError = function(context, buttons, index, selectBox){ //context is common parent of product options
    this.pw = context;
    this.optionsClass = 'pOption'+index;
	this.btns = buttons;
	this.btnsDisabledUrl = 'images/productdisplay/Btn_addtobag_r.gif';
	this.btnsEnabledUrl = 'images/productdisplay/Btn_addtobag.gif';
	this.selbox = selectBox;
    this.options = this.getOptions();
};

fos.ProductError.prototype.getOptions = function(){
	var opts = [];
    var $opts = $(this.pw).find('.' + this.optionsClass).get();
    var ol = $opts.length;
    for (var i = 0; i < ol; ++i) {
        opts.push(new fos.ProductOptionError($opts[i], this));
    }
    return opts;
};

fos.ProductError.prototype.Validate = function(updateNotifs){ //updateNotifs is a bool param used to indicate if notifications should be updated
	var ol = this.options.length;
	var valid = true;
	return valid;
};

//start option object
fos.ProductOptionError = function(context, prodObj){ //context is main div wrapper around options including label
	var _this = this; //cached obj ref for scope changes and closures for events
    this.pow = context;
    this.oProd = prodObj;
    this.notifImgPath = '/wcsstore/Fossil/images/en_US/form_validation/selectarrowLeft.png';
    this.subopClass = 'pSubOp';
    this.subopSelectedClass = 'selected';
    this.subopDisabledClass = 'disabled';
    this.label = $(this.pow).find('label');
    this.notification;
	
};

fos.ProductOptionError.prototype.hasSelection = function(){ //look for Selected options return true if found
    return $(this.pow).find('.' + this.subopSelectedClass).length == 0 ? false : true;
};

fos.ProductOptionError.prototype.showErrorNotification = function(){
	this.hideErrorNotification();
    var iHTML = '<img alt="select" src="';
    iHTML += this.notifImgPath;
    iHTML += '" style="display:none;" />';
    var $tt = $(iHTML);
    this.notification = $tt;
    $('#content_div').append($tt);
    var $target = $(this.label);
    var wrapper = $('#content_div').position();
    var targPos = $target.position();
    var targHeight = $target.height();
    var targWidth = $target.outerWidth();
    var tipWidth = $tt.width();
    var tipHeight = $tt.height();
    
    var Top = targPos.top - Math.abs( (targHeight/2) - (tipHeight/2) );
    var Left = targPos.left  + targWidth + 5;
    $tt.css({
		position: 'absolute',
        left: Left,
        top: Top,
		display: 'block'
    });
	this.label.addClass('error');
};

fos.ProductOptionError.prototype.hideErrorNotification = function(){
    if (this.notification == null) 
        return;
    this.notification.remove();
    this.notification = null;
	this.label.removeClass('error');
};
