function _toggleModuleItem(module, item_id)
{
    module.children('.item').each(function() {
        if ('#' + $(this).attr('id') == item_id) {
            $(this).show();
        } else {
            $(this).hide();
        }
    });
}

function initCarousel()
{
    if (!$('.carousel').is('*')) {
        return;
    }
    $('.carousel').jcarousel();
}

function initFavLink()
{
    if (!$('#fumzup-quote').is('*')) {
        return;
    }
    $('#fumzup-quote').after('<a href="#" id="favlink">Bookmark this page</a>');
    $('#favlink').jFav();
}

function initLoginForm()
{
    if (!$('#login').is('*')) {
        return;
    }

    var username_text = $('#login label[for="username"]').text();
    var password_text = $('#login label[for="password"]').text();

    // Insert values
    $('#login #username').attr('value', username_text);
    $('#login #password').attr('value', password_text);

    // Hide the labels
    $('#login label[for="username"], #login label[for="password"]').hide();

    $('#login #username').focus(function() {
        if ($(this).attr('value') == username_text) {
            $(this).attr('value', '');
        }
    });

    $('#login #username').blur(function() {
        if ($(this).attr('value') == '') {
            $(this).attr('value', username_text);
        }
    });

    $('#login #password').focus(function() {
        if ($(this).attr('value') == password_text) {
            $(this).attr('value', '');
        }
    });

    $('#login #password').blur(function() {
        if ($(this).attr('value') == '') {
            $(this).attr('value', password_text);
        }
    });
}

function initModules()
{
    if (!$('.module').is('*')) {
        return;
    }

    if (!$('.carousel').is('*')) {
        return;
    }

    // Add click event to carousel icons within each module
    $('.carousel a').each(function() {
        var module = $(this).parent().parent().parent().parent().parent().parent();
        $(this).click(function() {
            _toggleModuleItem(module, $(this).attr('href'));
            return false;
        });
    });

    // Hide all but the first item within each module
    $('.module').each(function() {
        var i = 1;
        $(this).children('.item').each(function() {
            if (i > 1) {
                $(this).hide();
            }
            i++;
        });
    });

    // Add click event to headings for each help module
    if ($('.help').is('*')) {
        $('h1 a').each(function() {
            var a = $(this);
            a.click(function() {
                $(a.attr('href')).slideToggle('normal', function() {
                    if (a.attr('class') == 'more') {
                        a.attr('class', 'less');
                    } else {
                        a.attr('class', 'more');
                    }
                });
                return false;
            });
        });
    }
}

function initNewsItems()
{
    if (!$('#news-items').is('*')) {
        return;
    }

    $('#news-items').after('<div class="news-items-nav"><div class="news-items-nav-prev">Previous story</div><div class="news-items-nav-next">Next story</div></div>');

    $('#news-items').serialScroll({
        items: 'div.item',
        prev: 'div.news-items-nav-prev',
        next: 'div.news-items-nav-next',
        duration: 700,
        axis: 'y'
    });
}

function initNewsList()
{
    if (!$('#latest-headline-news-list').is('*')) {
        return;
    }

    $('#latest-headline-news-list').serialScroll({
        items: 'li',
        duration: 500,
        force: true,
        axis: 'y',
        interval: 5000,
        step: 1
    });

    $('#latest-headline-news-list').hover(function(){
        $(this).stop().trigger('stop');
    },function(){
        $(this).stop().trigger('start');
    });
}

function initNewWindow()
{
    $('a').each(function() {
        $(this).click(function() {
            if ($(this).attr('rel') == 'external') {
                $(this).attr('target', '_blank');
                return;
            }
        });
    });
}

function initPartners()
{
    if (!$('.partners').is('*')) {
        return;
    }

    // Add click event to carousel icons within each module
    $('.partners ul li a').each(function() {
        $(this).click(function() {
            _toggleModuleItem($('.partners'), $(this).attr('href'));
            return false;
        });
    });

    // Hide all but the first item within each module
    var i = 1;
    $('.partners').children('.item').each(function() {
        if (i > 1) {
            $(this).hide();
        }
        i++;
    });
}

function initSearchResults()
{
    if (!$('#search-results').is('*')) {
        return;
    }

    $('#search-results h2 a').each(function() {
        var a = $(this);
        a.click(function() {
            $(a.attr('href')).slideToggle('normal', function() {
                if (a.attr('class') == 'more') {
                    a.attr('class', 'less');
                } else {
                    a.attr('class', 'more');
                }
            });
            return false;
        });
    });

    var i = 1;
    $('.search-results').each(function() {
        if (i > 0) {
            $(this).children('div:last').hide();
        }
        i++;
    });
}

function initProductLists()
{
    if (!$('#product-lists').is('*')) {
        return;
    }

    $('#product-lists h2 a').each(function() {
        var a = $(this);
        a.click(function() {
            $(a.attr('href')).slideToggle('normal', function() {
                if (a.attr('class') == 'more') {
                    a.attr('class', 'less');
                } else {
                    a.attr('class', 'more');
                }
            });
            return false;
        });
    });
    
    $('#products-results-1').each(function() {
            $(this).hide();
    });
    $('#products-results-2').each(function() {
            $(this).hide();
    });
	$('#products-results-3').each(function() {
            $(this).hide();
    });
	$('#products-results-4').each(function() {
            $(this).hide();
    });
	$('#products-results-5').each(function() {
            $(this).hide();
    });
}

function initCatNavLists()
{
    if (!$('#catnav-lists').is('*')) {
        return;
    }

    $('#catnav-lists h2 a').each(function() {
        var a = $(this);
        a.click(function() {
            $(a.attr('href')).slideToggle('normal', function() {
                if (a.attr('class') == 'more') {
                    a.attr('class', 'less');
                } else {
                    a.attr('class', 'more');
                }
            });
            return false;
        });
    });
    
    $('#catnav-results-1').each(function() {
            $(this).hide();
    });
    $('#catnav-results-2').each(function() {
            $(this).hide();
    });
	$('#catnav-results-3').each(function() {
            $(this).hide();
    });
}

function initSelect()
{
    if (!$('.select').is('*')) {
        return;
    }

    $('select.select').wrapAll('<div class="select"></div>');
    $('div.select').append('<span>Select</span>');
    $('select.select').css('opacity', '0');

    $('select.select').change(function() {
        var str = $('select.select option:selected').text();
        $('div.select span').text(str);

/*
COMMENTED DUE TO CONFLICTS
        // Handle Ajax category filter in Latest News section on About Us page
        if ($('form#news-search #category').is('*') && $('select.select').attr('id') == 'category') {
            $('#news-items').load('/news/category/' + $(this).val());
        }
*/

    }).change();
}

function initTestimonials()
{
    if (!$('.testimonial').is('*')) {
        return;
    }

    $('.testimonial').each(function() {
        var div = $(this).children('div');
        var a = $(this).children(':last').children('a');
        a.click(function() {
            div.slideToggle('fast', function() {
                if (a.hasClass('more')) {
                    a.toggleClass('less');
                } else {
                    a.toggleClass('more');
                }
            });
            return false;
        });
    });
}

function initProdlists()
{
    if (!$('.prodlist').is('*')) {
        return;
    }

    $('.prodlist').each(function() {
        var div = $(this).children('div');
        var a = $(this).children(':last').children('a');
        a.click(function() {
            div.slideToggle('fast', function() {
                if (a.hasClass('more')) {
                    a.toggleClass('less');
                } else {
                    a.toggleClass('more');
                }
            });
            return false;
        });
    });
}

function initTooltips()
{
    if (!$('.tooltips').is('*')) {
        return;
    }

    if ($('#fumzup-sidebar #team .tooltips').is('*')) {
        var id_attr = 'team-tooltip';
    } else {
        var id_attr = 'tooltip';
    }

    var x = -10;
    var y = -40;

    $('.tooltips').each(function() {
        $(this).children('dt').hover(function(e) {
            $('body').append('<div id="' + id_attr + '"><h2>' + $(this).text() + '</h2>' + $(this).next().html() + '</div>');
            $('#' + id_attr)
                .css('top', (e.pageY - x) + 'px')
                .css('left', (e.pageX + y) + 'px')
                .fadeIn('slow');
        },
        function() {
            $('#' + id_attr).remove();
        });
        $(this).children('dt').mousemove(function(e) {
            $('#' + id_attr)
                .css('top', (e.pageY - x) + 'px')
                .css('left', (e.pageX + y) + 'px');
        });
    });
}

function initValidate()
{
    if (!$('#feedback-form').is('*')) {
        return;
    }

    $('#feedback-form').validate({
        rules: {
            name: 'required',
            email: {
                required: true,
                email: true
            },
            subject: 'required',
            type: 'required',
            comments: 'required'
        },
        messages: {
            name: 'Please enter your name',
            email: 'Please enter a valid email address',
            subject: 'Please enter a subject',
            type: 'Please select a feedback type',
            comments: 'Please enter your comments'
        }
    });
    
}

function initSignupValidate()
{
	    $("#signup-form").validate({
		rules: {
			first_name: "required",
			surname: "required",
			website_name: "required",
			username: {
				required: true,
				minlength: 2
			},
			password: {
				required: true,
				minlength: 5
			},
			confirm_password: {
				required: true,
				minlength: 5,
				equalTo: "#password"
			},
			email: {
				required: true,
				email: true
			},
			mobile: {
				required: true,
				digits: true
			},
			postcode: {
				required: true,
				digits: true
			},
			dobyear: {
				required: true,
				digits: true,
				maxlength: 4,
				minlength: 4
			}
		},
		messages: {
			first_name: "Please enter your first name",
			surname: "Please enter your last name",
			website_name: "Please enter a nickname",
			username: {
				required: "Please enter a username",
				minlength: "Your username must consist of at least 2 characters"
			},
			password: {
				required: "Please provide a password",
				minlength: "Your password must be at least 5 characters long"
			},
			confirm_password: {
				required: "Please confirm your password",
				minlength: "Your password must be at least 5 characters long",
				equalTo: "Please enter the same password "
			},
			email: "Please enter a valid email address",
			mobile: {
				required: "Please enter your mobile number",
				digits: "Please enter only numeric characters"
			},
			postcode: {
				required: "Please enter your postcode",
				digits: "Please enter only numeric characters"
			},
			dobyear: {
				required: "Please enter your year of birth",
				digits: "Please enter only numeric characters",
				maxlength: "Please enter year in 'yyyy' format",
				minlength: "Please enter year in 'yyyy' format"
			}
		}
	});
	
	$("#password").blur(function() {
		$("#confirm_password").valid();
	});
}

function initSignupModals()
{
    $(document).ready(function(){		$('#signup-dialog').dialog({			width: 760,			draggable: false,			resizable: false,			modal: true,			autoOpen: false		});		$('#success').dialog({			width: 300,			height: 200,			draggable: false,			resizable: false,			modal: true,			autoOpen: false,			close:function(event){				$('#signup-dialog').dialog('close');			}		});		$('#dupe').dialog({			width: 300,			height: 200,			draggable: false,			resizable: false,			modal: true,			autoOpen: false		});		$('#error').dialog({			width: 300,			height: 200,			draggable: false,			resizable: false,			modal: true,			autoOpen: false,			close:function(event){				$('#signup-dialog').dialog('close');			}		});		$('#submit_button').click(function(event){			if ($("#signup-form").valid()){				$.getJSON(					remote_server_location + "ajax_signup.php?website_id=" + website_id + "&jsoncallback=?",					$("#signup-form").serialize(),					function(o_data){						if(o_data.status=='0'){							$('#success').dialog('open');						}else if(o_data.status=='1'){							$('#dupe').dialog('open');						}else{							$('#error').dialog('open');						}					}				);			}		});		$('div#popup_content').bind('dialogclose', function(event) {		     alert('closed'); 		});	});
}



$(document).ready(function() {
    initCarousel();
    initFavLink();
    initLoginForm();
    initModules();
    initNewsItems();
    initNewsList();
    initNewWindow();
    initPartners();
    initSearchResults();
    initProductLists();
    initCatNavLists();
    initSelect();
    initTestimonials();
    initProdlists();
    initTooltips();
    initValidate();
    initSignupValidate();
    initSignupModals();
});
