
var fontSizeNormal = 100;
var fontSizeSmall = 90;

$(document).ready(function() {

    $('a.increase').click(function() {
        if (fontSizeNormal <= 160) {
            fontSizeNormal += 10;
            fontSizeSmall += 10;
            $('#main').css('font-size', fontSizeNormal + '%');
            $('.small').css('font-size', fontSizeSmall + '%');
        }

        return false;
    });

    $('a.decrease').click(function() {
        if (fontSizeNormal >= 60) {
            fontSizeNormal -= 10;
            fontSizeSmall -= 10;
            $('#main').css('font-size', fontSizeNormal + '%');
            $('.small').css('font-size', fontSizeSmall + '%');
        }

        return false;
    });

    $('#consumer-link-menu').click(function() {
        $('#professional-link-menu').removeClass('current');
        $('#consumer-link-menu').addClass('current');
        $('#search-professional').hide();
        $('#search-consumer').show();

        return false;
    });

    $('#consumer-link-text').click(function() {
        $('#professional-link-menu').removeClass('current');
        $('#consumer-link-menu').addClass('current');
        $('#search-professional').hide();
        $('#search-consumer').show();

        return false;
    });

    $('#professional-link-menu').click(function() {
        $('#consumer-link-menu').removeClass('current');
        $('#professional-link-menu').addClass('current');
        $('#search-consumer').hide();
        $('#search-professional').show();

        return false;
    });

    $('#professional-link-text').click(function() {
        $('#consumer-link-menu').removeClass('current');
        $('#professional-link-menu').addClass('current');
        $('#search-consumer').hide();
        $('#search-professional').show();

        return false;
    });

    $('#search-professional-input').focus(function() {
    	$(this).val('');
    });

    $('#search-professional-input').blur(function() {
        //$(this).val('Your search text here');
    });

    $('#search-consumer-input').focus(function() {
    	$(this).val('');
    });

    $('#search-consumer-input').blur(function() {
        //$(this).val('Your search text here');
    });

    // LISTING FUNCTIONS

    $('#listing-num').click(function() {
        //$('#listing-am').addClass('current');
        //$('#listing-nz').removeClass('current');
        //$('#listing-am-buttons').show();
        //$('#listing-nz-buttons').hide();
    	var url = window.location.href;
    	url = url.replace('&prod=' + $('#prod-previous').val(), '');
    	if (url.indexOf('?') <= 0) url = url + '?';
    	window.location.href = url + '&prod=0';

        return false;
    });

    $('#listing-am').click(function() {
        //$('#listing-am').addClass('current');
        //$('#listing-nz').removeClass('current');
        //$('#listing-am-buttons').show();
        //$('#listing-nz-buttons').hide();
    	var url = window.location.href;
    	url = url.replace('&prod=' + $('#prod-previous').val(), '');
    	if (url.indexOf('?') <= 0) url = url + '?';
    	window.location.href = url + '&prod=a';

        return false;
    });

    $('#listing-nz').click(function() {
        //$('#listing-am').removeClass('current');
        //$('#listing-nz').addClass('current');
        //$('#listing-am-buttons').hide();
        //$('#listing-nz-buttons').show();
    	var url = window.location.href;
    	url = url.replace('&prod=' + $('#prod-previous').val(), '');
    	if (url.indexOf('?') <= 0) url = url + '?';
    	window.location.href = url + '&prod=n';

        return false;
    });

    $('#listing-num-buttons a').each(function() {
    	$(this).click(function() {
    		var url = window.location.href;
    		url = url.replace('&prod=' + $('#prod-previous').val().toLowerCase(), '');
        	url = url.replace('&page=' + $('#results-paging-page-previous').val().toLowerCase(), '');
        	if (url.indexOf('?') <= 0) url = url + '?';
        	window.location.href = url + '&prod=' + $(this).html().toLowerCase();
        	/*
            $('#listing-am-buttons a').each(function() {
                $(this).removeClass('current');
            });
            $('#listing-nz-buttons a').each(function() {
                $(this).removeClass('current');
            });
            $(this).addClass('current');

            if ($(this).html() == 'H') {
                $('#listing-results').show();
                $('#listing-results-paging').show();
            } else {
                $('#listing-results').hide();
                $('#listing-results-paging').hide();
            }
        	*/
        	
            return false;
        });
    });

    $('#listing-am-buttons a').each(function() {
    	$(this).click(function() {
    		var url = window.location.href;
    		url = url.replace('&prod=' + $('#prod-previous').val().toLowerCase(), '');
        	url = url.replace('&page=' + $('#results-paging-page-previous').val().toLowerCase(), '');
        	if (url.indexOf('?') <= 0) url = url + '?';
        	window.location.href = url + '&prod=' + $(this).html().toLowerCase();
        	/*
            $('#listing-am-buttons a').each(function() {
                $(this).removeClass('current');
            });
            $('#listing-nz-buttons a').each(function() {
                $(this).removeClass('current');
            });
            $(this).addClass('current');

            if ($(this).html() == 'H') {
                $('#listing-results').show();
                $('#listing-results-paging').show();
            } else {
                $('#listing-results').hide();
                $('#listing-results-paging').hide();
            }
        	*/
        	
            return false;
        });
    });

    $('#listing-nz-buttons a').each(function() {
        $(this).click(function() {
        	var url = window.location.href;
        	url = url.replace('&prod=' + $('#prod-previous').val().toLowerCase(), '');
        	url = url.replace('&page=' + $('#results-paging-page-previous').val().toLowerCase(), '');
        	if (url.indexOf('?') <= 0) url = url + '?';
        	window.location.href = url + '&prod=' + $(this).html().toLowerCase();
        	/*
        	$('#listing-am-buttons a').each(function() {
                $(this).removeClass('current');
            });
            $('#listing-nz-buttons a').each(function() {
                $(this).removeClass('current');
            });
            $(this).addClass('current');

            if ($(this).html() == 'H') {
                $('#listing-results').show();
                $('#listing-results-paging').show();
            } else {
                $('#listing-results').hide();
                $('#listing-results-paging').hide();
            }
			*/
        	return false;
        });
    });

    $('#listing-results-cmi tr').each(function() {
        if ($(this).attr('class') == 'row-header') {
            $(this).show();
            $(this).css('cursor', 'pointer');

            $(this).click(function() {
                $('#listing-results-cmi tr').each(function() {
                    $(this).removeClass('selected');
                    if ($(this).attr('class') == 'row-data') {
                        $(this).hide();
                    }
                });

                $(this).parent().parent().parent().parent().addClass('selected');
                $(this).parent().children('tr .row-data').show();
            });
        }

        if ($(this).attr('class') == 'row-data') {
            $(this).hide();
        }
    });

    $('#listing-results-cmi td.td').each(function() {
        $(this).hide();
    });
    
    $('#listing-results-pi tr').each(function() {
        if ($(this).attr('class') == 'row-header') {
            $(this).show();
            $(this).css('cursor', 'pointer');
        }

        if ($(this).attr('class') == 'row-data') {
            $(this).hide();
        }
    });
    
    $('#listing-results tr').each(function() {
        if ($(this).attr('class') == 'row-header-cmi' || $(this).attr('class') == 'row-header-pi') {
            $(this).show();
            $(this).css('cursor', 'pointer');
            
            if ($(this).attr('class') == 'row-header-cmi') {
	            $(this).click(function() {
	                $('#listing-results tr').each(function() {
	                    $(this).removeClass('selected');
	                    if ($(this).attr('class') == 'row-data') {
	                        $(this).hide();
	                    }
	                });
	
	                $(this).parent().parent().parent().parent().addClass('selected');
	                $(this).parent().children('tr .row-data').show();
	            });
            }
        }

        if ($(this).attr('class') == 'row-data') {
            $(this).hide();
        }
    });

    $('#listing-results td.td').each(function() {
        $(this).hide();
    });
    
    // PAGING
    $('#results-paging-page').blur(function() {
    	if (!isNaN($(this).val())) {
    		var url = window.location.href;
    		url = url.replace('&page=' + $('#results-paging-page-previous').val(), '');
    		if (url.indexOf('?') <= 0) url = url + '?';
        	window.location.href = url + '&page=' + $(this).val();
    	}
    });
    
    $('#results-paging-pagesize').change(function() {
    	var url = window.location.href;
    	url = url.replace('&pagesize=' + $('#results-paging-pagesize-previous').val(), '');
    	url = url.replace('&page=' + $('#results-paging-page-previous').val(), '');
    	if (url.indexOf('?') <= 0) url = url + '?';
    	window.location.href = url + '&pagesize=' + $(this).val();
    });
    
    // AUTOCOMPLETE
    
    function findValue(li, type) {
    	if (type == 'cmi') {    	
    		$('#search-consumer-submit').focus();
        } else if (type == 'pi') {
    		$('#search-professional-submit').focus();
        }
    }

    function selectItem_CMI(li) {
    	findValue(li, 'cmi');
    }

    function selectItem_PI(li) {
    	findValue(li, 'pi');
    }

    function formatItem(row) {
    	return row[0];
    }
    
    function formatItem_PI(row) {
    	return '<strong>' + row[0] + ':</strong> ' + row[2];
    }
    
    $('#search-consumer-input').autocomplete(
    	'autocomplete_cmi.cfm',
    	{
	    	delay:10,
	    	minChars:1,
	    	matchSubset:1,
	    	matchContains:1,
	    	maxItemsToShow:20,
	    	cacheLength:10,
	    	onItemSelect:selectItem_CMI,
	    	onFindValue:findValue,
	    	formatItem:formatItem,
	    	autoFill:true
    	}
    );
    
    $('#search-professional-input').autocomplete(
       	'autocomplete_pi.cfm',
       	{
        	delay:10,
        	minChars:1,
        	matchSubset:1,
        	matchContains:1,
        	maxItemsToShow:20,
	    	cacheLength:10,
        	onItemSelect:selectItem_PI,
        	onFindValue:findValue,
        	formatItem:formatItem_PI,
        	autoFill:true
       	}
    );
    
    //checks for empty field
    BlankCheck = function(fieldId) {
    	if ($('#' + fieldId).val() == '' || $('#' + fieldId).val() == 'Your search text here') {
    		return false;
    	}
    }

});
