function FilterItem(item, itemTypes) {
	this.mItem = item;
	this.mItemTypes = itemTypes;
}

FilterItem.prototype.Match = function(itemType) {
    if (itemType.length == 0) {
        return true;
    } else {
	    return (this.mItemTypes.indexOf(';'+itemType+';') > -1);
	}
}

FilterItem.prototype.IsFiltered = function(filter) {
    return (this.mItem.className == filter.mShowItemCSS);
}


function SelectedFilter(filterItemID, filterType) {
	this.mFilterItemID = filterItemID;
	this.mFilterType = filterType;	
}


function Filter()
{
    this.mChildFilters = new Array();
    this.mFilters = new Array();
    this.mSelectedFilters = new Array();
    this.mFilterMenuItems = new Array();
    this.mParentFilter = null;

    this.mfnHoverHook = function(sender, hovering) {
        return(false);
    };
    
    this.mFilterPrefix = '';
    this.mFilterTypesCSS = '';
    this.mHideItemCSS = '';
    this.mMenuElementID = '';
    this.mNoFilterElementID = '';
    this.mNoFilterHideCSS = '' ;
    this.mNoFilterShowCSS = '' ;
    this.mSelectHeaderCSS = '';
    this.mSelectHeaderHoverCSS = '';
    this.mShowItemCSS = '';
    this.mUnselectHeaderCSS = '';
    this.mUnselectHeaderHoverCSS = '';
}

Filter.prototype.AddChild = function(childFilter, parentAdded) {
    this.mChildFilters.push(childFilter);
    if ( !parentAdded )
        childFilter.AddParent(this,true);
}

Filter.prototype.AddParent = function(parentFilter, childAdded) {
    this.mParentFilter = parentFilter;
    if ( !childAdded )
        parentFilter.AddChild(this,true);
}

Filter.prototype.Initialise = function(showItemCSS, hideItemCSS, 
        selectHeaderCSS, selectHeaderHoverCSS, unselectHeaderCSS, unselectHeaderHoverCSS,
        filterTypesCSS, menuElementID, filterPrefix, 
        noFilterElementID, noFilterShowCSS, noFilterHideCSS) {
        
    this.mFilterPrefix = filterPrefix;
    this.mFilterTypesCSS = filterTypesCSS;
    this.mHideItemCSS = hideItemCSS;
    this.mMenuElementID = menuElementID;
    this.mNoFilterElementID = noFilterElementID;
    this.mNoFilterHideCSS = noFilterHideCSS;
    this.mNoFilterShowCSS = noFilterShowCSS;
    this.mSelectHeaderCSS = selectHeaderCSS;
    this.mSelectHeaderHoverCSS = selectHeaderHoverCSS;
    this.mShowItemCSS = showItemCSS;
    this.mUnselectHeaderCSS = unselectHeaderCSS;
    this.mUnselectHeaderHoverCSS = unselectHeaderHoverCSS;

    var items = document.getElementsByTagName('span');
    var filter;
    for (var i = 0 ; i < items.length ; i++) {
        if (items[i].className == this.mFilterTypesCSS) {
            filter = new FilterItem(items[i].parentNode,items[i].innerHTML);
			this.mFilters.push(filter);
        }
    }
	
	itemHTML = document.getElementById(this.mMenuElementID);
	if ( itemHTML ) {
		var prefixLen = this.mFilterPrefix.length;
		this.LocateMenuEntries(itemHTML,prefixLen);
	}
}

Filter.prototype.LocateMenuEntries = function(itemHTML,prefixLen) {
	if ( itemHTML ) {
		var nodes;
		
		if ( itemHTML.children ) {
			nodes = itemHTML.children;
		} else {
			nodes = itemHTML.childNodes;
		}
		
		for ( var i = 0 ; i < nodes.length; i++ ) {
			if ( nodes[i].id ) {
				if ( (nodes[i].id.length >= prefixLen) && (nodes[i].id.substr(0,prefixLen) == this.mFilterPrefix) )
					this.mFilterMenuItems.push(nodes[i].id);
			}
			this.LocateMenuEntries(nodes[i],prefixLen);
		}
	}
}


Filter.prototype.ChangeState = function(sender, itemID, trackingURL) {
	var tag;
	var tagID;
	tagID = this.mFilterPrefix+itemID;
	for (var i=0; i < this.mFilterMenuItems.length; i++) {
		tag = document.getElementById(this.mFilterMenuItems[i])
		if (tag){
			if(tag.id == tagID) {
				tag.className=this.mSelectHeaderCSS;
			}else{
				tag.className=this.mUnselectHeaderCSS;
			}
		}
	}
	this.SetFilter([itemID]);
	TrackURL(trackingURL);
}

Filter.prototype.ToggleState = function (sender,itemID) {
	var tag;
	tag=document.getElementById(this.mFilterPrefix+itemID);
	if (tag) {
		switch (tag.className)
		{
		    case this.mUnselectHeaderCSS:
		    case this.mUnselectHeaderHoverCSS:
    			tag.className=selectHeaderCSS;
	    		this.AddFilter(itemID);
	    		break;
	    		
	    	case this.mSelectHeaderCSS:
	    	case this.mSelectHeaderHoverCSS:
    			tag.className=unselectHeaderCSS;
	    		this.RemoveFilter(itemID);
		}
	}	
}

Filter.prototype.AddFilter = function(filterID) {
	this.mSelectedFilters.push(filterID);
	this.FilterItems();
}

Filter.prototype.RemoveFilter = function(filterID) {
	var updatedFilters = new Array(0);
	for (var i = 0 ; i < this.mSelectedFilters.length ; i++) {
		if (this.mSelectedFilters[i] != filterID) {
			updatedFilters.push(this.mSelectedFilters[i])
		}
	}
	this.mSelectedFilters = updatedFilters;
	this.FilterItems();
}

Filter.prototype.SetFilter = function (filterList) {
	this.mSelectedFilters = filterList;
	this.FilterItems();
}

Filter.prototype.FilterItems = function(selective) {
	var s = new String();
	var tag;

	for ( var i = 0 ; i < this.mSelectedFilters.length ; i++ ) {
		s += ';' + this.mSelectedFilters[i];
	}
	if ( s.length > 0 )
	    s += ';';
	tag = document.getElementById('debug');
	if ( tag )
	    tag.innerHTML=s;

	for ( var i = 0 ; i < this.mFilters.length; i++ ) {
		var matched = true;

        if ( !selective || this.mFilters[i].IsFiltered(this) ) {
		    for (var j = 0 ; j < this.mSelectedFilters.length ; j++) {
			    if (!this.mFilters[i].Match(this.mSelectedFilters[j])) {
				    matched = false;
				    break;
			    }
			}
		    if (matched) {
			    this.mFilters[i].mItem.className = this.mShowItemCSS;
		    } else {
			    this.mFilters[i].mItem.className = this.mHideItemCSS;
		    }
		}
	}
	
	if ( !selective )
	{
	    this.FilterDown();
	    this.FilterUp();
	}
	
	if ( this.mNoFilterElementID.length > 0 ) {
	    var count = 0;

	    for ( var i = 0 ; i < this.mFilters.length; i++ ) {
            if ( this.mFilters[i].IsFiltered(this) )
                count++;
	    }
	    
        tag = document.getElementById(this.mNoFilterElementID);
        if ( tag ) {
            if ( count == 0 ) {
                tag.className = this.mNoFilterShowCSS;
            } else {
                tag.className = this.mNoFilterHideCSS;
            }
        }
    }
}

Filter.prototype.Hover = function (sender, hovering) {
    var newCSS;

    if ( !this.mfnHoverHook(sender, hovering) )
    {
        if ( hovering ) {
            switch( sender.className ) {
                case this.mSelectHeaderCSS:
                case this.mSelectHeaderHoverCSS:
                    newCSS = this.mSelectHeaderHoverCSS;
                    break;
                    
                case this.mUnselectHeaderCSS:
                case this.mUnselectHeaderHoverCSS:
                    newCSS = this.mUnselectHeaderHoverCSS;
                    break;
                    
                default:
                    newCSS = sender.className;
                    break;
            }
        } else {
            switch(sender.className) {
                case this.mSelectHeaderCSS:
                case this.mSelectHeaderHoverCSS:
                    newCSS = this.mSelectHeaderCSS;
                    break;
                    
                case this.mUnselectHeaderCSS:
                case this.mUnselectHeaderHoverCSS:
                    newCSS = this.mUnselectHeaderCSS;
                    break;
                    
                default:
                    newCSS = sender.className;
                    break;
            }
        }
        sender.className = newCSS;
    }
}

Filter.prototype.FilterDown = function() {
    for ( var i = 0; i < this.mChildFilters.length; i++ ) {
        this.mChildFilters[i].FilterItems(true);
        this.mChildFilters[i].FilterDown();
    }
}

Filter.prototype.FilterUp = function() {
    if ( this.mParentFilter ) {
        this.mParentFilter.FilterItems(true);
        this.mParentFilter.FilterUp();
    }
}

DealFilter.prototype = new Filter();
DealFilter.prototype.constructor = DealFilter();

function DealFilter() {
	this.mDealsArray = new Array();
	this.mDealsBestDealsType = 2;
	this.mDealsCount = 0;
	this.mDealsName;
	this.mDealsReseller = '';
	this.mDealsStart = 1;
	this.mDealsState = {top_deal_type: 1, reseller_id: -1, minutes: -1, texts: -1, monthly_cost: -1, download_amount: -1, free_gifts: -1, handset_cost: -1};
	this.mDealsTimer;
}

DealFilter.prototype.AddDeals = function (pDealsArray) {
	for (i in pDealsArray) {
		if (pDealsArray[i][0] != undefined)
			this.mDealsArray.push({product_deal_id: pDealsArray[i][0], top_deal_type: pDealsArray[i][1], reseller_id: pDealsArray[i][2], minutes: pDealsArray[i][3], texts: pDealsArray[i][4], monthly_cost: pDealsArray[i][5], download_amount: pDealsArray[i][6], free_gifts: pDealsArray[i][7], handset_cost: pDealsArray[i][8]});
	}
}

DealFilter.prototype.FilterDeals = function () {
	this.mDealsCount = 0;
	for (i in this.mDealsArray) {
		if (
			(this.mDealsState.reseller_id == -1 && this.mDealsArray[i].top_deal_type >= this.mDealsBestDealsType)
			|| (this.mDealsState.reseller_id == this.mDealsArray[i].reseller_id && this.mDealsState.top_deal_type > 0 && this.mDealsArray[i].top_deal_type > 0)
			|| ((this.mDealsState.top_deal_type == 0 || this.mDealsState.reseller_id == 0)
				&& (this.mDealsState.reseller_id == this.mDealsArray[i].reseller_id || this.mDealsState.reseller_id == 0)
				&& (this.mDealsState.minutes <= this.mDealsArray[i].minutes || this.mDealsState.minutes == -1)
				&& (this.mDealsState.texts <= this.mDealsArray[i].texts || this.mDealsState.texts == -1)
				&& (this.mDealsState.monthly_cost >= this.mDealsArray[i].monthly_cost || this.mDealsState.monthly_cost == -1)
				&& (this.mDealsState.download_amount < this.mDealsArray[i].download_amount || this.mDealsState.download_amount == -1)
				&& (this.mDealsState.free_gifts < this.mDealsArray[i].free_gifts || this.mDealsState.free_gifts == -1)
				&& (this.mDealsState.handset_cost >= this.mDealsArray[i].handset_cost || this.mDealsState.handset_cost == -1)
			)
		) {
			this.mDealsCount++;
			if (this.mDealsCount >= this.mDealsStart && this.mDealsCount < this.mDealsStart + 10) {
				if ((this.mDealsState.reseller_id <= 0 && this.mDealsArray[i].top_deal_type > this.mDealsBestDealsType)
						|| (this.mDealsState.reseller_id > 0 && this.mDealsArray[i].top_deal_type >= this.mDealsBestDealsType))
					$('#DPDL' + this.mDealsArray[i].product_deal_id).attr('class', this.mShowItemCSS + '3');
				else if (this.mDealsCount % 2 == 0)
					$('#DPDL' + this.mDealsArray[i].product_deal_id).attr('class', this.mShowItemCSS);
				else
					$('#DPDL' + this.mDealsArray[i].product_deal_id).attr('class', this.mShowItemCSS + '2');
			}
			else
				$('#DPDL' + this.mDealsArray[i].product_deal_id).attr('class', this.mHideItemCSS);
		}
		else
			$('#DPDL' + this.mDealsArray[i].product_deal_id).attr('class', this.mHideItemCSS);
	}
	$('#deal_finder_inline #deal_finder_info .number').html(this.mDealsCount);
	$('#deal_finder_inline #deal_finder_info .text').html('Deal' + (this.mDealsCount != 1 ? 's' : '') + ' Found');
	if (this.mDealsCount == 0) {
		$('.detailbottomboxtitle').hide();
		if (this.mDealsState.reseller_id == -1)
			$('#deal_finder_inline_empty').html('<b>No best deals found</b><br />Try using the tabs above to browse through all available deals');
		else
			$('#deal_finder_inline_empty').html('<b>No deals found</b><br />Try changing the options above, or click the <b>reset</b> button to show all deals');
		$('#deal_finder_inline_empty').show();
		$('.deal_finder_reset').addClass('deal_finder_reset_empty');
	}
	else {
		$('.detailbottomboxtitle').show();
		$('#deal_finder_inline_empty').hide();
		$('.deal_finder_reset').removeClass('deal_finder_reset_empty');
	}

	if (this.mDealsStart > 10)
		$('#deal_finder_pager_l').css('display','inline-block');
	else
		$('#deal_finder_pager_l').hide();
	
	if (this.mDealsCount >= (this.mDealsStart + 10)) {
		$('#deal_finder_pager_r').html('Next <b>' + (this.mDealsCount - this.mDealsStart - 9 >= 10 ? 10 : this.mDealsCount - this.mDealsStart - 9) + '</b> deal' + (this.mDealsCount - this.mDealsStart - 9 != 1 ? 's' : '') + ' <b>&raquo;</b>');
		$('#deal_finder_pager_r').css('display','inline-block');
	}
	else
		$('#deal_finder_pager_r').hide();
}

DealFilter.prototype.InternalName = function(pDealsName) {
	this.mDealsName = pDealsName;
}

DealFilter.prototype.PageDeals = function (pageDir) {
	if (pageDir == undefined)
		return;
	this.mDealsStart = this.mDealsStart + (pageDir * 10);
	this.FilterDeals();
}

DealFilter.prototype.ResetFilter = function() {
	this.UpdateFilter({minutes: -1, texts: -1, monthly_cost: -1, download_amount: -1, free_gifts: -1, handset_cost: -1}, true, false);
}

DealFilter.prototype.SetBestDealsType = function (top_deal_type) {
	if (top_deal_type != undefined)
		this.mDealsBestDealsType = top_deal_type;
}

DealFilter.prototype.SetFilter = function (itemID) {
	if (itemID == undefined)
		return;
	var rid = itemID[0].replace(/DEAL/, '');
	switch (rid) {
		case 'BEST':
			this.UpdateFilter({reseller_id: -1}, true, false);
			this.mDealsReseller = '';
			break;
		case 'ALL':
		case 'OTHERS':
			this.UpdateFilter({reseller_id: 0}, true, false);
			this.mDealsReseller = '';
			break;
		default:
			this.UpdateFilter({top_deal_type: (this.mDealsState.top_deal_type == 0 ? 0 : 1), reseller_id: parseInt(rid)}, true, false);
			this.mDealsReseller = $('#FDEAL' + rid + ' .deal_tabs_text').html();
	}
	this.UpdateFinder();
}

DealFilter.prototype.ToggleList = function () {
	if (this.mDealsState.top_deal_type == 0)
		this.UpdateFilter({top_deal_type: 1}, true, false);
	else
		this.UpdateFilter({top_deal_type: 0}, true, false);
	this.UpdateFinder();
}

DealFilter.prototype.UpdateFilter = function (pDealsState, pRefresh, pDelay) {
	if (pDealsState == undefined)
		return;
	if (pDealsState.top_deal_type != undefined)
		this.mDealsState.top_deal_type = pDealsState.top_deal_type;
	if (pDealsState.reseller_id != undefined)
		this.mDealsState.reseller_id = pDealsState.reseller_id;
	if (pDealsState.minutes != undefined)
		this.mDealsState.minutes = pDealsState.minutes;
	if (pDealsState.texts != undefined)
		this.mDealsState.texts = pDealsState.texts;
	if (pDealsState.monthly_cost != undefined)
		this.mDealsState.monthly_cost = pDealsState.monthly_cost;
	if (pDealsState.download_amount != undefined)
		this.mDealsState.download_amount = pDealsState.download_amount;
	if (pDealsState.free_gifts != undefined)
		this.mDealsState.free_gifts = pDealsState.free_gifts;
	if (pDealsState.handset_cost != undefined)
		this.mDealsState.handset_cost = pDealsState.handset_cost;

	this.mDealsStart = 1;
	clearTimeout(this.mDealsTimer);
	if (pRefresh && pDelay)
		this.mDealsTimer = eval('setTimeout("' + this.mDealsName + '.FilterDeals();", 200)');
	else if (pRefresh)
		this.FilterDeals();
}

DealFilter.prototype.UpdateFinder = function () {
	if ((this.mDealsState.top_deal_type == 0 && this.mDealsState.reseller_id > -1) || this.mDealsState.reseller_id == 0) {
		$('#deal_finder_inline').show();
		$('#deal_finder_toggle').html('Click here to see only the <b>top</b> deals' + (this.mDealsReseller != '' ? ' for ' + this.mDealsReseller : ''));
	}
	else {
		$('#deal_finder_inline').hide();
		$('#deal_finder_toggle').html('Click here to see <b>all</b> deals' + (this.mDealsReseller != '' ? ' for ' + this.mDealsReseller : ''));
	}
	if (this.mDealsState.reseller_id <= 0)
		$('#deal_finder_toggle').hide();
	else
		$('#deal_finder_toggle').show();
}
