/* Author:
    Wyatt Tigert
*/

//Jquery Exists Function
jQuery.fn.exists = function(){return jQuery(this).length>0;}

var currentHref = null;

$(document).ready(function() {
  winBgSize();
  getColorClass();
});

function winBgSize() {
  var bgImage = $("#dynamic-background");
  var bgFile = bgImage.attr('name');
  var winWidth = $(window).width();
  var winHeight = $(window).height();

  if(winWidth > 1400) {
    bgImage.attr('src', '../../img/bgs/' + bgFile + '-BIG.jpg');
  }

  $(window).resize(function() {
    console.log("BLARGH");
    var winWidth = $(window).width();
    var winHeight = $(window).height();

    if(winWidth > 1400) {
      bgImage.attr('src', '../../img/bgs/' + bgFile + '-BIG.jpg');
    } else {
      bgImage.attr('src', '../../img/bgs/' + bgFile + '.jpg');
    }
  });
}

function getColorClass(){
	$.ajax({
		type: "POST",
		url: "../flash/cube/cube.php",
		data: {action: 'getColorClass'},
		success: function(response){
			$('ul.crumblist a').addClass(response);
			$('ul.crumblist span').addClass(response);
			if(response == 'random')
				$('#connect span').addClass('color' + Math.ceil(Math.random()*6));
			else
				$('#connect span').addClass(response);
		}
	});
}
/*$('a').click(function(){
	currentHref = this.href;
	return true;
});

$(window).bind("beforeunload", function(){
	if(!currentHref){
		$.ajax({
			type: 'POST',
			url: '../flash/cube/cube.php',
			data: {action: 'clearCubeShown'}
		});
	}
});*/
//Scrolling Function
if($('#main').hasClass('scrollable')) {
	scrollBind();
	if($('#scroller').width() <= $('#main').width())
		$('.next').hide();
}
else {
	$('.next').hide();
	$('.previous').hide();
}

//move connect tab off screen
$("#connect").css('right', -180);

//setup hover slide for connect tab
$("#connect").hover(
    function() {
        $(this).stop().animate({right: 0}, 'fast');
    },
    function() {
        $(this).stop().animate({right: -180}, 'fast');
    }
);


//future hover thingy
/*if($(window).height() <= 720) {
	$('footer').css('bottom', -50);
	$(document).mousemove(function(e) {
		if(e.pageY >= 570 && !$('footer').hasClass('alive')) {
			$('footer').animate({bottom : 0}, 'fast').addClass('alive');
		}
		else if (e.pageY < 570 && $('footer').hasClass('alive')) {
			$('footer').animate({bottom : -50}, 'fast').removeClass('alive');
		}
		else {
			//console.log(e.pageY);
		}
	});
}*/

/*-----------------------
 * SCROLLING JS
 * --------------------*/

function scrollBind () {
	//Regular Mousewheel scroll (vertical -> horizontal)
	$('#main').bind('mousewheel', function(event,delta) {
		this.scrollLeft -= (delta*60);
		event.preventDefault();
	});

	//Button Scrolling
	var moving;
	var mdown = false;
	$('.next').bind('mousedown', function() {
		mdown = true;
		setTimeout(function(){mdown = false}, 200);
		moving = setInterval(function(){$('#main').scrollLeft($('#main').scrollLeft() + 5)}, 1);
	});
	$('.next').bind('mouseup', function() {
		clearInterval(moving);
		if (mdown)
			$('#main').animate({ scrollLeft : '+=800'}, 1000);
	});
	$('.previous').bind('mousedown', function() {
		mdown = true;
		setTimeout(function(){mdown = false}, 200);
		moving = setInterval(function(){$('#main').scrollLeft($('#main').scrollLeft() - 5)}, 1);
	});
	$('.previous').bind('mouseup', function() {
		clearInterval(moving);
		if (mdown)
			$('#main').animate({ scrollLeft : '-=800'}, 1000);
	});

	//Button Hiding
	$('#main').bind('scroll', function() {
		var n = $('#main').scrollLeft();
		if (n == 0)
			$('.previous').stop(true,true).fadeOut('slow');
		else if (n == ($('#scroller').width() - $(window).width()))
			$('.next').stop(true,true).fadeOut('slow');
		else {
			$('.previous').stop(true,true).fadeIn('slow');
			$('.next').stop(true,true).fadeIn('slow');
		}
	});

	if ($('.previous').exists()) {
		//Hide previous button
		$('.previous').hide();

		if ($('#scroller').width() < $(window).width()) {
			$('.next').hide();
		}

		$(window).bind('resize', function () {
			if ($('#scroller').width() <= $(window).width())
				$('.next').hide();
			else
				$('.next').show();
		});
	}
}


/*-----------------------
 * OTHER JS
 * --------------------*/

//Page specific functions


$.fn.venn = function () {
    diag = this;

    $(diag).find('.content').hover(function() {
        $(diag).css('background-position', '-543px 0');
        $('.line').css('background', 'rgb(236, 0, 140)');
        $('.blurb.cont').stop(true, true).fadeIn('fast');
    },
    function() {
        $(diag).css('background-position', '0 0');
        $('.line').css('background', 'rgb(204, 204, 204)');
        $('.blurb.cont').stop(true, true).fadeOut('fast');
    });

    $(diag).find('.culture').hover(function() {
        $(diag).css('background-position', '0 -544px');
        $('.line').css('background', 'rgb(0, 174, 239)');
        $('.blurb.cult').stop(true, true).fadeIn('fast');
    },
    function() {
        $(diag).css('background-position', '0 0');
        $('.line').css('background', 'rgb(204, 204, 204)');
        $('.blurb.cult').stop(true, true).fadeOut('fast');
    });

    $(diag).find('.conversion').hover(function() {
        $(diag).css('background-position', '-543px -544px');
        $('.line').css('background', 'rgb(255, 204, 8)');
        $('.blurb.conv').stop(true, true).fadeIn('fast');
    },
    function() {
        $(diag).css('background-position', '0 0');
        $('.line').css('background', 'rgb(204, 204, 204)');
        $('.blurb.conv').stop(true, true).fadeOut('fast');
    });
};

$.fn.brainHover = function () {
    var container = this;

    hovers = $(container).find('.hover');

    $.each(hovers, function(i, hoverObj) {
        hoverObj.type = $(hoverObj).attr('class').slice(6);
        hoverObj.quote = $('#quotes').find('.quote.' + hoverObj.type);
        $(hoverObj).hover(function() {
            $('.page-title p').hide();
            $('#quotes').show();
            $(hoverObj.quote).stop(true, true).fadeIn('fast');
            $('.line').addClass(hoverObj.type);
        },
        function() {
            $('#quotes').hide();
            $('.page-title p').stop(true, true).fadeIn('fast');
            $('.line').removeClass(hoverObj.type);
            $(hoverObj.quote).hide();

        });
    });
};


$.fn.benefitsHover = function () {
    var container = this;

    benefits = $(this).find('div');

    $.each(benefits, function(i, benefit) {
        set = $(benefit).attr('id');

        links = $('.set' + '.' + set).find('li');
        blurbs = $(benefit).find('p');

        $.each(links, function(i, link) {
            link.blurb = blurbs[i];
            $(link).hover(function () {
                $(link.blurb).addClass('show');
				$('#benefits').css('background-position', '-546px 0');
            },
            function () {
                $(link.blurb).removeClass('show');
				$('#benefits').css('background-position', '0 0');
            });
        });
    });
};

function uniqueRandCoord (xSize, ySize, numOfUnique) {
    if ((xSize * ySize) < numOfUnique) {
        console.log ('Requesting Too Many Values to Be Unique');
        return false;
    }
    else {
        var coords = [];

        while(coords.length < numOfUnique) {
            var loc = {
                "x" : "",
                "y" : ""
            };
            loc.x = Math.ceil(Math.random() * xSize);
            loc.y = Math.ceil(Math.random() * ySize);
            var found = false;
            for(var i = 0; i < coords.length; i++) {
                if(coords[i].x == loc.x && coords[i].y == loc.y) {
                    found = true;
                    break;
                }
            }
            if(!found)
                coords[coords.length] = loc;
        }
        return coords;
    }
};

function uniqueRand (size, numOfUnique) {
    if (size < numOfUnique) {
        console.log ('Requesting Too Many Values to Be Unique');
        return false;
    }
    else {
        var arr = [];

        while(arr.length < numOfUnique) {
            var randNum = Math.ceil(Math.random() * size);
            var found = false;
            for(var i = 0; i < arr.length; i++) {
                if(arr[i] == randNum) {
                    found = true;
                    break;
                }
            }
            if(!found)
                arr[arr.length] = randNum;
        }
        return arr;
    }
};

$.fn.animateAwards = function () {
    var container = this;

    coords = uniqueRandCoord(8, 4, 24);

    bgPositions = uniqueRand(24, 24);

    for (var i = 0; i < 24; i++) {
        x = ((coords[i].x - 1) * 118) + (10 * (coords[i].x -1));
        if (coords[i].y -1 == 0)
            y = (coords[i].y - 1) * 118;
        else
            y = ((coords[i].y - 1) * 128);
        bg = '-' + ((bgPositions[i] - 1) * 115) + 'px 0px';
        color = 'color' + Math.ceil(Math.random() * 3);
        tile = '<div class="tile" style="top:' + y + 'px; left:' + x + 'px; background-position:' + bg + ';">\n';
        sub = '<div class="sub ' + color + '"></div>\n';
        tile = tile + sub + '</div>\n';
        $(container).append(tile);
    }

    function causeFadeIn() {
        tiles = $(container).find('.tile');
        $.each(tiles, function(i, tile) {
            setTimeout(function() {
                $(tile).fadeIn('slow', function () {
                    setTimeout(function() {
                        $(tile).find('.sub').fadeOut('slow');
                    }, Math.random() * 3000);
                });
            }, Math.random() * 3000);
        });
    };

    setTimeout(function() { causeFadeIn() }, 300);
};

$.fn.heroSetup = function () {
	//var $self = $(this);
    var container = this;
    var officers = $(container).find('.officer');

	officers = $(container).find('.officer');
    $.each(officers, function(i, officer) {
        officer.slider = $(officer).find('.slider');
        officer.slideControl = $(officer).find('h1');

        $(officer.slideControl).click(function() {
            if ($(officer.slider).hasClass('active')) {
                $(officer).stop().animate({ width : '405px'}, 500, function() {
                    $('#scroller').animate({ width : '-=500px' }, 400);
                });
                $(officer.slider).removeClass('active');
                $(this).removeClass('active');
            }
            else {
                $('#scroller').animate({ width : '+=500px' }, 400);
                $(officer).stop().animate({ width : '905px'}, 500);
                $(officer.slider).addClass('active');
                $(this).addClass('active');
                $('.next').fadeIn('slow');
            }
        });
    });
};

$.fn.subHover = function () {
    var container = this;

    mains = $(container).find('div').has('.sub');

    $.each(mains, function(i, main) {
        main.sub = $(main).find('.sub');
        $(main).hover(function() {
            $(main.sub).show();
        },
        function () {
            $(main.sub).hide();
        });
    });
};

$.fn.generateFAQ = function () {
    var container = this;

    questions = $(container).find('#questions .question');
    answers = $(container).find('#answers .answer');

    $.each(answers, function(i, answer) {
        $(answer).find('.close').click(function() {
            $('#answers').hide();
            $(answer).hide();
        });
    });

    i = 0;
    $.each(questions, function(i, question) {
        question.answer = answers[i];
        $(question).click(function () {
            $('#answers').show(0, function() {
                $(question.answer).fadeIn('slow');
            });
        });
        i++;
    });

};

$.fn.bookHover = function () {
    var container = this;

    links = $(container).find('.link');

    $.each(links, function(i, link) {
        link.img = $(link).find('.cover');
        $(link).hover(function() {
            $(link.img).stop(true, true).width(link.img.width() + 15);
        },
        function () {
            $(link.img).stop(true, true).width(link.img.width() - 15);
        });
    });

};

$.fn.experimentHover = function () {
    var container = this;

    blurbs = $(container).find('.blurb');

    $.each(blurbs, function(i, blurb) {
        blurb.img = $(blurb).find('img');
        blurb.link = $(blurb).parents('.experiment').find('a');
        blurb.color = $(blurb).find('.color');
        $(blurb.link).hover(function() {
            $(blurb.color).stop(true, true).fadeIn('fast');
            $(blurb.img).stop(true, true).animate({
                bottom : '-10px'
            }, 'fast');
        },
        function () {
            $(blurb.color).stop(true, true).fadeOut('fast');
            $(blurb.img).stop(true, true).animate({
                bottom : '-179px'
            }, 'fast');
        });
        $(blurb.link).click(function() {
            $(blurb.color).hide();
        });
    });
};

/* PHASE 2 TRANSITIONS START
$.fn.ajaxOverlay = function () {
	frame = this;
    curScroll = $(frame).find('#scroller');
	bclass = $('body').attr('class');

	$('.close').hide();

    links = $(frame).find('.link');

    $.each(links, function(i, link) {
        link.loc = $(link).attr('href');
        $(link).click(function() {
            ajaxLoad(link.loc);
			return false;
        });
    });

	function ajaxLoad(url) {
		toLoad = url + ' #scroller';

		old = $('#scroller').clone().hide();

		$(frame).load(toLoad, '', function () {
			$('body').removeClass(bclass).addClass('template');
			$('#scroller').hide().delay(300).fadeIn('slow');
			$('.close').show();
		});
	}

	$('.close').click(function () {
		$('body').attr('class', bclass);
		$('#scroller').remove();
		$('#main').append(old);
		$('#scroller').delay(300).fadeIn('slow');
		$(this).hide();
	});
};
*/

