/* custom file made by denis */
//
// TOP MENU TABS
//
function fnc_tab_mouseover(tab)
{
	if (tab.className == 'top-menu-tab-active') return;
	tab.className = 'top-menu-tab-inactiveH';
}
function fnc_tab_mouseout(tab)
{
	if (tab.className == 'top-menu-tab-active') return;
	tab.className = 'top-menu-tab-inactive';
}

//
// BUTTOM 'STANDARDS' IMAGES
//
function fnc_td_mouseover(td)
{
	td.style.borderColor = 'FFD50F';
}
function fnc_td_mouseout(td)
{
	td.style.borderColor = '2D598D';
}

//
// ADD TO CART BUTTON
//
// placed all standard functionality of add to cart button to that function
// and added extra : set hidden input field value
function fnc_on_addtocart(product_id, is_ajax, tag)
{
	// check product options combination exceptions
	// if there's exception, exit
	// if no exception has been met, then add product to cart
	var check_result = fn_check_exceptions(product_id, tag)
	if (!check_result) {
		alert(cannot_buy);
		return;
	}

	// set product's form's target value
	var form_name = 'product_details_form_'+product_id;
	document.forms[form_name].target.value='cart';

	// due to customisations, product form contains several instances of same product
	// therefore, all text inputs, select boxes, checkboxes, radioboxes, etc. which are not related to
	// currently selected 'Add To Cart' button must be disabled
	fnc_on_addtocart_disablefields(true);

	// if ajax allowed - send ajax request; otherwise, simply submit form
	if (is_ajax == 'Y') {
		fn_http_request('ca', fn_form_get_request(form_name) + 'ajax=cart_status');
		// if AJAX is turned off then user doesn't see page refresh, therefore, all disabled fields must be enabled back
		fnc_on_addtocart_disablefields(false);
	} else {
		document.forms[form_name].submit();
	}

	return;
}

function fnc_on_addtocart_disablefields(is_disabled)
{
	var els = document.getElementsByTagName(current_tag.tagName);
	for (var i = 0; i < els.length; i++) {
		if (els[i].id == 'echeck_parent' && els[i] != current_tag) {
			fnc_disable_childs(els[i], "INPUT", is_disabled);
			fnc_disable_childs(els[i], "TEXTAREA", is_disabled);
			fnc_disable_childs(els[i], "SELECT", is_disabled);
			fnc_disable_childs(els[i], "CHECKBOX", is_disabled);
			fnc_disable_childs(els[i], "RADIO", is_disabled);
		}
	}

	return;
}

function fnc_disable_childs(parent_tag, tag_name, is_disabled)
{
	var els = parent_tag.getElementsByTagName(tag_name);
	for (var i = 0; i < els.length; i++) {
		els[i].disabled = is_disabled;
	}

	return;
}

//
// Additional Images Advanced Display Class
//
function AdditionalImages(id, id2)
{
	this.source_id = id;
	this.background_id = id2;
	this.images = new Array();
	this.container = null;
	this.background = null;
	this.main_image = null;
	this.InitImages();

	return;
}

AdditionalImages.prototype.StopPropagation = function(e)
{
	if (e && e.stopPropagation) {
		e.stopPropagation();
	}	else if (window.event) {
		window.event.cancelBubble = true;
	}

	return;
}

// el - dom element
// e_type - event type (mouseover, mousedown, mouseup, etc...)
// e_label - function name
// e_capture - if possible to use DOM2 AddEventListener, used as 3rd parameter of that function
AdditionalImages.prototype.AddEventListener = function(el, e_type, e_label, e_capture)
{
	e_type = e_type.toLowerCase();
	e_capture = e_capture == "undefined" ? false : e_capture;

	if (el.addEventListener) {
		el.addEventListener(e_type, e_label, e_capture);
	} else if (el.attachEvent) {
		el.attachEvent('on'+e_type, e_label);// works for IE only
	} else {
		el['on'+e_type] = e_label;
	}

	return;
}

// el - dom element
// e_type - event type (mouseover, mousedown, mouseup, etc...)
// e_label - function name
// e_capture - if possible to use DOM2 AddEventListener, used as 3rd parameter of that function
AdditionalImages.prototype.RemoveEventListener = function(el, e_type, e_label, e_capture)
{
	e_type = e_type.toLowerCase();
	e_capture = e_capture == "undefined" ? false : e_capture;

	if (el.removeEventListener) {
		el.removeEventListener(e_type, e_label, e_capture);
	} else if(el.detachEvent) {
		el.detachEvent('on'+e_type, e_label);// works for IE only
	} else {
		el['on'+e_type] = null;
	}

	return;
}

AdditionalImages.prototype.InitImages = function()
{
	var src = document.getElementById(this.source_id);
	this.container = src;

	var ai_bg = document.getElementById(this.background_id);
	this.background = ai_bg;

	var images = src.getElementsByTagName("IMG");
	var img, pair_id;
	var _this = this;

	for (var i = 0; i < images.length; i++) {
		img = images[i];
		if (img.id == 'ai_main_image') {
			this.main_image = img;
		}
		else if (img.id.substr(0,18) == 'ai_detailed_image_') {
			// get pair id
			pair_id = parseInt(img.id.substr(18));
			// add image to array of images pairs
			this.AddImage('detailed', pair_id, img);
			// hide that image (if not hidden before)
			img.style.display = 'none';
		}
		else if (img.id.substr(0,14) == 'ai_icon_image_') {
			// get pair id
			pair_id = parseInt(img.id.substr(14));
			// add image to array of images pairs
			this.AddImage('icon', pair_id, img);
			// add events to image
			this.AddEventListener(img, 'mouseover', function(e){_this.MouseOverIcon(e);} );
		}
	}

	this.AddEventListener(this.container, 'mouseover', function(e){_this.StopPropagation(e);} );
	this.AddEventListener(this.background, 'mouseover', function(e){_this.MouseOverBg(e);} );

	return;
}

AdditionalImages.prototype.MouseOverBg = function(e)
{
	this.ShowPair(null);
	this.main_image.style.display = 'inline';
	return;
}

AdditionalImages.prototype.MouseOverIcon = function(e)
{
	var event = new fncEvent(e);
	var img = event.target;
	var pair_id = parseInt(img.id.substr(14));
	this.ShowPair(this.GetImagePair(pair_id));

	return;
}

AdditionalImages.prototype.ShowPair = function(visible_image_pair)
{
	var p;
	for (var i = 0; i < this.images.length; i++) {
		p = this.images[i];
		if (p != visible_image_pair) {
			p.detailed.style.display = 'none';
		}
	}

	if (visible_image_pair) {
		visible_image_pair.detailed.style.display = 'inline';
		this.main_image.style.display = 'none';
	}

	return;
}

AdditionalImages.prototype.GetImagePair = function(pair_id)
{
	var image_pair = null;

	for (var i = 0; i < this.images.length; i++) {
		if (this.images[i].pair_id == pair_id) {
			image_pair = this.images[i];
			break;
		}
	}

	return image_pair;
}

AdditionalImages.prototype.AddImage = function(img_type, pair_id, img)
{
	var img_type2 = img_type == 'icon' ? 'detailed' : 'icon';
	var pair_found = null;

	// search if paired image already in array
	if (this.images.length) {
		for (var i = 0; i < this.images.length; i++) {
			if (this.images[i].pair_id == pair_id) {
				pair_found = this.images[i];
				break;
			}
		}//for
	}//if

	if (pair_found) {
		pair_found[img_type] = img;
	} else {
		var image_pair = {};
		image_pair['pair_id'] = pair_id;
		image_pair[img_type]  = img;
		image_pair[img_type2] = null;
		this.images[this.images.length] = image_pair;
	}

	return;
}

// cross-browser event object
fncEvent = function(e)
{
	// NS&FF : IE
	var e = e ? e : window.event;
	if(!e) {
		return;
	}

	// NS&FF : IE
	this.type = e.type ? e.type : null;
	// NS&FF : IE
	this.target = e.target ? e.target : (e.srcElement ? e.srcElement : null);

	if (e.relatedTarget) {
		this.relatedTarget = e.relatedTarget;
	}	else if (e.type == 'mouseover' && e.fromElement) {
		this.relatedTarget = e.fromElement;
	} else if (e.type == 'mouseout') {
		this.relatedTarget = e.toElement;
	} else {
		this.relatedTarget = null;
	}

	return;
}
