function debug(s) {
	if(window.console && console.log) {
		console.log(s);
	}
}

function getSizes()
{
    // get viewport size
    var vw = $(window).width();
    var vh = $(window).height();

    // set the height and width of the .content
    var cw = vw;
    if(showtopyellowmenu == 1)
    {
        var ch = vh - 167;
    }
    else
    {
        var ch = vh - 137; // Webguru update on 02.07.2011
    }

    return [cw,ch];
}

function placeFooter()
{
    // place footer at bottom
    var sizes = getSizes();
    var cw = sizes[0];
    var ch = sizes[1];

    // set initial .content and .backgroundimageholder height
    $('#content').height(ch);
    $('#backgroundimageholder').height(ch);
}


// ORIGINAL resizeBackground Function Width over height
// Handles the home page backgrounds and content
function resizeBackground()
{
    $('#backgroundimageholder_home img').each(function(i) {
        var sizes = getSizes();
        var cw = sizes[0];
        var ch = sizes[1];

        var bgw = $(this).width();
        var bgh = $(this).height();

        if(cw / ch > bgw / bgh)
        {
            var scale = cw / bgw ;

            var nbgw = bgw * scale;
            var nbgh = bgh * scale;

            $(this).width(nbgw);
            $(this).height(nbgh);
        }
        else
        {
            scale = ch / bgh;

            nbgw = bgw * scale;
            nbgh = bgh * scale;

            $(this).width(nbgw);
            $(this).height(nbgh);
        }
        var left = -((nbgw-cw)/2);
        var top = -((nbgh-ch)/2);

        $(this).css({'left':left,'top':top});
    });
}

// Handles the home page backgrounds and content
// this one contains the adjustments made for the horizontal
// view on the iphone
/*
function resizeBackground()
{
    $('#backgroundimageholder_home img').each(function(i) {
        var sizes = getSizes();
        var cw = sizes[0];
        var ch = sizes[1];

        var bgw = $(this).width();
        var bgh = $(this).height();

        if(cw / ch > bgw / bgh)
        {
            var scale = ch / bgh ;

            var nbgw = bgw * scale;
            var nbgh = bgh * scale;

            $(this).width(nbgw);
            $(this).height(nbgh);
        }
        else
        {
            scale = cw / bgw;

            nbgw = bgw * scale;
            nbgh = bgh * scale;

            $(this).width(nbgw);
            $(this).height(nbgh);
        }
        var left = -((nbgw-cw)/2);
        var top = -((nbgh-ch)/2);

        $(this).css({'left':left,'top':top});
    });
}
*/

function resizeBackgoundLink() // maybe i will use this for image background linking
{
    var sizes = getSizes();
    var cw = sizes[0];
    var ch = sizes[1];

    var bgw = $(this).width();
    var bgh = $(this).height();

    if(cw / ch > bgw / bgh)
    {
        var scale = cw / bgw ;

        var nbgw = bgw * scale;
        var nbgh = bgh * scale;

        $('#homepagepicturelink').width(nbgw);
        $('#homepagepicturelink').height(nbgh);
    }
    else
    {
        scale = ch / bgh;

        nbgw = bgw * scale;
        nbgh = bgh * scale;

        $('#homepagepicturelink').width(nbgw);
        $('#homepagepicturelink').height(nbgh);
    }
    var left = -((nbgw-cw)/2);
    var top = -((nbgh-ch)/2);

    $('#homepagepicturelink').css({'left':left,'top':top});
}

function placeContentContainer()
{
    var sizes = getSizes();
    var cw = sizes[0];
    var ch = sizes[1];

    $('#contentcontainer_home ul li').each(function(i){
        var cch = $('#contentcontainer_home ul li').height();
        var ccw = $('#contentcontainer_home ul li').width();

        //var cctop = (ch-cch)/2; // this would place it in the middle of the container
        var cctop = ch - cch; // this one places it at the bottom
        var ccleft = cw-ccw-20;

        $('#contentcontainer_home ul li').css({'top':cctop,'left':ccleft});
    });
}

// Handles all the other page backgrounds and content placement
function resizeBackground_all()
{
    $('#backgroundimageholder_all img').each(function(i) {
        var sizes = getSizes();
        var cw = sizes[0];
        var ch = sizes[1];

        var bgw = $(this).width();
        var bgh = $(this).height();

        if(cw / ch > bgw / bgh)
        {
            var scale = cw / bgw ;

            var nbgw = bgw * scale;
            var nbgh = bgh * scale;

            $(this).width(nbgw);
            $(this).height(nbgh);
        }
        else
        {
            scale = ch / bgh;

            nbgw = bgw * scale;
            nbgh = bgh * scale;

            $(this).width(nbgw);
            $(this).height(nbgh);
        }
        var left = -((nbgw-cw)/2);
        var top = -((nbgh-ch)/2);

        $(this).css({'left':left,'top':top});
    });
}

function placeContentContainer_all()
{
    var sizes = getSizes();
    var cw = sizes[0];
    var ch = sizes[1];

    var cch = $('#contentgrp_a').height();
    var ccw = $('#contentgrp_a').width();

    var cctop = (ch-cch)/2;
    var ccleft = (cw-ccw)/2;

    $('#contentgrp_a').css({'top':cctop,'left':ccleft});
}

function homepageRotator()
{
    var numofimgs = $('#backgroundimageholder_home img').length;
    var index = 0;

    //run the rotator
    rotator_too();

    function rotator_too()
    {
        $('#backgroundimageholder_home img').eq(index).fadeIn('1000').delay('9000').fadeOut('1000');
        $('#contentcontainer_home ul li').eq(index).delay('5000').fadeIn('1000').delay('3000').fadeOut('1000');


        index = index + 1;

        if(index == numofimgs)
        {
            index = 0;
        }

        setTimeout(function() {rotator_too()},9000)
    }
}

function placeEvents()
{

    // this is a modified version of the gallery js file that will instead do the same thing but for the events page
    // numofimages, isn't actually counting the number of images, it's counting the number of container divs that hold events

    var sizes = getSizes();
    var cw = sizes[0];
    var ch = sizes[1];

    // change the max content width to change the size the content container will be that's holding the images
    // it used to be 75%
    var maxgalw = parseInt(cw * 1); // max content width 75% of screen width
    var maxgalh = parseInt(ch * 1); // max content height 75% of screen height

    var numofimages = $("#contentgrp_b div.contentgrp_b_cont").length;

    var cwbnumimages = 360 * numofimages; // conten width based on num of images 360px * num of images

    if(cwbnumimages > maxgalw)
    {
        var galw = parseInt(maxgalw /360) * 360;

        var numperrow = (galw / 360);

        var numrows = numofimages / numperrow;

        if(numofimages % numrows > 0)
        {
            numrows = numrows + 1;
        }
    }
    else
    {
        var galw = cwbnumimages;

        var galh = 200;
    }

    var galh = parseInt(numrows) * 200;

    // uncomment the galtop line below to always have the images placed in a container at the center of the screen vertically
    var galleft = (cw-galw)/2;
    //var galtop = (ch-galh)/2;

    // 'top':galtop, - put this back in to always have the images placed in a container at the center of the screen vertically
    $('#contentgrp_b').css({'width':galw,'height':galh,'left':galleft});
}

function galleryHandler()
{
    // get the number of images to display per page
    var perpage = calcGalleryPerPage();
    //console.log('PER PAGE: '+perpage);
    // set the file that will be used to load images
    var datapage = "/ajax/loadgalleryimages/";
    //console.log('DATA PAGE: '+datapage);
    // load the first set of images
    window.currpage = 1;
    //console.log('CURR PAGE: '+currpage);
    $("#gallerycontainer").load(datapage + currpage + '/' + perpage, function() {
        placeGallery();
        placePagiNext();
        updatePagiClass();
    });

    // previous handler
    $('#paginationprev').click(function(){
        if(currpage>1)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage - 1;
            // Load Data
            $('#gallerycontainer').load(datapage + newpage + '/' + perpage, function() {placeGallery();updatePagiClass()});
        }
    });

    // next handler
    $('#paginationnext').click(function(){
        if(currpage<maxpages)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage + 1;
            // Load Data
            $('#gallerycontainer').load(datapage + newpage + '/' + perpage, function() {placeGallery();updatePagiClass()});
        }
    });

}

function videosHandler()
{
    // get the number of images to display per page
    var perpage = calcGalleryPerPage();

    // set the file that will be used to load images
    var datapage = "/ajax/loadbrittotv/";

    // load the first set of images
    window.currpage = 1;
    $("#gallerycontainer").load(datapage + currpage + '/' + perpage, function() {
        placeGallery();
        placePagiNext();
        updatePagiClass();
    });

    // previous handler
    $('#paginationprev').click(function(){
        if(currpage>1)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage - 1;
            // Load Data
            $('#gallerycontainer').load(datapage + newpage + '/' + perpage, function() {placeGallery();updatePagiClass();});
        }
    });

    // next handler
    $('#paginationnext').click(function(){
        if(currpage<maxpages)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage + 1;
            // Load Data
            $('#gallerycontainer').load(datapage + newpage + '/' + perpage, function() {placeGallery();updatePagiClass();});
        }
    });
}

function eventsHandler()
{
    // get the number of images to display per page
    var perpage = calcDivsPerPage();

    // set the file that will be used to load images
    var datapage = "/ajax/loadeventgallery/";

    // load the first set of images
    window.currpage = 1;
    $("#contentgrp_b").load(datapage + currpage + '/' + perpage, function() {
        placeEvents();
        placePagiNext();
        updatePagiClass();
    });

    // previous handler
    $('#paginationprev').click(function(){
        if(currpage>1)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage - 1;
            // Load Data
            $('#contentgrp_b').load(datapage + newpage + '/' + perpage, function() {placeEvents();updatePagiClass();});
        }
    });

    // next handler
    $('#paginationnext').click(function(){
        if(currpage<maxpages)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage + 1;
            // Load Data
            $('#contentgrp_b').load(datapage + newpage + '/' + perpage, function() {placeEvents();updatePagiClass();});
        }
    });
}

function newsletterHandler()
{
    // get the number of images to display per page
    var perpage = calcDivsPerPage();

    // set the file that will be used to load images
    var datapage = "/ajax/loadnewsletters/";

    // load the first set of images
    window.currpage = 1;
    $("#contentgrp_b").load(datapage + currpage + '/' + perpage, function() {
        placeEvents();
        placePagiNext();
        updatePagiClass();
    });

    // previous handler
    $('#paginationprev').click(function(){
        if(currpage>1)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage - 1;
            // Load Data
            $('#contentgrp_b').load(datapage + newpage + '/' + perpage, function() {placeEvents();updatePagiClass();});
        }
    });

    // next handler
    $('#paginationnext').click(function(){
        if(currpage<maxpages)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage + 1;
            // Load Data
            $('#contentgrp_b').load(datapage + newpage + '/' + perpage, function() {placeEvents();updatePagiClass();});
        }
    });
}

function specialProjHandler()
{
    // get the number of images to display per page
    var perpage = calcDivsPerPage();

    // set the file that will be used to load images
    var datapage = "/ajax/loadspecialprojgallery/";

    // load the first set of images
    window.currpage = 1;
    $("#contentgrp_b").load(datapage + currpage + '/' + perpage, function() {
        placeEvents();
        placePagiNext();
        updatePagiClass();
    });

    // previous handler
    $('#paginationprev').click(function(){
        if(currpage>1)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage - 1;
            // Load Data
            $('#contentgrp_b').load(datapage + newpage + '/' + perpage, function() {placeEvents();updatePagiClass();});
        }
    });

    // next handler
    $('#paginationnext').click(function(){
        if(currpage<maxpages)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage + 1;
            // Load Data
            $('#contentgrp_b').load(datapage + newpage + '/' + perpage, function() {placeEvents();updatePagiClass();});
        }
    });
}

function eventCalHandler()
{
    // get the number of images to display per page
    var perpage = calcDivsPerPage();

    // set the file that will be used to load images
    var datapage = "/ajax/loadeventcalendargallery/";

    // load the first set of images
    window.currpage = 1;
    $("#contentgrp_b").load(datapage + currpage + '/' + perpage, function() {
        placeEvents();
        placePagiNext();
        updatePagiClass();
    });

    // previous handler
    $('#paginationprev').click(function(){
        if(currpage>1)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage - 1;
            // Load Data
            $('#contentgrp_b').load(datapage + newpage + '/' + perpage, function() {placeEvents();updatePagiClass();});
        }
    });

    // next handler
    $('#paginationnext').click(function(){
        if(currpage<maxpages)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage + 1;
            // Load Data
            $('#contentgrp_b').load(datapage + newpage + '/' + perpage, function() {placeEvents();updatePagiClass();});
        }
    });
}

function eventsSubGalleryHandler()
{
    // get the number of images to display per page
    var perpage = calcGalleryPerPage();

    // set the file that will be used to load images
    var datapage = "/ajax/loadeventssubgalleryimages/";

    // load the first set of images
    window.currpage = 1;
    $("#gallerycontainer").load(datapage + currpage + '/' + perpage, function() {
        placeGallery();
        placePagiNext();
        updatePagiClass();
    });

    // previous handler
    $('#paginationprev').click(function(){
        if(currpage>1)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage - 1;
            // Load Data
            $('#gallerycontainer').load(datapage + newpage + '/' + perpage, function() {placeGallery();updatePagiClass();});
        }
    });

    // next handler
    $('#paginationnext').click(function(){
        if(currpage<maxpages)
        {
            // run a loading function here so that it looks good
            // LOADING FUNCTION()

            var newpage = window.currpage + 1;
            // Load Data
            $('#gallerycontainer').load(datapage + newpage + '/' + perpage, function() {placeGallery();updatePagiClass();});
        }
    });

}

function updatePagiClass()
{
    if(currpage>1)
    {
        $('#paginationprev').show();
    }
    else
    {
        $('#paginationprev').hide();
    }

    if(currpage<maxpages)
    {
        $('#paginationnext').show();
    }
    else
    {
        $('#paginationnext').hide();
    }
}

function placeGallery()
{
    var sizes = getSizes();

    var cw = sizes[0];   
   
   	var ch = sizes[1];  

    var maxgalw = parseInt(cw * 1);
	
	var maxgalh = parseInt(ch * 1);

    var numofimages = $("#gallerycontainer img").length;

    var cwbnumimages = 124 * numofimages;

    if(cwbnumimages > maxgalw)
    {
        var galw = parseInt(maxgalw /124) * 124;

        var numperrow = (galw / 124);

        var numrows = numofimages / numperrow;

		if(numofimages % numperrow > 0)
        {
            numrows = numrows + 1;
        }

		/*
        if(numofimages % numrows > 0)
        {
            numrows = numrows + 1;   console.log("numrows second="+numrows);
        }
        */
    }
    else
    {
        var galw = cwbnumimages;  

        var galh = 124;  
    }

    var galh = parseInt(numrows) * 124; 

    var galleft = (cw-galw)/2;  

    var galtop = (ch-galh)/2;  

    $('#gallerycontainer').css({'width':galw,'top':galtop,'height':galh,'left':galleft});
}

function calcGalleryPerPage()
{
    var sizes = getSizes();
    var cw = sizes[0];
    var ch = sizes[1];

    var numcols = parseInt(cw / 124);
    var numrows = parseInt(ch / 124);

    var galperpage = numcols * numrows;

    return galperpage;
}

function addGalPerPageToSession(galperpage)
{
    // lets pass the number per page into a session
    $.ajax({
        url: "/front/set_gal_page_sizes",
        type: "post",
        data: {galperpage: galperpage},
        success: function(data){
            //alert('saved');
        },
        error: function(){
            //alert('error');
        }
    });
}

function calcDivsPerPage()
{
    var sizes = getSizes();
    var cw = sizes[0];
    var ch = sizes[1];

    var numcols = parseInt(cw / 360);
    var numrows = parseInt(ch / 200);

    var divsperpage = numcols * numrows;

    return divsperpage;
}

function showMediaResultsFirst()
{
    $('#thenewsresults').slideUp();
    $('#theeventresults').slideUp();
    setTimeout(function() {$('#themediaresults').slideDown('slow',function() {
        $(function() {
            var settings = {autoReinitialise: true};
            $('.resultswrapper').jScrollPane(settings);
        });
    });
    },1000);
    $('#showmediaresults').addClass('activesearch');
    $('#shownewsresults').removeClass('activesearch');
    $('#showeventresults').removeClass('activesearch');
}


// functions I'm not sure about anymore
function placePagiNext()
{
    var sizes = getSizes();
    var cw = sizes[0];
    var ch = sizes[1];

    var paginexttop = ch - 45;

    $('#paginationnext').css({'top':paginexttop});
}

function slidethegal()
{
    $("#gallerycontainer").animate({
        top: "100%"
    }, 1500 );
}

function addDivsPerPageToSession(divsperpage)
{
    // lets pass the number per page into a session
    $.ajax({
        url: "/front/set_divs_page_sizes",
        type: "post",
        data: {divsperpage: divsperpage},
        success: function(data){
            //alert('saved');
        },
        error: function(){
            //alert('error');
        }
    });
}

validate_required_fields = function(form_id) {
	var form = $(form_id);
	form.addClass("validating");
	var missing = [];
	var A = {};
	A.validated = function(){
		missing = [];
		$(".required:visible",form).each(function(i,e){
			if($.trim($(e).val())=="") {
				missing.push({id:$(e).attr("id") || null, name:$(e).attr("name")||null, title:$(e).attr("title")||null });
			}
		});
		return (missing.length==0 ? true : false);
	};
	A.highlight = function(){
		$(".required:visible",form).removeClass("highlight_required");
		$(".required:visible",form).each(function(i,e){
			if($.trim($(e).val())=="") {
				$(e).addClass("highlight_required");
                $(e).parentsUntil('.inputsholder').prev().prev().addClass("redlabel");
			}
		});
	};
	A.get_missing = function(){
		return missing;
	};
	A.check_field = function(e){
		if($.trim(e.value) != "") {
			$(e).removeClass("highlight_required");
            $(e).parentsUntil('.inputsholder').prev().prev().removeClass("redlabel");
		}
	};
	A.check_all = function(){
		$(".required:visible",form).each(function(i,e){
			A.check_field(e);
		});
	};
	
	$(".highlight_required:visible",form).live("focusout keyup",function(e){
		A.check_field(this);
	});
	
	return A;
};

