//Handle Flash
var flashvars = {};
var params = {
    wmode: "transparent",
    bgcolor: "transparent"
};
var attributes = {
    id: "player"
};
swfobject.embedSWF("/flash/standardVideo.swf", "video", "240", "180", "9.0.0", false, flashvars, params, attributes);
var attributes = {
    id: "olh"
};
swfobject.embedSWF("/flash/olhpepsicoheader.swf", "olh", "305", "192", "9.0.0", false, flashvars, params, attributes);
//---------------------------

//to check that one of the necessary checkboxes is checked
function necessaryChecked(){
    var result = false;
    $('.necessary').each(function(){
        if ($(this).is(':checked')) 
            result = true;
    });
    return result;
}

//called onload to see if the bottler list should show
function initBottlerList(){
    if (necessaryChecked()) {
        $('#Bottler').show().removeAttr("disabled");
    }
    else {
        $("#Bottler").hide().attr("disabled", true);
    }
}

//called onload to see if restaurant info should show
function initConsumerType(){
    if ($("input:radio[name=consumer_type]:checked").val() == "1000") {
        $('#Consumer').show();
        $('#Consumer input, #Consumer select').removeAttr('disabled');
    }
    else {
        $('#Consumer').hide();
        $('#Consumer input, #Consumer select').attr('disabled', 'true');
    }
}

function validateZip(zip){
    regex = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
    if (!regex.test(zip)) {
        return false;
    }
    else {
        return true;
    }
}

//create slideshow class
var slideshow = new function(){
    this.numSlides = 1, this.title = "slide", this.path = "/images/", this.ext = ".jpg"
    this.current = 1, this.imgClass = "viewer", this.pgNumClass = "pageNumber", this.next = function(){
        this.current++;
        var c = parseInt(this.current);
        var n = parseInt(this.numSlides);
        if (c > n) 
            this.current = 1;
        this.switchImg(this.path + this.title + this.current + this.ext);
    }
    this.prev = function(){
        this.current--;
        if (this.current < 1) 
            this.current = this.numSlides;
        this.switchImg(this.path + this.title + this.current + this.ext);
    }
    this.switchImg = function(image){
        $('.' + this.imgClass).attr("src", image);
    }
    this.updatePageNumber = function(){
        $('.' + this.pgNumClass).html(current);
    }
};


$(document).ready(function(){
    $('a[href^=/documents/]').each(function(i){
		href = $(this).attr('href');
		$(this).bind("click", function(e){
			dcsMultiTrack("DCS.dcssip","","DCS.dcsuri",href,"WT.ti","Download: "+href, "WT.dl", 20, "WT.nv", "download document");
		});
	});
	$('.TOC a[href]').each(function(i){
        $(this).bind("click", function(e){
            slideshow.switchImg($(this).attr("href"));
            slideshow.current = parseInt($(this).attr("name"));
            return false;
        });
    });
    $('.prev').bind("click", function(e){
        slideshow.prev();
        return false;
    });
    $('.next').bind("click", function(e){
        slideshow.next();
        return false;
    });
    
    $('.selectGo').each(function(i){
        var $this = $(this);
        $this.bind("click", function(e){
            ref = $this.attr('href');
            ref = $('#' + $this.attr('name')).val();
            $this.attr('href', ref);
        });
    });
    
    $('#zipSubmit').bind("click", function(e){
        ref = $(this).attr('href');
        zip = $('#zip').val();
        if (validateZip(zip)) {
            ref += zip;
            $(this).attr('href', ref);
        }
        else {
            alert('Zip Code is Invalid');
            $('#zip').val('');
            return false;
        }
    });
    var evt = $.browser.msie ? "click" : "change";
    
    initConsumerType();
    $("input:radio[name=consumer_type]").bind(evt, function(){
        if ($(this).val() == "1000") {
            $('#Consumer').show();
            $('#Consumer input, #Consumer select').removeAttr('disabled');
        }
        else {
            $('#Consumer').hide();
            $('#Consumer input, #Consumer select').attr('disabled', 'true');
        }
    });
    
    initBottlerList();
    $('.necessary').bind(evt, function(){
        if (necessaryChecked()) {
            $('#Bottler').show().removeAttr("disabled");
        }
        else {
            $("#Bottler").hide().attr("disabled", true);
        }
    });
    
    $('.toolTip').hover(function(){
        this.tip = this.title;
        $(this).append('<div class="toolTipWrapper">' +
        this.tip +
        '</div>');
        this.title = "";
        this.width = $(this).width();
        $(this).find('.toolTipWrapper').css({
            left: this.width - 20
        })
        $('.toolTipWrapper').fadeIn(100);
    }, function(){
        $('.toolTipWrapper').fadeOut(100);
        $(this).children().remove();
        this.title = this.tip;
    });
    
	//Ajax Captcha
	$('#RefreshCaptcha, #captcha img').bind("click", function(){
		$image = $('#captcha img').eq(0);
		$hiddenField = $('#captcha-id').eq(0);
		
		$.post("/ajax/update-captcha", { },
  		function(captcha){
    		$image.attr('src', captcha.src);
			$hiddenField.attr('value', captcha.token);
  		}, "json");
	});
	
	$('#searchBox').bind('blur', function(){
		if ( $(this).val() == "" )
			$(this).val('Search');
	});
	
	$('#searchBox').bind('click', function(){
		if ( $(this).val() == "Search" )
			$(this).val("");
	});
});
